1 import * as Sequelize from 'sequelize'
3 AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
5 } from 'sequelize-typescript'
6 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
7 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
8 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
9 import { CONSTRAINTS_FIELDS } from '../../initializers'
10 import { AccountModel } from '../account/account'
11 import { getSort, throwIfNotValid } from '../utils'
12 import { VideoModel } from './video'
15 WITH_ACCOUNT = 'WITH_ACCOUNT',
16 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO'
20 [ScopeNames.WITH_ACCOUNT]: {
25 [ScopeNames.WITH_IN_REPLY_TO]: {
28 model: () => VideoCommentModel,
35 tableName: 'videoComment',
41 fields: [ 'videoId', 'originCommentId' ]
45 export class VideoCommentModel extends Model<VideoCommentModel> {
53 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
54 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
58 @Column(DataType.TEXT)
61 @ForeignKey(() => VideoCommentModel)
63 originCommentId: number
65 @BelongsTo(() => VideoCommentModel, {
71 OriginVideoComment: VideoCommentModel
73 @ForeignKey(() => VideoCommentModel)
75 inReplyToCommentId: number
77 @BelongsTo(() => VideoCommentModel, {
84 InReplyToVideoComment: VideoCommentModel
86 @ForeignKey(() => VideoModel)
90 @BelongsTo(() => VideoModel, {
98 @ForeignKey(() => AccountModel)
102 @BelongsTo(() => AccountModel, {
108 Account: AccountModel
111 static sendDeleteIfOwned (instance: VideoCommentModel) {
116 static loadById (id: number, t?: Sequelize.Transaction) {
117 const query: IFindOptions<VideoCommentModel> = {
123 if (t !== undefined) query.transaction = t
125 return VideoCommentModel.findOne(query)
128 static loadByUrl (url: string, t?: Sequelize.Transaction) {
129 const query: IFindOptions<VideoCommentModel> = {
135 if (t !== undefined) query.transaction = t
137 return VideoCommentModel.findOne(query)
140 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
144 order: [ getSort(sort) ],
147 inReplyToCommentId: null
151 return VideoCommentModel
152 .scope([ ScopeNames.WITH_ACCOUNT ])
153 .findAndCountAll(query)
154 .then(({ rows, count }) => {
155 return { total: count, data: rows }
159 static listThreadCommentsForApi (videoId: number, threadId: number) {
161 order: [ [ 'id', 'ASC' ] ],
164 [ Sequelize.Op.or ]: [
166 { originCommentId: threadId }
171 return VideoCommentModel
172 .scope([ ScopeNames.WITH_ACCOUNT ])
173 .findAndCountAll(query)
174 .then(({ rows, count }) => {
175 return { total: count, data: rows }
184 threadId: this.originCommentId || this.id,
185 inReplyToCommentId: this.inReplyToCommentId,
186 videoId: this.videoId,
187 createdAt: this.createdAt,
188 updatedAt: this.updatedAt,
190 name: this.Account.name
195 toActivityPubObject (): VideoCommentObject {
196 let inReplyTo: string
197 // New thread, so in AS we reply to the video
198 if (this.inReplyToCommentId === null) {
199 inReplyTo = this.Video.url
201 inReplyTo = this.InReplyToVideoComment.url
205 type: 'Note' as 'Note',
209 published: this.createdAt.toISOString(),