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 { 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 {}

View file

@ -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<HistoryEntry>,
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<HistoryEntryDto> {
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,
};
}

View file

@ -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<Note> {
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<string[]> {
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<NoteMetadataDto> {
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,
};