Skip to content
Draft
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
3 changes: 2 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In your Nextcloud instance, simply navigate to **Β»AppsΒ«**, find the
**Β»TeamsΒ«** and **Β»CollectivesΒ«** apps and enable them.

]]></description>
<version>4.2.0</version>
<version>4.2.1</version>
<licence>agpl</licence>
<author>CollectiveCloud Team</author>
<namespace>Collectives</namespace>
Expand Down Expand Up @@ -59,6 +59,7 @@ In your Nextcloud instance, simply navigate to **Β»AppsΒ«**, find the
<repair-steps>
<post-migration>
<step>OCA\Collectives\Migration\MigrateTemplates</step>
<step>OCA\Collectives\Migration\PopulatePageCollectiveId</step>
</post-migration>
<live-migration>
<step>OCA\Collectives\Migration\GenerateSlugs</step>
Expand Down
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\Collectives\Listeners\CircleDestroyedListener;
use OCA\Collectives\Listeners\CircleEditingEventListener;
use OCA\Collectives\Listeners\CollectivesReferenceListener;
use OCA\Collectives\Listeners\NodeDeletedListener;
use OCA\Collectives\Listeners\NodeRenamedListener;
use OCA\Collectives\Listeners\NodeWrittenListener;
use OCA\Collectives\Listeners\ShareDeletedListener;
Expand Down Expand Up @@ -52,6 +53,7 @@
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\IMimeTypeLoader;
Expand All @@ -75,6 +77,7 @@ public function __construct(array $urlParams = []) {
public function register(IRegistrationContext $context): void {
require_once(__DIR__ . '/../../vendor/autoload.php');
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class);
$context->registerEventListener(NodeRenamedEvent::class, NodeRenamedListener::class);
$context->registerEventListener(NodeWrittenEvent::class, NodeWrittenListener::class);
$context->registerEventListener(CircleDestroyedEvent::class, CircleDestroyedListener::class);
Expand Down
18 changes: 17 additions & 1 deletion lib/Command/GenerateSlugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
use OCA\Collectives\Fs\NodeHelper;
use OCA\Collectives\Model\PageInfo;
use OCA\Collectives\Service\CircleHelper;
use OCA\Collectives\Service\MissingDependencyException;
use OCA\Collectives\Service\NotFoundException;
use OCA\Collectives\Service\NotPermittedException;
use OCA\Collectives\Service\PageService;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -98,7 +100,7 @@ private function generatePageSlugs(): void {
while ($rowCollective = $resultCollectives->fetch()) {
try {
$circle = $this->circleHelper->getCircle($rowCollective['circle_unique_id'], null, true);
$pageInfos = $this->pageService->findAll($rowCollective['id'], $circle->getOwner()->getUserId());
$pageInfos = $this->findAllPages((int)$rowCollective['id'], $circle->getOwner()->getUserId());
} catch (NotFoundException|NotPermittedException) {
// Ignore exceptions from CircleManager (e.g. due to cruft collective without circle)
continue;
Expand All @@ -121,4 +123,18 @@ private function generatePageSlugs(): void {

$resultCollectives->closeCursor();
}

/**
* @throws MissingDependencyException
* @throws NotFoundException
* @throws NotPermittedException
*/
private function findAllPages(int $collectiveId, string $userId): array {
$folder = $this->pageService->getCollectiveFolder($collectiveId, $userId);
try {
return $this->pageService->getPagesFromFolder($collectiveId, $folder, $userId, true, true);
} catch (FilesNotFoundException $e) {
throw new NotFoundException($e->getMessage(), 0, $e);
}
}
}
68 changes: 68 additions & 0 deletions lib/Db/FileCacheMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

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

namespace OCA\Collectives\Db;

use OCA\Collectives\Model\FileInfo;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class FileCacheMapper {
private const BATCH_SIZE = 1000;

public function __construct(
private readonly IDBConnection $db,
) {
}

/**
* Query filecache for file data by file IDs
*
* @param int[] $fileIds
* @return array<int, FileInfo> Indexed by fileId
*/
public function findByFileIds(array $fileIds): array {
if (empty($fileIds)) {
return [];
}

$fileInfos = [];
foreach (array_chunk($fileIds, self::BATCH_SIZE) as $chunk) {
$qb = $this->db->getQueryBuilder();
$qb->select('fileid', 'storage', 'parent', 'path', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'etag', 'encrypted', 'permissions', 'checksum', 'unencrypted_size')
->from('filecache')
->where($qb->expr()->in('fileid', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));

$result = $qb->executeQuery();
while ($row = $result->fetch()) {
$fileInfo = new FileInfo(
fileId: (int)$row['fileid'],
storage: (int)$row['storage'],
path: (string)$row['path'],
parent: (int)$row['parent'],
name: (string)$row['name'],
mimetype: (int)$row['mimetype'],
mimepart: (int)$row['mimepart'],
size: (int)$row['size'],
mtime: (int)$row['mtime'],
storage_mtime: (int)$row['storage_mtime'],
encrypted: (int)$row['encrypted'],
unencrypted_size: (int)$row['unencrypted_size'],
etag: (string)$row['etag'],
permissions: (int)$row['permissions'],
checksum: $row['checksum'] !== null ? (string)$row['checksum'] : null,
);
$fileInfos[$fileInfo->fileId] = $fileInfo;
}
$result->closeCursor();
}

return $fileInfos;
}
}
4 changes: 4 additions & 0 deletions lib/Db/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* @method int getId()
* @method void setId(int $value)
* @method int getCollectiveId()
* @method void setCollectiveId(int $value)
* @method int getFileId()
* @method void setFileId(int $value)
* @method string getSlug()
Expand All @@ -36,6 +38,7 @@
*/
class Page extends Entity implements JsonSerializable {
protected ?int $fileId = null;
protected ?int $collectiveId = null;
protected ?string $slug = null;
protected ?string $lastUserId = null;
protected ?string $emoji = null;
Expand All @@ -51,6 +54,7 @@ public function __construct() {
public function jsonSerialize(): array {
return [
'id' => $this->id,
'collectiveId' => $this->collectiveId,
'fileId' => $this->fileId,
'slug' => $this->slug,
'lastUserId' => $this->lastUserId,
Expand Down
31 changes: 31 additions & 0 deletions lib/Db/PageLinkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\Collectives\Db;

use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class PageLinkMapper {
Expand All @@ -32,6 +33,36 @@ public function findByPageId(int $pageId): array {
return array_values($result);
}

/**
* @param int[] $pageIds
* @return array<int, array<int>> Indexed by page_id, values are arrays of linked_page_ids
* @throws Exception
*/
public function findByPageIds(array $pageIds): array {
if (empty($pageIds)) {
return [];
}

$qb = $this->db->getQueryBuilder();
$qb->select('page_id', 'linked_page_id')
->from(self::TABLE_NAME)
->where($qb->expr()->in('page_id', $qb->createNamedParameter($pageIds, IQueryBuilder::PARAM_INT_ARRAY)));

$result = $qb->executeQuery();
$linkedPagesByPageId = [];
while ($row = $result->fetch()) {
$pageId = (int)$row['page_id'];
$linkedPageId = (int)$row['linked_page_id'];
if (!isset($linkedPagesByPageId[$pageId])) {
$linkedPagesByPageId[$pageId] = [];
}
$linkedPagesByPageId[$pageId][] = $linkedPageId;
}
$result->closeCursor();

return $linkedPagesByPageId;
}

/**
* @throws Exception
*/
Expand Down
20 changes: 20 additions & 0 deletions lib/Db/PageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function findByFileIds(array $fileIds, bool $trashed = false): array {
return $pagesByFileId;
}

/**
* @return Page[]
*/
public function findByCollectiveId(int $collectiveId, bool $trashed = false): array {
$qb = $this->db->getQueryBuilder();
$andX = [
$qb->expr()->eq('collective_id', $qb->createNamedParameter($collectiveId, IQueryBuilder::PARAM_INT)),
];
// fixme: change index to use timestamp as well?
if ($trashed) {
$andX[] = $qb->expr()->isNotNull('trash_timestamp');
} else {
$andX[] = $qb->expr()->isNull('trash_timestamp');
}
$qb->select('*')
->from($this->tableName)
->where($qb->expr()->andX(...$andX));
return $this->findEntities($qb);
}

/**
* @return Page[]
*/
Expand Down
43 changes: 43 additions & 0 deletions lib/Fs/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
namespace OCA\Collectives\Fs;

use OCA\Collectives\Model\PageInfo;
use OCA\Collectives\Mount\CollectiveStorage;
use OCA\Collectives\Service\NotFoundException;
use OCA\Collectives\Service\NotPermittedException;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\Node;
use OCP\Files\NotFoundException as FilesNotFoundException;
use OCP\Files\NotPermittedException as FilesNotPermittedException;
use OCP\IDBConnection;
Expand Down Expand Up @@ -251,4 +253,45 @@ public static function folderHasSubPage(Folder $folder, string $title): int {

return 0;
}

/**
* Extract collective_id from path like "appdata_<instanceid>/collectives/<collective_id>/..."
* Only processes collective pages (not versions)
*/
public static function extractCollectiveIdFromPath(string $path): ?int {
$parts = explode('/', $path);
if (!isset($parts[0]) || !str_starts_with($parts[0], 'appdata_')) {
return null;
}
if (!isset($parts[1]) || $parts[1] !== 'collectives') {
return null;
}

$collectiveId = $parts[2] ?? null;
if ($collectiveId === 'trash') {
$collectiveId = $parts[3] ?? null;
}
if (!is_numeric($collectiveId)) {
return null;
}

return (int)$collectiveId;
}

/**
* Get collective ID from node if it is a markdown file belonging to a CollectiveStorage
*/
public static function getCollectiveIdFromNode(Node $node): ?int {
if (!($node instanceof File) || !self::isPage($node)) {
return null;
}

$storage = $node->getStorage();
if (!$storage->instanceOfStorage(CollectiveStorage::class)) {
return null;
}

/** @var CollectiveStorage $storage */
return $storage->getFolderId();
}
}
44 changes: 44 additions & 0 deletions lib/Listeners/NodeDeletedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

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

namespace OCA\Collectives\Listeners;

use OCA\Collectives\Db\PageMapper;
use OCA\Collectives\Fs\NodeHelper;
use OCA\Collectives\Service\PageService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeDeletedEvent;

/** @template-implements IEventListener<Event|NodeDeletedEvent> */
class NodeDeletedListener implements IEventListener {
public function __construct(
private PageMapper $pageMapper,
private PageService $pageService,
) {
}

public function handle(Event $event): void {
if (!($event instanceof NodeDeletedEvent)) {
return;
}

if ($this->pageService->isFromCollectives()) {
return;
}

$node = $event->getNode();
$collectiveId = NodeHelper::getCollectiveIdFromNode($node);
if ($collectiveId === null) {
return;
}

$this->pageMapper->deleteByFileId($node->getId());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mejo- should we hard delete page or just set trashed timestamp to use softdelete? I guess it's better to preserve page id in order to not break references

}
}
Loading
Loading