Skip to content
Merged
25 changes: 13 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

namespace OCA\Notes\AppInfo;

use OCA\Notes\Listener\NoteFileEventsListener;
use OCA\Notes\Reference\NoteReferenceProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Share\Events\BeforeShareCreatedEvent;

/** @phan-suppress-next-line PhanUnreferencedUseNormal */
Expand All @@ -30,21 +35,17 @@ public function __construct(array $urlParams = []) {
public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerSearchProvider(SearchProvider::class);
$context->registerDashboardWidget(DashboardWidget::class);
$context->registerEventListener(
BeforeTemplateRenderedEvent::class,
BeforeTemplateRenderedListener::class
);
$context->registerEventListener(
BeforeShareCreatedEvent::class,
BeforeShareCreatedListener::class
);
$context->registerReferenceProvider(NoteReferenceProvider::class);
$context->registerDashboardWidget(DashboardWidget::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(BeforeShareCreatedEvent::class, BeforeShareCreatedListener::class);
$context->registerEventListener(BeforeNodeWrittenEvent::class, NoteFileEventsListener::class);
$context->registerEventListener(BeforeNodeTouchedEvent::class, NoteFileEventsListener::class);
$context->registerEventListener(BeforeNodeDeletedEvent::class, NoteFileEventsListener::class);
$context->registerEventListener(BeforeNodeRenamedEvent::class, NoteFileEventsListener::class);
}

public function boot(IBootContext $context): void {
$context->injectFn(function (NotesHooks $notesHooks) {
$notesHooks->register();
});
// Intentionally empty
}
}
85 changes: 0 additions & 85 deletions lib/AppInfo/NotesHooks.php

This file was deleted.

49 changes: 49 additions & 0 deletions lib/Listener/NoteFileEventsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

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

namespace OCA\Notes\Listener;

use OCA\Notes\Service\MetaService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Node;

/** @template-implements IEventListener<BeforeNodeWrittenEvent|BeforeNodeTouchedEvent|BeforeNodeDeletedEvent|BeforeNodeRenamedEvent|Event> */
class NoteFileEventsListener implements IEventListener {
public function __construct(
private MetaService $metaService,
) {
}

public function handle(Event $event): void {
if ($event instanceof BeforeNodeWrittenEvent) {
$this->onFileModified($event->getNode());
} elseif ($event instanceof BeforeNodeTouchedEvent) {
$this->onFileModified($event->getNode());
} elseif ($event instanceof BeforeNodeDeletedEvent) {
$this->onFileModified($event->getNode());
} elseif ($event instanceof BeforeNodeRenamedEvent) {
$this->onFileModified($event->getSource());
}
}

private function onFileModified(Node $node): void {
try {
$this->metaService->deleteByNote($node->getId());
} catch (\Throwable $e) {
// Intentionally non-fatal: MetaService::getAll() will reconcile on next sync.
// Consider: Log at debug level so persistent failures remain diagnosable.
// (logger would need to be injected if added)
}
}
}
16 changes: 8 additions & 8 deletions lib/Service/MetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
* Therefore, the Notes app maintains this information on its own. It is saved
* in the database table `notes_meta`. To be honest, we do not store the exact
* changed time, but a time `t` that is at some point between the real changed
* time and the next synchronization time. However, this is totally sufficient
* time and next synchronization time. However, this is totally sufficient
* for this purpose.
*
* Therefore, on synchronization, the method `MetaService.getAll` is called.
* It generates an ETag for each note and compares it with the ETag from
* Therefore, on synchronization, the method `MetaService::getAll` is called.
* It generates an ETag for each note and compares it with the ETag from the
* `notes_meta` database table in order to detect changes (or creates an entry
* if not existent). If there are changes, the ETag is updated and `LastUpdate`
* is set to the current time. The ETag is a hash over all note attributes
* (except content, see below).
*
* But in order to further speed up synchronization, the content is not
* compared every time (this would be very expensive!). Instead, a file hook
* (see `OCA\Notes\NotesHook`) deletes the meta entry on every file change. As
* a consequence, a new entry in `note_meta` is created on next
* compared every time (this would be very expensive!). Instead, a file event
* listener invalidates the meta entry on every relevant file change. As a
* consequence, a new entry in `notes_meta` is created on the next
* synchronization.
*
* Hence, instead of using the real content for generating the note's ETag, it
* uses a "content ETag" which is a hash over the content. Additionaly to the
* file hooks, this "content ETag" is updated if Nextcloud's "file ETag" has
* uses a "content ETag" which is a hash over the content. Additionally to the
* file events, this "content ETag" is updated if Nextcloud's "file ETag" has
* changed (but again, the "file ETag" is just an indicator, since it is not a
* hash over the content).
*
Expand Down
9 changes: 0 additions & 9 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
<code><![CDATA[$this->noteUtil->getRoot()]]></code>
</MissingDependency>
</file>
<file src="lib/AppInfo/NotesHooks.php">
<MissingDependency>
<code><![CDATA[IRootFolder]]></code>
<code><![CDATA[IRootFolder]]></code>
</MissingDependency>
<MissingParamType>
<code><![CDATA[$service]]></code>
</MissingParamType>
</file>
<file src="lib/Controller/ChunkCursor.php">
<MissingConstructor>
<code><![CDATA[$noteId]]></code>
Expand Down
Loading