From 053da10e94c2412f58215116a958c0922261a610 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 20 Sep 2023 11:33:36 +0900 Subject: refactor(backend): update directory structure for models --- packages/backend/src/models/Note.ts | 258 ++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 packages/backend/src/models/Note.ts (limited to 'packages/backend/src/models/Note.ts') diff --git a/packages/backend/src/models/Note.ts b/packages/backend/src/models/Note.ts new file mode 100644 index 0000000000..549e9cfe90 --- /dev/null +++ b/packages/backend/src/models/Note.ts @@ -0,0 +1,258 @@ +/* + * SPDX-FileCopyrightText: syuilo and other misskey contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { Entity, Index, JoinColumn, Column, PrimaryColumn, ManyToOne } from 'typeorm'; +import { id } from './util/id.js'; +import { noteVisibilities } from '../../types.js'; +import { MiUser } from './User.js'; +import { MiChannel } from './Channel.js'; +import type { MiDriveFile } from './DriveFile.js'; + +@Entity('note') +@Index('IDX_NOTE_TAGS', { synchronize: false }) +@Index('IDX_NOTE_MENTIONS', { synchronize: false }) +@Index('IDX_NOTE_VISIBLE_USER_IDS', { synchronize: false }) +export class MiNote { + @PrimaryColumn(id()) + public id: string; + + @Index() + @Column('timestamp with time zone', { + comment: 'The created date of the Note.', + }) + public createdAt: Date; + + @Index() + @Column({ + ...id(), + nullable: true, + comment: 'The ID of reply target.', + }) + public replyId: MiNote['id'] | null; + + @ManyToOne(type => MiNote, { + onDelete: 'CASCADE', + }) + @JoinColumn() + public reply: MiNote | null; + + @Index() + @Column({ + ...id(), + nullable: true, + comment: 'The ID of renote target.', + }) + public renoteId: MiNote['id'] | null; + + @ManyToOne(type => MiNote, { + onDelete: 'CASCADE', + }) + @JoinColumn() + public renote: MiNote | null; + + @Index() + @Column('varchar', { + length: 256, nullable: true, + }) + public threadId: string | null; + + // TODO: varcharにしたい + @Column('text', { + nullable: true, + }) + public text: string | null; + + @Column('varchar', { + length: 256, nullable: true, + }) + public name: string | null; + + @Column('varchar', { + length: 512, nullable: true, + }) + public cw: string | null; + + @Index() + @Column({ + ...id(), + comment: 'The ID of author.', + }) + public userId: MiUser['id']; + + @ManyToOne(type => MiUser, { + onDelete: 'CASCADE', + }) + @JoinColumn() + public user: MiUser | null; + + @Column('boolean', { + default: false, + }) + public localOnly: boolean; + + @Column('varchar', { + length: 64, nullable: true, + }) + public reactionAcceptance: 'likeOnly' | 'likeOnlyForRemote' | 'nonSensitiveOnly' | 'nonSensitiveOnlyForLocalLikeOnlyForRemote' | null; + + @Column('smallint', { + default: 0, + }) + public renoteCount: number; + + @Column('smallint', { + default: 0, + }) + public repliesCount: number; + + @Column('smallint', { + default: 0, + }) + public clippedCount: number; + + @Column('jsonb', { + default: {}, + }) + public reactions: Record; + + /** + * public ... 公開 + * home ... ホームタイムライン(ユーザーページのタイムライン含む)のみに流す + * followers ... フォロワーのみ + * specified ... visibleUserIds で指定したユーザーのみ + */ + @Column('enum', { enum: noteVisibilities }) + public visibility: typeof noteVisibilities[number]; + + @Index({ unique: true }) + @Column('varchar', { + length: 512, nullable: true, + comment: 'The URI of a note. it will be null when the note is local.', + }) + public uri: string | null; + + @Column('varchar', { + length: 512, nullable: true, + comment: 'The human readable url of a note. it will be null when the note is local.', + }) + public url: string | null; + + @Column('integer', { + default: 0, select: false, + }) + public score: number; + + @Index() + @Column({ + ...id(), + array: true, default: '{}', + }) + public fileIds: MiDriveFile['id'][]; + + @Index() + @Column('varchar', { + length: 256, array: true, default: '{}', + }) + public attachedFileTypes: string[]; + + @Index() + @Column({ + ...id(), + array: true, default: '{}', + }) + public visibleUserIds: MiUser['id'][]; + + @Index() + @Column({ + ...id(), + array: true, default: '{}', + }) + public mentions: MiUser['id'][]; + + @Column('text', { + default: '[]', + }) + public mentionedRemoteUsers: string; + + @Column('varchar', { + length: 128, array: true, default: '{}', + }) + public emojis: string[]; + + @Index() + @Column('varchar', { + length: 128, array: true, default: '{}', + }) + public tags: string[]; + + @Column('boolean', { + default: false, + }) + public hasPoll: boolean; + + @Index() + @Column({ + ...id(), + nullable: true, + comment: 'The ID of source channel.', + }) + public channelId: MiChannel['id'] | null; + + @ManyToOne(type => MiChannel, { + onDelete: 'CASCADE', + }) + @JoinColumn() + public channel: MiChannel | null; + + //#region Denormalized fields + @Index() + @Column('varchar', { + length: 128, nullable: true, + comment: '[Denormalized]', + }) + public userHost: string | null; + + @Column({ + ...id(), + nullable: true, + comment: '[Denormalized]', + }) + public replyUserId: MiUser['id'] | null; + + @Column('varchar', { + length: 128, nullable: true, + comment: '[Denormalized]', + }) + public replyUserHost: string | null; + + @Column({ + ...id(), + nullable: true, + comment: '[Denormalized]', + }) + public renoteUserId: MiUser['id'] | null; + + @Column('varchar', { + length: 128, nullable: true, + comment: '[Denormalized]', + }) + public renoteUserHost: string | null; + //#endregion + + constructor(data: Partial) { + if (data == null) return; + + for (const [k, v] of Object.entries(data)) { + (this as any)[k] = v; + } + } +} + +export type IMentionedRemoteUsers = { + uri: string; + url?: string; + username: string; + host: string; +}[]; -- cgit v1.2.3-freya From 5d65e34078ead0f1f8572bb0d509627a42c1b0c5 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 20 Sep 2023 11:40:17 +0900 Subject: fix import --- packages/backend/src/models/MutedNote.ts | 2 +- packages/backend/src/models/Note.ts | 2 +- packages/backend/src/models/Poll.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/backend/src/models/Note.ts') diff --git a/packages/backend/src/models/MutedNote.ts b/packages/backend/src/models/MutedNote.ts index 75b7ab5fe9..89a678a2a7 100644 --- a/packages/backend/src/models/MutedNote.ts +++ b/packages/backend/src/models/MutedNote.ts @@ -4,8 +4,8 @@ */ import { Entity, Index, JoinColumn, Column, ManyToOne, PrimaryColumn } from 'typeorm'; +import { mutedNoteReasons } from '@/types.js'; import { id } from './util/id.js'; -import { mutedNoteReasons } from '../../types.js'; import { MiNote } from './Note.js'; import { MiUser } from './User.js'; diff --git a/packages/backend/src/models/Note.ts b/packages/backend/src/models/Note.ts index 549e9cfe90..ed86d4549e 100644 --- a/packages/backend/src/models/Note.ts +++ b/packages/backend/src/models/Note.ts @@ -4,8 +4,8 @@ */ import { Entity, Index, JoinColumn, Column, PrimaryColumn, ManyToOne } from 'typeorm'; +import { noteVisibilities } from '@/types.js'; import { id } from './util/id.js'; -import { noteVisibilities } from '../../types.js'; import { MiUser } from './User.js'; import { MiChannel } from './Channel.js'; import type { MiDriveFile } from './DriveFile.js'; diff --git a/packages/backend/src/models/Poll.ts b/packages/backend/src/models/Poll.ts index 6863a78b7a..5ce0b9a2fc 100644 --- a/packages/backend/src/models/Poll.ts +++ b/packages/backend/src/models/Poll.ts @@ -4,8 +4,8 @@ */ import { PrimaryColumn, Entity, Index, JoinColumn, Column, OneToOne } from 'typeorm'; +import { noteVisibilities } from '@/types.js'; import { id } from './util/id.js'; -import { noteVisibilities } from '../../types.js'; import { MiNote } from './Note.js'; import type { MiUser } from './User.js'; -- cgit v1.2.3-freya