fix(backend): updated realtime connection's acceptEdit, close connection based on permission

Signed-off-by: Avinash <avinash.kumar.cs92@gmail.com>
This commit is contained in:
Avinash 2023-05-11 21:16:48 +05:30 committed by Tilman Vatteroth
parent c6e283a111
commit d15a8b18d9
2 changed files with 40 additions and 12 deletions

View file

@ -191,8 +191,8 @@ export class PermissionsService {
return false;
}
private notifyOthers(noteId: Note['id']): void {
this.eventEmitter.emit(NoteEvent.PERMISSION_CHANGE, noteId);
private notifyOthers(note: Note): void {
this.eventEmitter.emit(NoteEvent.PERMISSION_CHANGE, note);
}
/**
@ -256,7 +256,7 @@ export class PermissionsService {
createdPermission.note = Promise.resolve(note);
(await note.groupPermissions).push(createdPermission);
}
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
@ -291,7 +291,7 @@ export class PermissionsService {
);
(await note.userPermissions).push(noteUserPermission);
}
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
@ -323,7 +323,7 @@ export class PermissionsService {
}
}
note.userPermissions = Promise.resolve(newPermissions);
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
@ -363,7 +363,7 @@ export class PermissionsService {
);
(await note.groupPermissions).push(noteGroupPermission);
}
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
@ -398,7 +398,7 @@ export class PermissionsService {
}
}
note.groupPermissions = Promise.resolve(newPermissions);
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
@ -411,7 +411,7 @@ export class PermissionsService {
*/
async changeOwner(note: Note, owner: User): Promise<Note> {
note.owner = Promise.resolve(owner);
this.notifyOthers(note.id);
this.notifyOthers(note);
return await this.noteRepository.save(note);
}
}

View file

@ -12,7 +12,9 @@ import appConfiguration, { AppConfig } from '../../config/app.config';
import { NoteEvent } from '../../events';
import { ConsoleLoggerService } from '../../logger/console-logger.service';
import { Note } from '../../notes/note.entity';
import { PermissionsService } from '../../permissions/permissions.service';
import { RevisionsService } from '../../revisions/revisions.service';
import { RealtimeConnection } from './realtime-connection';
import { RealtimeNote } from './realtime-note';
import { RealtimeNoteStore } from './realtime-note-store';
@ -25,6 +27,7 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
private schedulerRegistry: SchedulerRegistry,
@Inject(appConfiguration.KEY)
private appConfig: AppConfig,
private permissionService: PermissionsService,
) {}
beforeApplicationShutdown(): void {
@ -109,11 +112,36 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
}
@OnEvent(NoteEvent.PERMISSION_CHANGE)
public handleNotePermissionChanged(noteId: Note['id']): void {
const realtimeNote = this.realtimeNoteStore.find(noteId);
if (realtimeNote) {
public async handleNotePermissionChanged(note: Note): Promise<void> {
const realtimeNote = this.realtimeNoteStore.find(note.id);
if (!realtimeNote) return;
realtimeNote.announcePermissionChange();
const allConnections = realtimeNote.getConnections();
await this.updateOrCloseConnection(allConnections, note);
}
private async updateOrCloseConnection(
connections: RealtimeConnection[],
note: Note,
): Promise<void> {
for (const connection of connections) {
if (await this.connectionCanRead(connection, note)) {
connection.acceptEdits = await this.permissionService.mayWrite(
connection.getUser(),
note,
);
} else {
connection.getTransporter().disconnect();
}
}
}
private async connectionCanRead(
connection: RealtimeConnection,
note: Note,
): Promise<boolean> {
return await this.permissionService.mayRead(connection.getUser(), note);
}
@OnEvent(NoteEvent.DELETION)