From f95f5406bedf0423ee60b0bc41f0d2dd00c67639 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Sun, 11 Jun 2023 12:57:56 +0200 Subject: [PATCH] fix: make other services use the revision create method Signed-off-by: Tilman Vatteroth --- backend/src/history/history.module.ts | 2 ++ backend/src/history/history.service.ts | 8 +++-- backend/src/notes/notes.service.ts | 43 ++++++++++++-------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/backend/src/history/history.module.ts b/backend/src/history/history.module.ts index a40927eb5..f1693b49d 100644 --- a/backend/src/history/history.module.ts +++ b/backend/src/history/history.module.ts @@ -9,6 +9,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { LoggerModule } from '../logger/logger.module'; import { NotesModule } from '../notes/notes.module'; +import { RevisionsModule } from '../revisions/revisions.module'; import { UsersModule } from '../users/users.module'; import { HistoryEntry } from './history-entry.entity'; import { HistoryService } from './history.service'; @@ -22,6 +23,7 @@ import { HistoryService } from './history.service'; UsersModule, NotesModule, ConfigModule, + RevisionsModule, ], }) export class HistoryModule {} diff --git a/backend/src/history/history.service.ts b/backend/src/history/history.service.ts index 2a3e9377f..57928333c 100644 --- a/backend/src/history/history.service.ts +++ b/backend/src/history/history.service.ts @@ -11,6 +11,7 @@ import { NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { Note } from '../notes/note.entity'; import { NotesService } from '../notes/notes.service'; +import { RevisionsService } from '../revisions/revisions.service'; import { User } from '../users/user.entity'; import { UsersService } from '../users/users.service'; import { HistoryEntryImportDto } from './history-entry-import.dto'; @@ -29,6 +30,7 @@ export class HistoryService { private historyEntryRepository: Repository, private usersService: UsersService, private notesService: NotesService, + private revisionsService: RevisionsService, ) { this.logger.setContext(HistoryService.name); } @@ -177,11 +179,13 @@ export class HistoryService { * @return {HistoryEntryDto} the built HistoryEntryDto */ async toHistoryEntryDto(entry: HistoryEntry): Promise { + const note = await entry.note; + const revision = await this.revisionsService.getLatestRevision(note); return { identifier: await getIdentifier(entry), lastVisitedAt: entry.updatedAt, - tags: await this.notesService.toTagList(await entry.note), - title: (await entry.note).title ?? '', + tags: (await revision.tags).map((tag) => tag.name), + title: revision.title ?? '', pinStatus: entry.pinStatus, }; } diff --git a/backend/src/notes/notes.service.ts b/backend/src/notes/notes.service.ts index c1febee44..111888589 100644 --- a/backend/src/notes/notes.service.ts +++ b/backend/src/notes/notes.service.ts @@ -25,7 +25,6 @@ import { ConsoleLoggerService } from '../logger/console-logger.service'; import { NoteGroupPermission } from '../permissions/note-group-permission.entity'; import { RealtimeNoteStore } from '../realtime/realtime-note/realtime-note-store'; import { RealtimeNoteService } from '../realtime/realtime-note/realtime-note.service'; -import { Revision } from '../revisions/revision.entity'; import { RevisionsService } from '../revisions/revisions.service'; import { User } from '../users/user.entity'; import { UsersService } from '../users/users.service'; @@ -102,10 +101,13 @@ export class NotesService { // We cast to a note early to keep the later code clean const newNote = Note.create(owner, alias) as Note; - newNote.revisions = Promise.resolve([ - //TODO: Calculate patch - Revision.create(noteContent, noteContent, newNote) as Revision, - ]); + const newRevision = await this.revisionsService.createRevision( + newNote, + noteContent, + ); + newNote.revisions = Promise.resolve( + newRevision === undefined ? [] : [newRevision], + ); let everyoneAccessLevel; @@ -218,7 +220,6 @@ export class NotesService { .leftJoinAndSelect('group_permission.group', 'group') .leftJoinAndSelect('note.userPermissions', 'user_permission') .leftJoinAndSelect('user_permission.user', 'user') - .leftJoinAndSelect('note.tags', 'tag') .where('note.publicId = :noteIdOrAlias') .orWhere((queryBuilder) => { const subQuery = queryBuilder @@ -300,9 +301,14 @@ export class NotesService { */ async updateNote(note: Note, noteContent: string): Promise { const revisions = await note.revisions; - //TODO: Calculate patch - revisions.push(Revision.create(noteContent, noteContent, note) as Revision); - note.revisions = Promise.resolve(revisions); + const newRevision = await this.revisionsService.createRevision( + note, + noteContent, + ); + if (newRevision !== undefined) { + revisions.push(newRevision); + note.revisions = Promise.resolve(revisions); + } return await this.noteRepository.save(note); } @@ -328,15 +334,6 @@ export class NotesService { return await note.owner; } - /** - * Map the tags of a note to a string array of the tags names. - * @param {Note} note - the note to use - * @return {string[]} string array of tags names - */ - async toTagList(note: Note): Promise { - return (await note.tags).map((tag) => tag.name); - } - /** * Build NotePermissionsDto from a note. * @param {Note} note - the note to use @@ -371,6 +368,7 @@ export class NotesService { */ async toNoteMetadataDto(note: Note): Promise { const updateUser = await this.calculateUpdateUser(note); + const latestRevision = await this.revisionsService.getLatestRevision(note); return { id: note.publicId, aliases: await Promise.all( @@ -379,15 +377,14 @@ export class NotesService { ).map((alias) => this.aliasService.toAliasDto(alias, note)), ), primaryAddress: (await getPrimaryAlias(note)) ?? note.publicId, - title: note.title ?? '', + title: latestRevision.title, + description: latestRevision.description, + tags: (await latestRevision.tags).map((tag) => tag.name), createdAt: note.createdAt, - description: note.description ?? '', editedBy: (await this.getAuthorUsers(note)).map((user) => user.username), permissions: await this.toNotePermissionsDto(note), - tags: await this.toTagList(note), version: note.version, - updatedAt: (await this.revisionsService.getLatestRevision(note)) - .createdAt, + updatedAt: latestRevision.createdAt, updateUsername: updateUser ? updateUser.username : null, viewCount: note.viewCount, };