refactor(edit): lazy-load relations

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-12-05 19:58:01 +01:00
parent 7f7886c5a7
commit 4e70044a2c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
5 changed files with 26 additions and 20 deletions

View file

@ -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';

View file

@ -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;
}

View file

@ -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);

View file

@ -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(
return await (
await lastRevision.edits.sort(
(a, b) => b.updatedAt.getTime() - a.updatedAt.getTime(),
)[0].author.user;
)[0].author
).user;
}
// If there are no Edits, the owner is the updateUser
return await note.owner;

View file

@ -28,13 +28,13 @@ export class Edit {
* Revisions this edit appears in
*/
@ManyToMany((_) => Revision, (revision) => revision.edits)
revisions: Revision[];
revisions: Promise<Revision[]>;
/**
* Author that created the change
*/
@ManyToOne(() => Author, (author) => author.edits)
author: Author;
author: Promise<Author>;
@Column()
startPos: number;
@ -57,8 +57,8 @@ export class Edit {
endPos: number,
): Omit<Edit, 'id' | 'createdAt' | 'updatedAt'> {
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;