2021-01-05 21:12:38 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2020-07-25 18:24:59 +00:00
|
|
|
import { generate as shortIdGenerate } from 'shortid';
|
|
|
|
import {
|
|
|
|
Column,
|
|
|
|
Entity,
|
2020-10-03 09:17:07 +00:00
|
|
|
JoinTable,
|
|
|
|
ManyToMany,
|
2020-07-25 18:24:59 +00:00
|
|
|
ManyToOne,
|
|
|
|
OneToMany,
|
|
|
|
PrimaryGeneratedColumn,
|
|
|
|
} from 'typeorm';
|
2020-08-13 18:24:45 +00:00
|
|
|
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
|
|
|
import { NoteUserPermission } from '../permissions/note-user-permission.entity';
|
2020-07-25 18:24:59 +00:00
|
|
|
import { Revision } from '../revisions/revision.entity';
|
|
|
|
import { User } from '../users/user.entity';
|
2020-08-13 18:24:45 +00:00
|
|
|
import { AuthorColor } from './author-color.entity';
|
2020-10-03 09:17:07 +00:00
|
|
|
import { Tag } from './tag.entity';
|
2020-07-25 18:24:59 +00:00
|
|
|
|
|
|
|
@Entity('Notes')
|
|
|
|
export class Note {
|
|
|
|
@PrimaryGeneratedColumn('uuid')
|
|
|
|
id: string;
|
|
|
|
@Column({
|
|
|
|
nullable: false,
|
|
|
|
unique: true,
|
|
|
|
})
|
|
|
|
shortid: string;
|
|
|
|
@Column({
|
|
|
|
unique: true,
|
|
|
|
nullable: true,
|
|
|
|
})
|
2020-10-03 09:17:07 +00:00
|
|
|
alias?: string;
|
2020-08-13 18:24:45 +00:00
|
|
|
@OneToMany(
|
|
|
|
_ => NoteGroupPermission,
|
|
|
|
groupPermission => groupPermission.note,
|
|
|
|
)
|
|
|
|
groupPermissions: NoteGroupPermission[];
|
|
|
|
@OneToMany(
|
|
|
|
_ => NoteUserPermission,
|
|
|
|
userPermission => userPermission.note,
|
|
|
|
)
|
|
|
|
userPermissions: NoteUserPermission[];
|
2020-07-25 18:24:59 +00:00
|
|
|
@Column({
|
|
|
|
nullable: false,
|
|
|
|
default: 0,
|
|
|
|
})
|
|
|
|
viewcount: number;
|
2020-08-13 18:24:45 +00:00
|
|
|
@ManyToOne(
|
|
|
|
_ => User,
|
|
|
|
user => user.ownedNotes,
|
|
|
|
{ onDelete: 'CASCADE' },
|
|
|
|
)
|
2020-07-25 18:24:59 +00:00
|
|
|
owner: User;
|
|
|
|
@OneToMany(
|
|
|
|
_ => Revision,
|
|
|
|
revision => revision.note,
|
2020-09-19 13:57:34 +00:00
|
|
|
{ cascade: true },
|
2020-07-25 18:24:59 +00:00
|
|
|
)
|
2020-09-22 15:32:35 +00:00
|
|
|
revisions: Promise<Revision[]>;
|
2020-07-25 18:24:59 +00:00
|
|
|
@OneToMany(
|
2020-08-13 18:24:45 +00:00
|
|
|
_ => AuthorColor,
|
|
|
|
authorColor => authorColor.note,
|
2020-07-25 18:24:59 +00:00
|
|
|
)
|
2020-08-13 18:24:45 +00:00
|
|
|
authorColors: AuthorColor[];
|
2020-07-25 18:24:59 +00:00
|
|
|
|
2020-10-03 09:17:07 +00:00
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
})
|
|
|
|
description?: string;
|
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
})
|
|
|
|
title?: string;
|
|
|
|
|
|
|
|
@ManyToMany(
|
|
|
|
_ => Tag,
|
|
|
|
tag => tag.notes,
|
2020-10-03 13:37:57 +00:00
|
|
|
{ eager: true, cascade: true },
|
2020-10-03 09:17:07 +00:00
|
|
|
)
|
|
|
|
@JoinTable()
|
|
|
|
tags: Tag[];
|
|
|
|
|
2020-09-19 14:00:29 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
private constructor() {}
|
|
|
|
|
2020-10-03 09:17:07 +00:00
|
|
|
public static create(owner?: User, alias?: string, shortid?: string): Note {
|
2020-09-19 14:00:29 +00:00
|
|
|
if (!shortid) {
|
|
|
|
shortid = shortIdGenerate();
|
2020-07-25 18:24:59 +00:00
|
|
|
}
|
2020-09-19 14:00:29 +00:00
|
|
|
const newNote = new Note();
|
|
|
|
newNote.shortid = shortid;
|
|
|
|
newNote.alias = alias;
|
|
|
|
newNote.viewcount = 0;
|
|
|
|
newNote.owner = owner;
|
2020-09-19 15:30:58 +00:00
|
|
|
newNote.authorColors = [];
|
|
|
|
newNote.userPermissions = [];
|
|
|
|
newNote.groupPermissions = [];
|
2020-10-03 09:17:07 +00:00
|
|
|
newNote.revisions = Promise.resolve([]);
|
|
|
|
newNote.description = null;
|
|
|
|
newNote.title = null;
|
|
|
|
newNote.tags = [];
|
2020-09-19 14:00:29 +00:00
|
|
|
return newNote;
|
2020-07-25 18:24:59 +00:00
|
|
|
}
|
|
|
|
}
|