mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 22:54:42 -04:00
feat: handle note deletion and permission change via event system
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
77615f0878
commit
865c70b942
4 changed files with 27 additions and 15 deletions
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
import { Optional } from '@mrdrogdrog/optional';
|
import { Optional } from '@mrdrogdrog/optional';
|
||||||
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
||||||
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ import {
|
||||||
ForbiddenIdError,
|
ForbiddenIdError,
|
||||||
NotInDBError,
|
NotInDBError,
|
||||||
} from '../errors/errors';
|
} from '../errors/errors';
|
||||||
|
import { NoteEvent } from '../events';
|
||||||
import { Group } from '../groups/group.entity';
|
import { Group } from '../groups/group.entity';
|
||||||
import { GroupsService } from '../groups/groups.service';
|
import { GroupsService } from '../groups/groups.service';
|
||||||
import { HistoryEntry } from '../history/history-entry.entity';
|
import { HistoryEntry } from '../history/history-entry.entity';
|
||||||
|
@ -51,6 +53,7 @@ export class NotesService {
|
||||||
@Inject(forwardRef(() => AliasService)) private aliasService: AliasService,
|
@Inject(forwardRef(() => AliasService)) private aliasService: AliasService,
|
||||||
private realtimeNoteService: RealtimeNoteService,
|
private realtimeNoteService: RealtimeNoteService,
|
||||||
private realtimeNoteStore: RealtimeNoteStore,
|
private realtimeNoteStore: RealtimeNoteStore,
|
||||||
|
private eventEmitter: EventEmitter2,
|
||||||
) {
|
) {
|
||||||
this.logger.setContext(NotesService.name);
|
this.logger.setContext(NotesService.name);
|
||||||
}
|
}
|
||||||
|
@ -260,10 +263,7 @@ export class NotesService {
|
||||||
* @throws {NotInDBError} there is no note with this id or alias
|
* @throws {NotInDBError} there is no note with this id or alias
|
||||||
*/
|
*/
|
||||||
async deleteNote(note: Note): Promise<Note> {
|
async deleteNote(note: Note): Promise<Note> {
|
||||||
const realtimeNote = this.realtimeNoteStore.find(note.id);
|
this.eventEmitter.emit(NoteEvent.DELETION, note.id);
|
||||||
if (realtimeNote) {
|
|
||||||
realtimeNote.announceNoteDeletion();
|
|
||||||
}
|
|
||||||
return await this.noteRepository.remove(note);
|
return await this.noteRepository.remove(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,12 @@
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
import { forwardRef, Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import { GroupsModule } from '../groups/groups.module';
|
import { GroupsModule } from '../groups/groups.module';
|
||||||
import { LoggerModule } from '../logger/logger.module';
|
import { LoggerModule } from '../logger/logger.module';
|
||||||
import { Note } from '../notes/note.entity';
|
import { Note } from '../notes/note.entity';
|
||||||
import { RealtimeNoteModule } from '../realtime/realtime-note/realtime-note.module';
|
|
||||||
import { UsersModule } from '../users/users.module';
|
import { UsersModule } from '../users/users.module';
|
||||||
import { PermissionsService } from './permissions.service';
|
import { PermissionsService } from './permissions.service';
|
||||||
|
|
||||||
|
@ -19,7 +18,6 @@ import { PermissionsService } from './permissions.service';
|
||||||
UsersModule,
|
UsersModule,
|
||||||
GroupsModule,
|
GroupsModule,
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
forwardRef(() => RealtimeNoteModule),
|
|
||||||
],
|
],
|
||||||
exports: [PermissionsService],
|
exports: [PermissionsService],
|
||||||
providers: [PermissionsService],
|
providers: [PermissionsService],
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
|
@ -13,14 +14,13 @@ import {
|
||||||
} from '../config/guest_access.enum';
|
} from '../config/guest_access.enum';
|
||||||
import noteConfiguration, { NoteConfig } from '../config/note.config';
|
import noteConfiguration, { NoteConfig } from '../config/note.config';
|
||||||
import { PermissionsUpdateInconsistentError } from '../errors/errors';
|
import { PermissionsUpdateInconsistentError } from '../errors/errors';
|
||||||
|
import { NoteEvent } from '../events';
|
||||||
import { Group } from '../groups/group.entity';
|
import { Group } from '../groups/group.entity';
|
||||||
import { GroupsService } from '../groups/groups.service';
|
import { GroupsService } from '../groups/groups.service';
|
||||||
import { SpecialGroup } from '../groups/groups.special';
|
import { SpecialGroup } from '../groups/groups.special';
|
||||||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||||
import { NotePermissionsUpdateDto } from '../notes/note-permissions.dto';
|
import { NotePermissionsUpdateDto } from '../notes/note-permissions.dto';
|
||||||
import { Note } from '../notes/note.entity';
|
import { Note } from '../notes/note.entity';
|
||||||
import { RealtimeNoteStore } from '../realtime/realtime-note/realtime-note-store';
|
|
||||||
import { RealtimeNoteService } from '../realtime/realtime-note/realtime-note.service';
|
|
||||||
import { User } from '../users/user.entity';
|
import { User } from '../users/user.entity';
|
||||||
import { UsersService } from '../users/users.service';
|
import { UsersService } from '../users/users.service';
|
||||||
import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck';
|
import { checkArrayForDuplicates } from '../utils/arrayDuplicatCheck';
|
||||||
|
@ -36,8 +36,7 @@ export class PermissionsService {
|
||||||
private readonly logger: ConsoleLoggerService,
|
private readonly logger: ConsoleLoggerService,
|
||||||
@Inject(noteConfiguration.KEY)
|
@Inject(noteConfiguration.KEY)
|
||||||
private noteConfig: NoteConfig,
|
private noteConfig: NoteConfig,
|
||||||
private realtimeNoteService: RealtimeNoteService,
|
private eventEmitter: EventEmitter2,
|
||||||
private realtimeNoteStore: RealtimeNoteStore,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -155,10 +154,7 @@ export class PermissionsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private notifyOthers(noteId: Note['id']): void {
|
private notifyOthers(noteId: Note['id']): void {
|
||||||
const realtimeNote = this.realtimeNoteStore.find(noteId);
|
this.eventEmitter.emit(NoteEvent.PERMISSION_CHANGE, noteId);
|
||||||
if (realtimeNote) {
|
|
||||||
realtimeNote.announcePermissionChange();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,9 +5,11 @@
|
||||||
*/
|
*/
|
||||||
import { Optional } from '@mrdrogdrog/optional';
|
import { Optional } from '@mrdrogdrog/optional';
|
||||||
import { BeforeApplicationShutdown, Inject, Injectable } from '@nestjs/common';
|
import { BeforeApplicationShutdown, Inject, Injectable } from '@nestjs/common';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
import { SchedulerRegistry } from '@nestjs/schedule';
|
import { SchedulerRegistry } from '@nestjs/schedule';
|
||||||
|
|
||||||
import appConfiguration, { AppConfig } from '../../config/app.config';
|
import appConfiguration, { AppConfig } from '../../config/app.config';
|
||||||
|
import { NoteEvent } from '../../events';
|
||||||
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
||||||
import { Note } from '../../notes/note.entity';
|
import { Note } from '../../notes/note.entity';
|
||||||
import { RevisionsService } from '../../revisions/revisions.service';
|
import { RevisionsService } from '../../revisions/revisions.service';
|
||||||
|
@ -101,4 +103,20 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnEvent(NoteEvent.PERMISSION_CHANGE)
|
||||||
|
public handleNotePermissionChanged(noteId: Note['id']): void {
|
||||||
|
const realtimeNote = this.realtimeNoteStore.find(noteId);
|
||||||
|
if (realtimeNote) {
|
||||||
|
realtimeNote.announcePermissionChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnEvent(NoteEvent.DELETION)
|
||||||
|
public handleNoteDeleted(noteId: Note['id']): void {
|
||||||
|
const realtimeNote = this.realtimeNoteStore.find(noteId);
|
||||||
|
if (realtimeNote) {
|
||||||
|
realtimeNote.announceNoteDeletion();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue