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' ]
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, options) {
180 if (!instance.Account || !instance.Account.Actor) {
181 instance.Account = await instance.$get('Account', {
182 include: [ ActorModel ],
183 transaction: options.transaction
187 if (instance.isOwned()) {
188 await sendDeleteVideoComment(instance, options.transaction)
192 static loadById (id: number, t?: Sequelize.Transaction) {
193 const query: IFindOptions<VideoCommentModel> = {
199 if (t !== undefined) query.transaction = t
201 return VideoCommentModel.findOne(query)
204 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Sequelize.Transaction) {
205 const query: IFindOptions<VideoCommentModel> = {
211 if (t !== undefined) query.transaction = t
213 return VideoCommentModel
214 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
218 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
219 const query: IFindOptions<VideoCommentModel> = {
225 if (t !== undefined) query.transaction = t
227 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT ]).findOne(query)
230 static loadByUrlAndPopulateReplyAndVideo (url: string, t?: Sequelize.Transaction) {
231 const query: IFindOptions<VideoCommentModel> = {
237 if (t !== undefined) query.transaction = t
239 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_VIDEO ]).findOne(query)
242 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
246 order: [ getSort(sort) ],
249 inReplyToCommentId: null
253 return VideoCommentModel
254 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
255 .findAndCountAll(query)
256 .then(({ rows, count }) => {
257 return { total: count, data: rows }
261 static listThreadCommentsForApi (videoId: number, threadId: number) {
263 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ],
266 [ Sequelize.Op.or ]: [
268 { originCommentId: threadId }
273 return VideoCommentModel
274 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
275 .findAndCountAll(query)
276 .then(({ rows, count }) => {
277 return { total: count, data: rows }
281 static listThreadParentComments (comment: VideoCommentModel, t: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') {
283 order: [ [ 'createdAt', order ] ],
285 [ Sequelize.Op.or ]: [
286 { id: comment.getThreadId() },
287 { originCommentId: comment.getThreadId() }
290 [ Sequelize.Op.ne ]: comment.id
293 [ Sequelize.Op.lt ]: comment.createdAt
299 return VideoCommentModel
300 .scope([ ScopeNames.WITH_ACCOUNT ])
304 getThreadId (): number {
305 return this.originCommentId || this.id
309 return this.Account.isOwned()
317 threadId: this.originCommentId || this.id,
318 inReplyToCommentId: this.inReplyToCommentId || null,
319 videoId: this.videoId,
320 createdAt: this.createdAt,
321 updatedAt: this.updatedAt,
322 totalReplies: this.get('totalReplies') || 0,
323 account: this.Account.toFormattedJSON()
327 toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject {
328 let inReplyTo: string
329 // New thread, so in AS we reply to the video
330 if (this.inReplyToCommentId === null) {
331 inReplyTo = this.Video.url
333 inReplyTo = this.InReplyToVideoComment.url
336 const tag: ActivityTagObject[] = []
337 for (const parentComment of threadParentComments) {
338 const actor = parentComment.Account.Actor
343 name: `@${actor.preferredUsername}@${actor.getHost()}`
348 type: 'Note' as 'Note',
352 updated: this.updatedAt.toISOString(),
353 published: this.createdAt.toISOString(),
355 attributedTo: this.Account.Actor.url,