From 4e70044a2cf10c404b3d4f40afbfc2174ac55ac4 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 5 Dec 2021 19:58:01 +0100 Subject: [PATCH] refactor(edit): lazy-load relations Signed-off-by: David Mehren --- src/api/private/notes/notes.controller.ts | 8 ++++++-- src/api/utils/permissions.guard.ts | 6 +++--- src/notes/notes.service.spec.ts | 16 ++++++++-------- src/notes/notes.service.ts | 8 +++++--- src/revisions/edit.entity.ts | 8 ++++---- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/api/private/notes/notes.controller.ts b/src/api/private/notes/notes.controller.ts index fd02e839b..1c9997530 100644 --- a/src/api/private/notes/notes.controller.ts +++ b/src/api/private/notes/notes.controller.ts @@ -14,10 +14,14 @@ import { Param, Post, UseGuards, - UseInterceptors + UseInterceptors, } from '@nestjs/common'; -import { AlreadyInDBError, ForbiddenIdError, NotInDBError } from '../../../errors/errors'; +import { + AlreadyInDBError, + ForbiddenIdError, + NotInDBError, +} from '../../../errors/errors'; import { HistoryService } from '../../../history/history.service'; import { SessionGuard } from '../../../identity/session.guard'; import { ConsoleLoggerService } from '../../../logger/console-logger.service'; diff --git a/src/api/utils/permissions.guard.ts b/src/api/utils/permissions.guard.ts index bdbe95ee5..96116db00 100644 --- a/src/api/utils/permissions.guard.ts +++ b/src/api/utils/permissions.guard.ts @@ -55,11 +55,11 @@ export class PermissionsGuard implements CanActivate { const note = await getNote(this.noteService, noteIdOrAlias); switch (permissions[0]) { case Permission.READ: - return this.permissionsService.mayRead(user, note); + return await this.permissionsService.mayRead(user, note); case Permission.WRITE: - return this.permissionsService.mayWrite(user, note); + return await this.permissionsService.mayWrite(user, note); case Permission.OWNER: - return this.permissionsService.isOwner(user, note); + return await this.permissionsService.isOwner(user, note); } return false; } diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index 0e38c41e7..741bdb5f4 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -713,18 +713,18 @@ describe('NotesService', () => { const revisions = await note.revisions; revisions[0].edits = [ { - revisions: revisions, + revisions: Promise.resolve(revisions), startPos: 0, endPos: 1, updatedAt: new Date(1549312452000), - author: author, + author: Promise.resolve(author), } as Edit, { - revisions: revisions, + revisions: Promise.resolve(revisions), startPos: 0, endPos: 1, updatedAt: new Date(1549312452001), - author: author, + author: Promise.resolve(author), } as Edit, ]; revisions[0].createdAt = new Date(1549312452000); @@ -812,18 +812,18 @@ describe('NotesService', () => { const revisions = await note.revisions; revisions[0].edits = [ { - revisions: revisions, + revisions: Promise.resolve(revisions), startPos: 0, endPos: 1, updatedAt: new Date(1549312452000), - author: author, + author: Promise.resolve(author), } as Edit, { - revisions: revisions, + revisions: Promise.resolve(revisions), startPos: 0, endPos: 1, updatedAt: new Date(1549312452001), - author: author, + author: Promise.resolve(author), } as Edit, ]; revisions[0].createdAt = new Date(1549312452000); diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index 51a24d07e..16d98e5cb 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -343,9 +343,11 @@ export class NotesService { if (lastRevision && lastRevision.edits) { // Sort the last Revisions Edits by their updatedAt Date to get the latest one // the user of that Edit is the updateUser - return await lastRevision.edits.sort( - (a, b) => b.updatedAt.getTime() - a.updatedAt.getTime(), - )[0].author.user; + return await ( + await lastRevision.edits.sort( + (a, b) => b.updatedAt.getTime() - a.updatedAt.getTime(), + )[0].author + ).user; } // If there are no Edits, the owner is the updateUser return await note.owner; diff --git a/src/revisions/edit.entity.ts b/src/revisions/edit.entity.ts index ac2a188fe..349e6d54f 100644 --- a/src/revisions/edit.entity.ts +++ b/src/revisions/edit.entity.ts @@ -28,13 +28,13 @@ export class Edit { * Revisions this edit appears in */ @ManyToMany((_) => Revision, (revision) => revision.edits) - revisions: Revision[]; + revisions: Promise; /** * Author that created the change */ @ManyToOne(() => Author, (author) => author.edits) - author: Author; + author: Promise; @Column() startPos: number; @@ -57,8 +57,8 @@ export class Edit { endPos: number, ): Omit { const newEdit = new Edit(); - newEdit.revisions = []; - newEdit.author = author; + newEdit.revisions = Promise.resolve([]); + newEdit.author = Promise.resolve(author); newEdit.startPos = startPos; newEdit.endPos = endPos; return newEdit;