feat: add realtime announcements for permission changes and note deletion

Co-authored-by: Erik Michelson <github@erik.michelson.eu>
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-09-04 23:38:26 +02:00
parent c363d0834e
commit 331747f61b
6 changed files with 56 additions and 3 deletions

View file

@ -47,7 +47,7 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
/**
* Creates or reuses a {@link RealtimeNote} that is handling the real time editing of the {@link Note} which is identified by the given note id.
* @param note The for which a {@link RealtimeNote realtime note} should be retrieved.
* @param note The {@link Note} for which a {@link RealtimeNote realtime note} should be retrieved.
* @throws NotInDBError if note doesn't exist or has no revisions.
* @return A {@link RealtimeNote} that is linked to the given note.
*/

View file

@ -3,6 +3,10 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
encodeDocumentDeletedMessage,
encodeMetadataUpdatedMessage,
} from '@hedgedoc/realtime';
import { Logger } from '@nestjs/common';
import { EventEmitter } from 'events';
import TypedEventEmitter, { EventMap } from 'typed-emitter';
@ -130,4 +134,29 @@ export class RealtimeNote extends (EventEmitter as TypedEventEmitterConstructor<
public getNote(): Note {
return this.note;
}
/**
* Announce to all clients that the permissions of the note have been changed.
*/
public announcePermissionChange(): void {
this.sendToAllClients(encodeMetadataUpdatedMessage());
}
/**
* Announce to all clients that the note has been deleted.
*/
public announceNoteDeletion(): void {
this.sendToAllClients(encodeDocumentDeletedMessage());
}
/**
* Broadcasts the given content to all connected clients.
*
* @param {Uint8Array} content The binary message to broadcast
*/
private sendToAllClients(content: Uint8Array): void {
this.getConnections().forEach((connection) => {
connection.send(content);
});
}
}