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 { ActorModel } from '../activitypub/actor'
12 import { ServerModel } from '../server/server'
13 import { getSort, throwIfNotValid } from '../utils'
14 import { VideoModel } from './video'
17 WITH_ACCOUNT = 'WITH_ACCOUNT',
18 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
19 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
23 [ScopeNames.ATTRIBUTES_FOR_API]: {
28 '(SELECT COUNT("replies"."id") ' +
29 'FROM "videoComment" AS "replies" ' +
30 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id")'
37 [ScopeNames.WITH_ACCOUNT]: {
40 model: () => AccountModel,
43 model: () => ActorModel,
46 model: () => ServerModel,
55 [ScopeNames.WITH_IN_REPLY_TO]: {
58 model: () => VideoCommentModel,
65 tableName: 'videoComment',
71 fields: [ 'videoId', 'originCommentId' ]
75 export class VideoCommentModel extends Model<VideoCommentModel> {
83 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
84 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
88 @Column(DataType.TEXT)
91 @ForeignKey(() => VideoCommentModel)
93 originCommentId: number
95 @BelongsTo(() => VideoCommentModel, {
101 OriginVideoComment: VideoCommentModel
103 @ForeignKey(() => VideoCommentModel)
105 inReplyToCommentId: number
107 @BelongsTo(() => VideoCommentModel, {
114 InReplyToVideoComment: VideoCommentModel
116 @ForeignKey(() => VideoModel)
120 @BelongsTo(() => VideoModel, {
128 @ForeignKey(() => AccountModel)
132 @BelongsTo(() => AccountModel, {
138 Account: AccountModel
141 static sendDeleteIfOwned (instance: VideoCommentModel) {
146 static loadById (id: number, t?: Sequelize.Transaction) {
147 const query: IFindOptions<VideoCommentModel> = {
153 if (t !== undefined) query.transaction = t
155 return VideoCommentModel.findOne(query)
158 static loadByUrl (url: string, t?: Sequelize.Transaction) {
159 const query: IFindOptions<VideoCommentModel> = {
165 if (t !== undefined) query.transaction = t
167 return VideoCommentModel.findOne(query)
170 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
174 order: [ getSort(sort) ],
177 inReplyToCommentId: null
181 return VideoCommentModel
182 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
183 .findAndCountAll(query)
184 .then(({ rows, count }) => {
185 return { total: count, data: rows }
189 static listThreadCommentsForApi (videoId: number, threadId: number) {
191 order: [ [ 'id', 'ASC' ] ],
194 [ Sequelize.Op.or ]: [
196 { originCommentId: threadId }
201 return VideoCommentModel
202 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
203 .findAndCountAll(query)
204 .then(({ rows, count }) => {
205 return { total: count, data: rows }
214 threadId: this.originCommentId || this.id,
215 inReplyToCommentId: this.inReplyToCommentId || null,
216 videoId: this.videoId,
217 createdAt: this.createdAt,
218 updatedAt: this.updatedAt,
219 totalReplies: this.get('totalReplies') || 0,
221 name: this.Account.name,
222 host: this.Account.Actor.getHost()
227 toActivityPubObject (): VideoCommentObject {
228 let inReplyTo: string
229 // New thread, so in AS we reply to the video
230 if (this.inReplyToCommentId === null) {
231 inReplyTo = this.Video.url
233 inReplyTo = this.InReplyToVideoComment.url
237 type: 'Note' as 'Note',
241 published: this.createdAt.toISOString(),