From 300b464efd01ccd8bc9065bf30d9f9113f405c76 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Wed, 3 Feb 2021 21:15:39 +0100 Subject: [PATCH] History: Add HistoryEntry With this the backend now can hold a history entry. Also included in this commit are some minor changes to tests and services so they can still work. Signed-off-by: Philip Molares --- docs/content/dev/db-schema.plantuml | 10 ++++++ src/api/public/me/me.controller.spec.ts | 3 ++ src/auth/auth.service.spec.ts | 1 + src/history/history-entry.entity.ts | 47 +++++++++++++++++++++++++ src/notes/note.entity.ts | 3 ++ src/notes/notes.service.ts | 5 +++ src/users/user.entity.ts | 5 +-- 7 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/history/history-entry.entity.ts diff --git a/docs/content/dev/db-schema.plantuml b/docs/content/dev/db-schema.plantuml index ca8d0a3c6..b64bce37f 100644 --- a/docs/content/dev/db-schema.plantuml +++ b/docs/content/dev/db-schema.plantuml @@ -134,11 +134,20 @@ entity "media_upload" { *createdAt : date } +entity "history_entry" { + *noteId : uuid <> + *userId : uuid <> + -- + *pinStatus: boolean + *updatedAt: date +} + user "1" -- "0..*" note: owner user "1" -u- "1..*" identity user "1" - "1..*" auth_token: authTokens user "1" -l- "1..*" session user "1" - "0..*" media_upload +user "1" - "0..*" history_entry user "0..*" -- "0..*" note user "1" - "0..*" authorship @@ -149,6 +158,7 @@ revision "0..*" - "0..*" authorship media_upload "0..*" -- "1" note note "1" - "1..*" revision +note "1" - "0..*" history_entry note "0..*" -l- "0..*" tag note "0..*" -- "0..*" group diff --git a/src/api/public/me/me.controller.spec.ts b/src/api/public/me/me.controller.spec.ts index e3f262396..a71d73dcf 100644 --- a/src/api/public/me/me.controller.spec.ts +++ b/src/api/public/me/me.controller.spec.ts @@ -19,6 +19,7 @@ import { Identity } from '../../../users/identity.entity'; import { User } from '../../../users/user.entity'; import { UsersModule } from '../../../users/users.module'; import { MeController } from './me.controller'; +import { HistoryEntry } from '../../../history/history-entry.entity'; describe('Me Controller', () => { let controller: MeController; @@ -44,6 +45,8 @@ describe('Me Controller', () => { .useValue({}) .overrideProvider(getRepositoryToken(Tag)) .useValue({}) + .overrideProvider(getRepositoryToken(HistoryEntry)) + .useValue({}) .compile(); controller = module.get(MeController); diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index b5a7e2c1d..a8ca9ffcb 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -28,6 +28,7 @@ describe('AuthService', () => { id: '1', identities: [], ownedNotes: [], + historyEntries: [], updatedAt: new Date(), userName: 'Testy', }; diff --git a/src/history/history-entry.entity.ts b/src/history/history-entry.entity.ts new file mode 100644 index 000000000..597e137b4 --- /dev/null +++ b/src/history/history-entry.entity.ts @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + Column, + Entity, + ManyToOne, + UpdateDateColumn, +} from 'typeorm'; +import { User } from '../users/user.entity'; +import { Note } from '../notes/note.entity'; + +@Entity() +export class HistoryEntry { + @ManyToOne((_) => User, (user) => user.historyEntries, { + onDelete: 'CASCADE', + primary: true, + }) + user: User; + + @ManyToOne((_) => Note, (note) => note.historyEntries, { + onDelete: 'CASCADE', + primary: true, + }) + note: Note; + + @Column() + pinStatus: boolean; + + @UpdateDateColumn() + updatedAt: Date; + + // The optional note parameter is necessary for the createNote method in the NotesService, + // as we create the note then and don't need to add it to the HistoryEntry. + public static create(user: User, note?: Note): HistoryEntry { + const newHistoryEntry = new HistoryEntry(); + newHistoryEntry.user = user; + if (note) { + newHistoryEntry.note = note; + } + newHistoryEntry.pinStatus = false; + return newHistoryEntry; + } +} diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index 27424dd95..ed2c2aae6 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -20,6 +20,7 @@ import { Revision } from '../revisions/revision.entity'; import { User } from '../users/user.entity'; import { AuthorColor } from './author-color.entity'; import { Tag } from './tag.entity'; +import { HistoryEntry } from '../history/history-entry.entity'; @Entity() export class Note { @@ -53,6 +54,8 @@ export class Note { revisions: Promise; @OneToMany((_) => AuthorColor, (authorColor) => authorColor.note) authorColors: AuthorColor[]; + @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) + historyEntries: HistoryEntry[]; @Column({ nullable: true, diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index 0e3998987..8a377f482 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -21,6 +21,7 @@ import { import { NoteDto } from './note.dto'; import { Note } from './note.entity'; import { Tag } from './tag.entity'; +import { HistoryEntry } from '../history/history-entry.entity'; @Injectable() export class NotesService { @@ -46,6 +47,7 @@ export class NotesService { description: 'Very descriptive text.', userPermissions: [], groupPermissions: [], + historyEntries: [], tags: [], revisions: Promise.resolve([]), authorColors: [], @@ -69,6 +71,7 @@ export class NotesService { newNote.alias = alias; } if (owner) { + newNote.historyEntries = [HistoryEntry.create(owner)]; newNote.owner = owner; } return this.noteRepository.save(newNote); @@ -153,12 +156,14 @@ export class NotesService { id: '1', identities: [], ownedNotes: [], + historyEntries: [], updatedAt: new Date(), userName: 'Testy', }, description: 'Very descriptive text.', userPermissions: [], groupPermissions: [], + historyEntries: [], tags: [], revisions: Promise.resolve([]), authorColors: [], diff --git a/src/users/user.entity.ts b/src/users/user.entity.ts index cd0ab9c84..ba8016495 100644 --- a/src/users/user.entity.ts +++ b/src/users/user.entity.ts @@ -14,6 +14,7 @@ import { Column, OneToMany } from 'typeorm'; import { Note } from '../notes/note.entity'; import { AuthToken } from '../auth/auth-token.entity'; import { Identity } from './identity.entity'; +import { HistoryEntry } from '../history/history-entry.entity'; @Entity() export class User { @@ -51,8 +52,8 @@ export class User { @OneToMany((_) => Identity, (identity) => identity.user) identities: Identity[]; - // eslint-disable-next-line @typescript-eslint/no-empty-function - private constructor() {} + @OneToMany((_) => HistoryEntry, (historyEntry) => historyEntry.user) + historyEntries: HistoryEntry[]; public static create( userName: string,