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 { 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' ]
110 export class VideoCommentModel extends Model<VideoCommentModel> {
118 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
119 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
123 @Column(DataType.TEXT)
126 @ForeignKey(() => VideoCommentModel)
128 originCommentId: number
130 @BelongsTo(() => VideoCommentModel, {
132 name: 'originCommentId',
135 as: 'OriginVideoComment',
138 OriginVideoComment: VideoCommentModel
140 @ForeignKey(() => VideoCommentModel)
142 inReplyToCommentId: number
144 @BelongsTo(() => VideoCommentModel, {
146 name: 'inReplyToCommentId',
149 as: 'InReplyToVideoComment',
152 InReplyToVideoComment: VideoCommentModel
154 @ForeignKey(() => VideoModel)
158 @BelongsTo(() => VideoModel, {
166 @ForeignKey(() => AccountModel)
170 @BelongsTo(() => AccountModel, {
176 Account: AccountModel
179 static async sendDeleteIfOwned (instance: VideoCommentModel) {
180 if (instance.isOwned()) {
181 await sendDeleteVideoComment(instance, undefined)
185 static loadById (id: number, t?: Sequelize.Transaction) {
186 const query: IFindOptions<VideoCommentModel> = {
192 if (t !== undefined) query.transaction = t
194 return VideoCommentModel.findOne(query)
197 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Sequelize.Transaction) {
198 const query: IFindOptions<VideoCommentModel> = {
204 if (t !== undefined) query.transaction = t
206 return VideoCommentModel
207 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
211 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
212 const query: IFindOptions<VideoCommentModel> = {
218 if (t !== undefined) query.transaction = t
220 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT ]).findOne(query)
223 static loadByUrlAndPopulateReplyAndVideo (url: string, t?: Sequelize.Transaction) {
224 const query: IFindOptions<VideoCommentModel> = {
230 if (t !== undefined) query.transaction = t
232 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_VIDEO ]).findOne(query)
235 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
239 order: [ getSort(sort) ],
242 inReplyToCommentId: null
246 return VideoCommentModel
247 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
248 .findAndCountAll(query)
249 .then(({ rows, count }) => {
250 return { total: count, data: rows }
254 static listThreadCommentsForApi (videoId: number, threadId: number) {
256 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ],
259 [ Sequelize.Op.or ]: [
261 { originCommentId: threadId }
266 return VideoCommentModel
267 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
268 .findAndCountAll(query)
269 .then(({ rows, count }) => {
270 return { total: count, data: rows }
274 static listThreadParentComments (comment: VideoCommentModel, t: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') {
276 order: [ [ 'createdAt', order ] ],
278 [ Sequelize.Op.or ]: [
279 { id: comment.getThreadId() },
280 { originCommentId: comment.getThreadId() }
283 [ Sequelize.Op.ne ]: comment.id
286 [ Sequelize.Op.lt ]: comment.createdAt
292 return VideoCommentModel
293 .scope([ ScopeNames.WITH_ACCOUNT ])
297 getThreadId (): number {
298 return this.originCommentId || this.id
302 return this.Account.isOwned()
310 threadId: this.originCommentId || this.id,
311 inReplyToCommentId: this.inReplyToCommentId || null,
312 videoId: this.videoId,
313 createdAt: this.createdAt,
314 updatedAt: this.updatedAt,
315 totalReplies: this.get('totalReplies') || 0,
316 account: this.Account.toFormattedJSON()
320 toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject {
321 let inReplyTo: string
322 // New thread, so in AS we reply to the video
323 if (this.inReplyToCommentId === null) {
324 inReplyTo = this.Video.url
326 inReplyTo = this.InReplyToVideoComment.url
329 const tag: ActivityTagObject[] = []
330 for (const parentComment of threadParentComments) {
331 const actor = parentComment.Account.Actor
336 name: `@${actor.preferredUsername}@${actor.getHost()}`
341 type: 'Note' as 'Note',
345 updated: this.updatedAt.toISOString(),
346 published: this.createdAt.toISOString(),
348 attributedTo: this.Account.Actor.url,