fix: make other services use the revision create method

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-06-11 12:57:56 +02:00
parent 7161ffd5f2
commit f95f5406be
3 changed files with 28 additions and 25 deletions

View file

@ -9,6 +9,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { LoggerModule } from '../logger/logger.module'; import { LoggerModule } from '../logger/logger.module';
import { NotesModule } from '../notes/notes.module'; import { NotesModule } from '../notes/notes.module';
import { RevisionsModule } from '../revisions/revisions.module';
import { UsersModule } from '../users/users.module'; import { UsersModule } from '../users/users.module';
import { HistoryEntry } from './history-entry.entity'; import { HistoryEntry } from './history-entry.entity';
import { HistoryService } from './history.service'; import { HistoryService } from './history.service';
@ -22,6 +23,7 @@ import { HistoryService } from './history.service';
UsersModule, UsersModule,
NotesModule, NotesModule,
ConfigModule, ConfigModule,
RevisionsModule,
], ],
}) })
export class HistoryModule {} export class HistoryModule {}

View file

@ -11,6 +11,7 @@ import { NotInDBError } from '../errors/errors';
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 { NotesService } from '../notes/notes.service'; import { NotesService } from '../notes/notes.service';
import { RevisionsService } from '../revisions/revisions.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 { HistoryEntryImportDto } from './history-entry-import.dto'; import { HistoryEntryImportDto } from './history-entry-import.dto';
@ -29,6 +30,7 @@ export class HistoryService {
private historyEntryRepository: Repository<HistoryEntry>, private historyEntryRepository: Repository<HistoryEntry>,
private usersService: UsersService, private usersService: UsersService,
private notesService: NotesService, private notesService: NotesService,
private revisionsService: RevisionsService,
) { ) {
this.logger.setContext(HistoryService.name); this.logger.setContext(HistoryService.name);
} }
@ -177,11 +179,13 @@ export class HistoryService {
* @return {HistoryEntryDto} the built HistoryEntryDto * @return {HistoryEntryDto} the built HistoryEntryDto
*/ */
async toHistoryEntryDto(entry: HistoryEntry): Promise<HistoryEntryDto> { async toHistoryEntryDto(entry: HistoryEntry): Promise<HistoryEntryDto> {
const note = await entry.note;
const revision = await this.revisionsService.getLatestRevision(note);
return { return {
identifier: await getIdentifier(entry), identifier: await getIdentifier(entry),
lastVisitedAt: entry.updatedAt, lastVisitedAt: entry.updatedAt,
tags: await this.notesService.toTagList(await entry.note), tags: (await revision.tags).map((tag) => tag.name),
title: (await entry.note).title ?? '', title: revision.title ?? '',
pinStatus: entry.pinStatus, pinStatus: entry.pinStatus,
}; };
} }

View file

@ -25,7 +25,6 @@ import { ConsoleLoggerService } from '../logger/console-logger.service';
import { NoteGroupPermission } from '../permissions/note-group-permission.entity'; import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
import { RealtimeNoteStore } from '../realtime/realtime-note/realtime-note-store'; import { RealtimeNoteStore } from '../realtime/realtime-note/realtime-note-store';
import { RealtimeNoteService } from '../realtime/realtime-note/realtime-note.service'; import { RealtimeNoteService } from '../realtime/realtime-note/realtime-note.service';
import { Revision } from '../revisions/revision.entity';
import { RevisionsService } from '../revisions/revisions.service'; import { RevisionsService } from '../revisions/revisions.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';
@ -102,10 +101,13 @@ export class NotesService {
// We cast to a note early to keep the later code clean // We cast to a note early to keep the later code clean
const newNote = Note.create(owner, alias) as Note; const newNote = Note.create(owner, alias) as Note;
newNote.revisions = Promise.resolve([ const newRevision = await this.revisionsService.createRevision(
//TODO: Calculate patch newNote,
Revision.create(noteContent, noteContent, newNote) as Revision, noteContent,
]); );
newNote.revisions = Promise.resolve(
newRevision === undefined ? [] : [newRevision],
);
let everyoneAccessLevel; let everyoneAccessLevel;
@ -218,7 +220,6 @@ export class NotesService {
.leftJoinAndSelect('group_permission.group', 'group') .leftJoinAndSelect('group_permission.group', 'group')
.leftJoinAndSelect('note.userPermissions', 'user_permission') .leftJoinAndSelect('note.userPermissions', 'user_permission')
.leftJoinAndSelect('user_permission.user', 'user') .leftJoinAndSelect('user_permission.user', 'user')
.leftJoinAndSelect('note.tags', 'tag')
.where('note.publicId = :noteIdOrAlias') .where('note.publicId = :noteIdOrAlias')
.orWhere((queryBuilder) => { .orWhere((queryBuilder) => {
const subQuery = queryBuilder const subQuery = queryBuilder
@ -300,9 +301,14 @@ export class NotesService {
*/ */
async updateNote(note: Note, noteContent: string): Promise<Note> { async updateNote(note: Note, noteContent: string): Promise<Note> {
const revisions = await note.revisions; const revisions = await note.revisions;
//TODO: Calculate patch const newRevision = await this.revisionsService.createRevision(
revisions.push(Revision.create(noteContent, noteContent, note) as Revision); note,
note.revisions = Promise.resolve(revisions); noteContent,
);
if (newRevision !== undefined) {
revisions.push(newRevision);
note.revisions = Promise.resolve(revisions);
}
return await this.noteRepository.save(note); return await this.noteRepository.save(note);
} }
@ -328,15 +334,6 @@ export class NotesService {
return await note.owner; 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<string[]> {
return (await note.tags).map((tag) => tag.name);
}
/** /**
* Build NotePermissionsDto from a note. * Build NotePermissionsDto from a note.
* @param {Note} note - the note to use * @param {Note} note - the note to use
@ -371,6 +368,7 @@ export class NotesService {
*/ */
async toNoteMetadataDto(note: Note): Promise<NoteMetadataDto> { async toNoteMetadataDto(note: Note): Promise<NoteMetadataDto> {
const updateUser = await this.calculateUpdateUser(note); const updateUser = await this.calculateUpdateUser(note);
const latestRevision = await this.revisionsService.getLatestRevision(note);
return { return {
id: note.publicId, id: note.publicId,
aliases: await Promise.all( aliases: await Promise.all(
@ -379,15 +377,14 @@ export class NotesService {
).map((alias) => this.aliasService.toAliasDto(alias, note)), ).map((alias) => this.aliasService.toAliasDto(alias, note)),
), ),
primaryAddress: (await getPrimaryAlias(note)) ?? note.publicId, 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, createdAt: note.createdAt,
description: note.description ?? '',
editedBy: (await this.getAuthorUsers(note)).map((user) => user.username), editedBy: (await this.getAuthorUsers(note)).map((user) => user.username),
permissions: await this.toNotePermissionsDto(note), permissions: await this.toNotePermissionsDto(note),
tags: await this.toTagList(note),
version: note.version, version: note.version,
updatedAt: (await this.revisionsService.getLatestRevision(note)) updatedAt: latestRevision.createdAt,
.createdAt,
updateUsername: updateUser ? updateUser.username : null, updateUsername: updateUser ? updateUser.username : null,
viewCount: note.viewCount, viewCount: note.viewCount,
}; };