From 794be4a5dc10783ce84fabe12718231da0bc60e4 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Thu, 16 Sep 2021 23:53:29 +0200 Subject: [PATCH] chore: create getIdentifier utility function Signed-off-by: Philip Molares --- src/history/history.service.ts | 25 +++++++++++------------ src/history/utils.spec.ts | 36 ++++++++++++++++++++++++++++++++++ src/history/utils.ts | 18 +++++++++++++++++ 3 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 src/history/utils.spec.ts create mode 100644 src/history/utils.ts diff --git a/src/history/history.service.ts b/src/history/history.service.ts index f61b8e93b..6e6832d65 100644 --- a/src/history/history.service.ts +++ b/src/history/history.service.ts @@ -17,6 +17,7 @@ import { HistoryEntryImportDto } from './history-entry-import.dto'; import { HistoryEntryUpdateDto } from './history-entry-update.dto'; import { HistoryEntryDto } from './history-entry.dto'; import { HistoryEntry } from './history-entry.entity'; +import { getIdentifier } from './utils'; @Injectable() export class HistoryService { @@ -41,7 +42,7 @@ export class HistoryService { async getEntriesByUser(user: User): Promise { return await this.historyEntryRepository.find({ where: { user: user }, - relations: ['note', 'user'], + relations: ['note', 'note.aliases', 'user'], }); } @@ -58,7 +59,7 @@ export class HistoryService { note: note, user: user, }, - relations: ['note', 'user'], + relations: ['note', 'note.aliases', 'user'], }); if (!entry) { throw new NotInDBError( @@ -150,23 +151,19 @@ export class HistoryService { await this.connection.transaction(async (manager) => { const currentHistory = await manager.find(HistoryEntry, { where: { user: user }, - relations: ['note', 'user'], + relations: ['note', 'note.aliases', 'user'], }); for (const entry of currentHistory) { await manager.remove(entry); } for (const historyEntry of history) { this.notesService.checkNoteIdOrAlias(historyEntry.note); - const note = await manager.findOne(Note, { - where: [ - { - id: historyEntry.note, - }, - { - alias: historyEntry.note, - }, - ], - }); + const note = await manager + .createQueryBuilder(Note, 'note') + .innerJoin('note.aliases', 'alias') + .where('note.id = :id', { id: historyEntry.note }) + .orWhere('alias.name = :id', { id: historyEntry.note }) + .getOne(); if (note === undefined) { this.logger.debug( `Could not find note '${historyEntry.note}'`, @@ -191,7 +188,7 @@ export class HistoryService { */ toHistoryEntryDto(entry: HistoryEntry): HistoryEntryDto { return { - identifier: entry.note.alias ? entry.note.alias : entry.note.id, + identifier: getIdentifier(entry), lastVisited: entry.updatedAt, tags: this.notesService.toTagList(entry.note), title: entry.note.title ?? '', diff --git a/src/history/utils.spec.ts b/src/history/utils.spec.ts new file mode 100644 index 000000000..d6465c27d --- /dev/null +++ b/src/history/utils.spec.ts @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { Alias } from '../notes/alias.entity'; +import { Note } from '../notes/note.entity'; +import { User } from '../users/user.entity'; +import { HistoryEntry } from './history-entry.entity'; +import { getIdentifier } from './utils'; + +describe('getIdentifier', () => { + const alias = 'alias'; + let note: Note; + let entry: HistoryEntry; + beforeEach(() => { + const user = User.create('hardcoded', 'Testy') as User; + note = Note.create(user, alias); + entry = HistoryEntry.create(user, note); + }); + it('returns the publicId if there are no aliases', () => { + note.aliases = undefined as unknown as Alias[]; + expect(getIdentifier(entry)).toEqual(note.publicId); + }); + it('returns the publicId, if the alias array is empty', () => { + note.aliases = []; + expect(getIdentifier(entry)).toEqual(note.publicId); + }); + it('returns the publicId, if the only alias is not primary', () => { + note.aliases[0].primary = false; + expect(getIdentifier(entry)).toEqual(note.publicId); + }); + it('returns the primary alias, if one exists', () => { + expect(getIdentifier(entry)).toEqual(note.aliases[0].name); + }); +}); diff --git a/src/history/utils.ts b/src/history/utils.ts new file mode 100644 index 000000000..ea49cee63 --- /dev/null +++ b/src/history/utils.ts @@ -0,0 +1,18 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { getPrimaryAlias } from '../notes/utils'; +import { HistoryEntry } from './history-entry.entity'; + +export function getIdentifier(entry: HistoryEntry): string { + if (!entry.note.aliases || entry.note.aliases.length === 0) { + return entry.note.publicId; + } + const primaryAlias = getPrimaryAlias(entry.note); + if (primaryAlias === undefined) { + return entry.note.publicId; + } + return primaryAlias; +}