From 6684b0f88651bd512776527aa2ee1622d7b42343 Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Wed, 7 Aug 2024 22:12:12 +0200 Subject: [PATCH] enhancement(realtime): send metadata update on revision save When the frontend is notified about metadata updates, it refreshes the data and therefore refreshes information like the timestamp of the last revision save in the sidebar. This commit adds such a notification from the backend to all clients on each revision save, so that the "last saved at" value in the frontend is correct. Signed-off-by: Erik Michelson --- .../src/realtime/realtime-note/realtime-note.service.ts | 7 +++++-- backend/src/realtime/realtime-note/realtime-note.spec.ts | 6 +++--- backend/src/realtime/realtime-note/realtime-note.ts | 7 ++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/src/realtime/realtime-note/realtime-note.service.ts b/backend/src/realtime/realtime-note/realtime-note.service.ts index 19ea69b8f..1a5fb9ba6 100644 --- a/backend/src/realtime/realtime-note/realtime-note.service.ts +++ b/backend/src/realtime/realtime-note/realtime-note.service.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -49,6 +49,9 @@ export class RealtimeNoteService implements BeforeApplicationShutdown { realtimeNote.getRealtimeDoc().getCurrentContent(), realtimeNote.getRealtimeDoc().encodeStateAsUpdate(), ) + .then(() => { + realtimeNote.announceMetadataUpdate(); + }) .catch((reason) => this.logger.error(reason)); } @@ -117,7 +120,7 @@ export class RealtimeNoteService implements BeforeApplicationShutdown { const realtimeNote = this.realtimeNoteStore.find(note.id); if (!realtimeNote) return; - realtimeNote.announcePermissionChange(); + realtimeNote.announceMetadataUpdate(); const allConnections = realtimeNote.getConnections(); await this.updateOrCloseConnection(allConnections, note); } diff --git a/backend/src/realtime/realtime-note/realtime-note.spec.ts b/backend/src/realtime/realtime-note/realtime-note.spec.ts index 1635d519e..809f0687f 100644 --- a/backend/src/realtime/realtime-note/realtime-note.spec.ts +++ b/backend/src/realtime/realtime-note/realtime-note.spec.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -82,11 +82,11 @@ describe('realtime note', () => { const sendMessage2Spy = jest.spyOn(client2.getTransporter(), 'sendMessage'); const metadataMessage = { type: MessageType.METADATA_UPDATED }; - sut.announcePermissionChange(); + sut.announceMetadataUpdate(); expect(sendMessage1Spy).toHaveBeenCalledWith(metadataMessage); expect(sendMessage2Spy).toHaveBeenCalledWith(metadataMessage); sut.removeClient(client2); - sut.announcePermissionChange(); + sut.announceMetadataUpdate(); expect(sendMessage1Spy).toHaveBeenCalledTimes(2); expect(sendMessage2Spy).toHaveBeenCalledTimes(1); }); diff --git a/backend/src/realtime/realtime-note/realtime-note.ts b/backend/src/realtime/realtime-note/realtime-note.ts index c55a20415..ed8399549 100644 --- a/backend/src/realtime/realtime-note/realtime-note.ts +++ b/backend/src/realtime/realtime-note/realtime-note.ts @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -149,9 +149,10 @@ export class RealtimeNote extends EventEmitter2 { } /** - * Announce to all clients that the permissions of the note have been changed. + * Announce to all clients that the metadata of the note have been changed. + * This could for example be a permission change or a revision being saved. */ - public announcePermissionChange(): void { + public announceMetadataUpdate(): void { this.sendToAllClients({ type: MessageType.METADATA_UPDATED }); }