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.isOwned()) {
192 await sendDeleteVideoComment(instance, options.transaction)
196 static loadById (id: number, t?: Sequelize.Transaction) {
197 const query: IFindOptions<VideoCommentModel> = {
203 if (t !== undefined) query.transaction = t
205 return VideoCommentModel.findOne(query)
208 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Sequelize.Transaction) {
209 const query: IFindOptions<VideoCommentModel> = {
215 if (t !== undefined) query.transaction = t
217 return VideoCommentModel
218 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
222 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
223 const query: IFindOptions<VideoCommentModel> = {
229 if (t !== undefined) query.transaction = t
231 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT ]).findOne(query)
234 static loadByUrlAndPopulateReplyAndVideo (url: string, t?: Sequelize.Transaction) {
235 const query: IFindOptions<VideoCommentModel> = {
241 if (t !== undefined) query.transaction = t
243 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_VIDEO ]).findOne(query)
246 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
250 order: [ getSort(sort) ],
253 inReplyToCommentId: null
257 return VideoCommentModel
258 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
259 .findAndCountAll(query)
260 .then(({ rows, count }) => {
261 return { total: count, data: rows }
265 static listThreadCommentsForApi (videoId: number, threadId: number) {
267 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ],
270 [ Sequelize.Op.or ]: [
272 { originCommentId: threadId }
277 return VideoCommentModel
278 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
279 .findAndCountAll(query)
280 .then(({ rows, count }) => {
281 return { total: count, data: rows }
285 static listThreadParentComments (comment: VideoCommentModel, t: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') {
287 order: [ [ 'createdAt', order ] ],
289 [ Sequelize.Op.or ]: [
290 { id: comment.getThreadId() },
291 { originCommentId: comment.getThreadId() }
294 [ Sequelize.Op.ne ]: comment.id
297 [ Sequelize.Op.lt ]: comment.createdAt
303 return VideoCommentModel
304 .scope([ ScopeNames.WITH_ACCOUNT ])
308 getThreadId (): number {
309 return this.originCommentId || this.id
313 return this.Account.isOwned()
321 threadId: this.originCommentId || this.id,
322 inReplyToCommentId: this.inReplyToCommentId || null,
323 videoId: this.videoId,
324 createdAt: this.createdAt,
325 updatedAt: this.updatedAt,
326 totalReplies: this.get('totalReplies') || 0,
327 account: this.Account.toFormattedJSON()
331 toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject {
332 let inReplyTo: string
333 // New thread, so in AS we reply to the video
334 if (this.inReplyToCommentId === null) {
335 inReplyTo = this.Video.url
337 inReplyTo = this.InReplyToVideoComment.url
340 const tag: ActivityTagObject[] = []
341 for (const parentComment of threadParentComments) {
342 const actor = parentComment.Account.Actor
347 name: `@${actor.preferredUsername}@${actor.getHost()}`
352 type: 'Note' as 'Note',
356 updated: this.updatedAt.toISOString(),
357 published: this.createdAt.toISOString(),
359 attributedTo: this.Account.Actor.url,