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 @@ -50,6 +50,17 @@ Have a good time and manage whatever you want.
<database>sqlite</database>
<nextcloud min-version="29" max-version="32"/>
</dependencies>
<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>
<repair-steps>
<pre-migration>
<step>OCA\Tables\Migration\FixContextsDefaults</step>
Expand Down
32 changes: 32 additions & 0 deletions lib/Activity/ActivityConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

namespace OCA\Tables\Activity;

class ActivityConstants {
public const APP_ID = 'tables';

/*****
* 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';
}
38 changes: 38 additions & 0 deletions lib/Activity/ActivityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

namespace OCA\Tables\Activity;

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(ActivityConstants::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);
}
}
44 changes: 44 additions & 0 deletions lib/Activity/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

namespace OCA\Tables\Activity;

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 ActivityConstants::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(ActivityConstants::APP_ID, 'app-dark.svg'));
}

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

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

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

use OCP\Activity\Exceptions\UnknownActivityException;
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 36 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:36: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() !== ActivityConstants::APP_ID) {
throw new UnknownActivityException();

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

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

$this->l = $this->languageFactory->get(ActivityConstants::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(ActivityConstants::APP_ID, 'app-dark.svg')));

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

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:54: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 62 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:62: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 66 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:66: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 74 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:74: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 81 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedDocblockClass

lib/Activity/Provider.php:81: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 94 in lib/Activity/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/Activity/Provider.php:94: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]),
];
}
}
53 changes: 53 additions & 0 deletions lib/Activity/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

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

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 ActivityConstants::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;
}
}
55 changes: 55 additions & 0 deletions lib/BackgroundJob/ImportTableJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

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

namespace OCA\Tables\BackgroundJob;

use OCA\Tables\Activity\ActivityManager;
use OCA\Tables\Service\ImportService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\IUserManager;
use OCP\IUserSession;

class ImportTableJob extends QueuedJob {
public function __construct(
ITimeFactory $time,
private IUserManager $userManager,
private IUserSession $userSession,
private ImportService $importService,
private ActivityManager $activityManager,
) {
parent::__construct($time);
}

/**
* @param array $argument
*/
public function run($argument): void {
$userId = $argument['user_id'];
$tableId = $argument['table_id'];
$viewId = $argument['view_id'];
$path = $argument['path'];
$createMissingColumns = $argument['create_missing_columns'] ?? false;
$columnsConfig = $argument['columns_config'] ?? null;

$oldUser = $this->userSession->getUser();
try {
$user = $this->userManager->get($userId);
$this->userSession->setUser($user);

//fixme: handle errors
$importStats = $this->importService
->import($userId, $tableId, $viewId, $path, $createMissingColumns, $columnsConfig);
} catch (\Throwable $e) {
throw $e;
} finally {
$this->userSession->setUser($oldUser);
}

$this->activityManager->notifyImportFinished($userId, $tableId, $importStats);
}
}
4 changes: 2 additions & 2 deletions lib/Controller/Api1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@
public function importInTable(int $tableId, string $path, bool $createMissingColumns = true): DataResponse {
try {
// minimal permission is checked, creating columns requires MANAGE permissions - currently tested on service layer
return new DataResponse($this->importService->import($tableId, null, $path, $createMissingColumns));
return new DataResponse($this->importService->scheduleImport($tableId, null, $path, $createMissingColumns));

Check failure on line 1376 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/Controller/Api1Controller.php:1376:28: InvalidArgument: Argument 1 of OCP\AppFramework\Http\DataResponse::__construct expects array<array-key, mixed>|null|object|scalar, but void provided (see https://psalm.dev/004)

Check failure on line 1376 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

InvalidArgument

lib/Controller/Api1Controller.php:1376:28: InvalidArgument: Argument 1 of OCP\AppFramework\Http\DataResponse::__construct expects array<array-key, mixed>|null|object|scalar, but void provided (see https://psalm.dev/004)
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
Expand Down Expand Up @@ -1408,7 +1408,7 @@
public function importInView(int $viewId, string $path, bool $createMissingColumns = true): DataResponse {
try {
// minimal permission is checked, creating columns requires MANAGE permissions - currently tested on service layer
return new DataResponse($this->importService->import(null, $viewId, $path, $createMissingColumns));
return new DataResponse($this->importService->scheduleImport(null, $viewId, $path, $createMissingColumns));

Check failure on line 1411 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/Controller/Api1Controller.php:1411:28: InvalidArgument: Argument 1 of OCP\AppFramework\Http\DataResponse::__construct expects array<array-key, mixed>|null|object|scalar, but void provided (see https://psalm.dev/004)

Check failure on line 1411 in lib/Controller/Api1Controller.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

InvalidArgument

lib/Controller/Api1Controller.php:1411:28: InvalidArgument: Argument 1 of OCP\AppFramework\Http\DataResponse::__construct expects array<array-key, mixed>|null|object|scalar, but void provided (see https://psalm.dev/004)
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]);
$message = ['message' => $e->getMessage()];
Expand Down
Loading
Loading