1 import * as Sequelize from 'sequelize'
3 AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
5 } from 'sequelize-typescript'
6 import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects'
7 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
8 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
9 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
10 import { CONSTRAINTS_FIELDS } from '../../initializers'
11 import { sendDeleteVideoComment } from '../../lib/activitypub/send'
12 import { AccountModel } from '../account/account'
13 import { ActorModel } from '../activitypub/actor'
14 import { AvatarModel } from '../avatar/avatar'
15 import { ServerModel } from '../server/server'
16 import { getSort, throwIfNotValid } from '../utils'
17 import { VideoModel } from './video'
18 import { VideoChannelModel } from './video-channel'
21 WITH_ACCOUNT = 'WITH_ACCOUNT',
22 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
23 WITH_VIDEO = 'WITH_VIDEO',
24 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
28 [ScopeNames.ATTRIBUTES_FOR_API]: {
33 '(SELECT COUNT("replies"."id") ' +
34 'FROM "videoComment" AS "replies" ' +
35 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id")'
42 [ScopeNames.WITH_ACCOUNT]: {
45 model: () => AccountModel,
48 model: () => ActorModel,
51 model: () => ServerModel,
55 model: () => AvatarModel,
64 [ScopeNames.WITH_IN_REPLY_TO]: {
67 model: () => VideoCommentModel,
68 as: 'InReplyToVideoComment'
72 [ScopeNames.WITH_VIDEO]: {
75 model: () => VideoModel,
79 model: () => VideoChannelModel.unscoped(),
83 model: () => AccountModel,
87 model: () => ActorModel,
100 tableName: 'videoComment',
103 fields: [ 'videoId' ]
106 fields: [ 'videoId', 'originCommentId' ]
114 export class VideoCommentModel extends Model<VideoCommentModel> {
122 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
123 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
127 @Column(DataType.TEXT)
130 @ForeignKey(() => VideoCommentModel)
132 originCommentId: number
134 @BelongsTo(() => VideoCommentModel, {
136 name: 'originCommentId',
139 as: 'OriginVideoComment',
142 OriginVideoComment: VideoCommentModel
144 @ForeignKey(() => VideoCommentModel)
146 inReplyToCommentId: number
148 @BelongsTo(() => VideoCommentModel, {
150 name: 'inReplyToCommentId',
153 as: 'InReplyToVideoComment',
156 InReplyToVideoComment: VideoCommentModel
158 @ForeignKey(() => VideoModel)
162 @BelongsTo(() => VideoModel, {
170 @ForeignKey(() => AccountModel)
174 @BelongsTo(() => AccountModel, {
180 Account: AccountModel
183 static async sendDeleteIfOwned (instance: VideoCommentModel, options) {
184 if (!instance.Account || !instance.Account.Actor) {
185 instance.Account = await instance.$get('Account', {
186 include: [ ActorModel ],
187 transaction: options.transaction
191 if (!instance.Video) {
192 instance.Video = await instance.$get('Video', {
195 model: VideoChannelModel,
208 transaction: options.transaction
212 if (instance.isOwned()) {
213 await sendDeleteVideoComment(instance, options.transaction)
217 static loadById (id: number, t?: Sequelize.Transaction) {
218 const query: IFindOptions<VideoCommentModel> = {
224 if (t !== undefined) query.transaction = t
226 return VideoCommentModel.findOne(query)
229 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Sequelize.Transaction) {
230 const query: IFindOptions<VideoCommentModel> = {
236 if (t !== undefined) query.transaction = t
238 return VideoCommentModel
239 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
243 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
244 const query: IFindOptions<VideoCommentModel> = {
250 if (t !== undefined) query.transaction = t
252 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT ]).findOne(query)
255 static loadByUrlAndPopulateReplyAndVideo (url: string, t?: Sequelize.Transaction) {
256 const query: IFindOptions<VideoCommentModel> = {
262 if (t !== undefined) query.transaction = t
264 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_VIDEO ]).findOne(query)
267 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
271 order: getSort(sort),
274 inReplyToCommentId: null
278 return VideoCommentModel
279 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
280 .findAndCountAll(query)
281 .then(({ rows, count }) => {
282 return { total: count, data: rows }
286 static listThreadCommentsForApi (videoId: number, threadId: number) {
288 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ],
291 [ Sequelize.Op.or ]: [
293 { originCommentId: threadId }
298 return VideoCommentModel
299 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
300 .findAndCountAll(query)
301 .then(({ rows, count }) => {
302 return { total: count, data: rows }
306 static listThreadParentComments (comment: VideoCommentModel, t: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') {
308 order: [ [ 'createdAt', order ] ],
310 [ Sequelize.Op.or ]: [
311 { id: comment.getThreadId() },
312 { originCommentId: comment.getThreadId() }
315 [ Sequelize.Op.ne ]: comment.id
318 [ Sequelize.Op.lt ]: comment.createdAt
324 return VideoCommentModel
325 .scope([ ScopeNames.WITH_ACCOUNT ])
329 getThreadId (): number {
330 return this.originCommentId || this.id
334 return this.Account.isOwned()
342 threadId: this.originCommentId || this.id,
343 inReplyToCommentId: this.inReplyToCommentId || null,
344 videoId: this.videoId,
345 createdAt: this.createdAt,
346 updatedAt: this.updatedAt,
347 totalReplies: this.get('totalReplies') || 0,
348 account: this.Account.toFormattedJSON()
352 toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject {
353 let inReplyTo: string
354 // New thread, so in AS we reply to the video
355 if (this.inReplyToCommentId === null) {
356 inReplyTo = this.Video.url
358 inReplyTo = this.InReplyToVideoComment.url
361 const tag: ActivityTagObject[] = []
362 for (const parentComment of threadParentComments) {
363 const actor = parentComment.Account.Actor
368 name: `@${actor.preferredUsername}@${actor.getHost()}`
373 type: 'Note' as 'Note',
377 updated: this.updatedAt.toISOString(),
378 published: this.createdAt.toISOString(),
380 attributedTo: this.Account.Actor.url,