Skip to content

Enhancement: Make tables import asynchronous #1801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ Have a good time and manage whatever you want.
<command>OCA\Tables\Command\CleanLegacy</command>
<command>OCA\Tables\Command\TransferLegacyRows</command>
</commands>
<activity>
<settings>
<setting>OCA\Tables\Activity\Setting</setting>
</settings>
<filters>
<filter>OCA\Tables\Activity\Filter</filter>
</filters>
<providers>
<provider>OCA\Tables\Activity\Provider</provider>
</providers>
</activity>
<navigations>
<navigation>
<name>Tables</name>
Expand Down
11 changes: 8 additions & 3 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@

// import
['name' => 'import#previewImportTable', 'url' => '/import-preview/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#importInTable', 'url' => '/import/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#scheduleImportInTable', 'url' => '/import/table/{tableId}/jobs', 'verb' => 'POST'],
['name' => 'import#scheduleImportInView', 'url' => '/import/view/{viewId}/jobs', 'verb' => 'POST'],
['name' => 'import#previewImportView', 'url' => '/import-preview/view/{viewId}', 'verb' => 'POST'],
['name' => 'import#importInView', 'url' => '/import/view/{viewId}', 'verb' => 'POST'],
['name' => 'import#previewUploadImportTable', 'url' => '/importupload-preview/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#importUploadInTable', 'url' => '/importupload/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#scheduleImportUploadInTable', 'url' => '/importupload/table/{tableId}/jobs', 'verb' => 'POST'],
['name' => 'import#previewUploadImportView', 'url' => '/importupload-preview/view/{viewId}', 'verb' => 'POST'],
['name' => 'import#scheduleImportUploadInView', 'url' => '/importupload/view/{viewId}/jobs', 'verb' => 'POST'],
// deprecated endpoints
['name' => 'import#importUploadInTable', 'url' => '/importupload/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#importUploadInView', 'url' => '/importupload/view/{viewId}', 'verb' => 'POST'],
['name' => 'import#importInTable', 'url' => '/import/table/{tableId}', 'verb' => 'POST'],
['name' => 'import#importInView', 'url' => '/import/view/{viewId}', 'verb' => 'POST'],

// search
['name' => 'search#all', 'url' => '/search/all', 'verb' => 'GET'],
Expand Down
30 changes: 30 additions & 0 deletions lib/Activity/ActivityConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Activity;

class ActivityConstants {
/*****
* Types can have different Settings for Mail/Notifications.
*/
public const TYPE_IMPORT_FINISHED = 'tables_import_finished';

/*****
* Subjects are internal 'types', that get interpreted by our own Provider.
*/

/**
* Somebody shared a form to a selected user
* Needs Params:
* "user": The userId of the user who shared.
* "formTitle": The hash of the shared form.
* "formHash": The hash of the shared form
*/
public const SUBJECT_IMPORT_FINISHED = 'import_finished_subject';

public const MESSAGE_IMPORT_FINISHED = 'import_finished_message';
}
39 changes: 39 additions & 0 deletions lib/Activity/ActivityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Activity;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Model\ImportStats;
use OCP\Activity\IManager;

class ActivityManager {
public function __construct(
protected IManager $activityManager,
) {
}

public function notifyImportFinished(string $userId, int $tableId, ImportStats $importStats): void {

$activity = $this->activityManager->generateEvent();
$activity->setApp(Application::APP_ID)
->setType(ActivityConstants::TYPE_IMPORT_FINISHED)
->setAuthor($userId)
->setObject('table', $tableId)
->setAffectedUser($userId)
->setSubject(ActivityConstants::SUBJECT_IMPORT_FINISHED, [
'actor' => $userId,
'tableId' => $tableId,
])
->setMessage(ActivityConstants::MESSAGE_IMPORT_FINISHED, [
'actor' => $userId,
'tableId' => $tableId,
] + (array)$importStats);

$this->activityManager->publish($activity);
}
}
45 changes: 45 additions & 0 deletions lib/Activity/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Tables\Activity;

use OCA\Tables\AppInfo\Application;
use OCP\Activity\IFilter;
use OCP\IL10N;
use OCP\IURLGenerator;

class Filter implements IFilter {
public function __construct(
protected IL10N $l,
protected IURLGenerator $url,
) {
}

public function getIdentifier(): string {
return Application::APP_ID;
}

public function getName(): string {
return $this->l->t('Tables');
}

public function getPriority(): int {
return 40;
}

public function getIcon(): string {
return $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
}

public function filterTypes(array $types): array {
return $types;
}

public function allowedApps(): array {
return [Application::APP_ID];
}
}
126 changes: 126 additions & 0 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Activity;

use OCA\Tables\AppInfo\Application;
use OCP\Activity\Exceptions\UnknownActivityException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying file does not seem to be committed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this exception should be located somewhere in the core. I've just stolen code to publish/parse activity from another module

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed. But this is available only as of NC 30, on main we still support 29.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, how to deal with that? Can we just bump minimum NC supported version to 30? Like in other modules: Collectives, Forms, etc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weed need to branch-off, which of course is possible. We were aiming for September originally. Related changes (version bump and housekeeping) would happen in a separate PR. I'll get back about it.

use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\Comments\ICommentsManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;

class Provider implements IProvider {
protected ?IL10N $l = null;

public function __construct(
protected IFactory $languageFactory,
protected IURLGenerator $url,
protected ICommentsManager $commentsManager,
protected IUserManager $userManager,
protected IManager $activityManager,
) {
}

/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws UnknownActivityException

Check failure on line 37 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:37:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/200)
*/
public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
if ($event->getApp() !== Application::APP_ID) {
throw new UnknownActivityException();

Check failure on line 41 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:41:14: UndefinedClass: Class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/019)
}

$this->l = $this->languageFactory->get(Application::APP_ID, $language);

if ($event->getSubject() === ActivityConstants::SUBJECT_IMPORT_FINISHED) {
// $event->setParsedMessage($comment->getMessage())
// ->setRichMessage($message, $mentions);

$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));

if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (UnknownActivityException) {

Check failure on line 55 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:55:14: UndefinedClass: Class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/019)
// Ignore and simply use the long version...
}
}

return $this->parseLongVersion($event);
}

throw new UnknownActivityException();

Check failure on line 63 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:63:13: UndefinedClass: Class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/019)
}

/**
* @throws UnknownActivityException

Check failure on line 67 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:67:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/200)
*/
protected function parseShortVersion(IEvent $event): IEvent {
$subjectParameters = $this->getSubjectParameters($event);

if ($event->getSubject() === ActivityConstants::SUBJECT_IMPORT_FINISHED) {
$event->setRichSubject($this->l->t('You commented'), []);
} else {
throw new UnknownActivityException();

Check failure on line 75 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:75:14: UndefinedClass: Class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/019)
}

return $event;
}

/**
* @throws UnknownActivityException

Check failure on line 82 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:82:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/200)
*/
protected function parseLongVersion(IEvent $event): IEvent {
$subjectParameters = $this->getSubjectParameters($event);

if ($event->getSubject() === ActivityConstants::SUBJECT_IMPORT_FINISHED) {
$event->setParsedSubject($this->l->t('You commented on %1$s', [
$subjectParameters['filePath'],
]))
->setRichSubject($this->l->t('You commented on {file}'), [
'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
]);
} else {
throw new UnknownActivityException();

Check failure on line 95 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:95:14: UndefinedClass: Class, interface or enum named OCP\Activity\Exceptions\UnknownActivityException does not exist (see https://psalm.dev/019)
}

return $event;
}

protected function getSubjectParameters(IEvent $event): array {
$subjectParameters = $event->getSubjectParameters();
if (isset($subjectParameters['fileId'])) {
return $subjectParameters;
}

return [
'actor' => $subjectParameters[0],
'fileId' => $event->getObjectId(),
'filePath' => trim($subjectParameters[1], '/'),
];
}

/**
* @return array<string, string>
*/
protected function generateFileParameter(int $id, string $path): array {
return [
'type' => 'file',
'id' => (string)$id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
}
54 changes: 54 additions & 0 deletions lib/Activity/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Tables\Activity;

use OCA\Tables\AppInfo\Application;
use OCP\Activity\ActivitySettings;
use OCP\IL10N;

class Setting extends ActivitySettings {
public function __construct(
protected IL10N $l,
) {
}

public function getIdentifier(): string {
return ActivityConstants::TYPE_IMPORT_FINISHED;
}

public function getName(): string {
return $this->l->t('<strong>Import</strong> of a file has finished');
}

public function getGroupIdentifier() {
return Application::APP_ID;
}

public function getGroupName() {
return $this->l->t('Tables');
}

public function getPriority(): int {
return 50;
}

public function canChangeStream(): bool {
return true;
}

public function isDefaultEnabledStream(): bool {
return true;
}

public function canChangeMail(): bool {
return true;
}

public function isDefaultEnabledMail(): bool {
return false;
}
}
Loading
Loading