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/misc'
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 WITH_VIDEO = 'WITH_VIDEO',
20 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
24 [ScopeNames.ATTRIBUTES_FOR_API]: {
29 '(SELECT COUNT("replies"."id") ' +
30 'FROM "videoComment" AS "replies" ' +
31 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id")'
38 [ScopeNames.WITH_ACCOUNT]: {
41 model: () => AccountModel,
44 model: () => ActorModel,
47 model: () => ServerModel,
56 [ScopeNames.WITH_IN_REPLY_TO]: {
59 model: () => VideoCommentModel,
60 as: 'InReplyToVideoComment'
64 [ScopeNames.WITH_VIDEO]: {
67 model: () => VideoModel,
74 tableName: 'videoComment',
80 fields: [ 'videoId', 'originCommentId' ]
84 export class VideoCommentModel extends Model<VideoCommentModel> {
92 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
93 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
97 @Column(DataType.TEXT)
100 @ForeignKey(() => VideoCommentModel)
102 originCommentId: number
104 @BelongsTo(() => VideoCommentModel, {
106 name: 'originCommentId',
109 as: 'OriginVideoComment',
112 OriginVideoComment: VideoCommentModel
114 @ForeignKey(() => VideoCommentModel)
116 inReplyToCommentId: number
118 @BelongsTo(() => VideoCommentModel, {
120 name: 'inReplyToCommentId',
123 as: 'InReplyToVideoComment',
126 InReplyToVideoComment: VideoCommentModel
128 @ForeignKey(() => VideoModel)
132 @BelongsTo(() => VideoModel, {
140 @ForeignKey(() => AccountModel)
144 @BelongsTo(() => AccountModel, {
150 Account: AccountModel
153 static sendDeleteIfOwned (instance: VideoCommentModel) {
158 static loadById (id: number, t?: Sequelize.Transaction) {
159 const query: IFindOptions<VideoCommentModel> = {
165 if (t !== undefined) query.transaction = t
167 return VideoCommentModel.findOne(query)
170 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Sequelize.Transaction) {
171 const query: IFindOptions<VideoCommentModel> = {
177 if (t !== undefined) query.transaction = t
179 return VideoCommentModel
180 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
184 static loadByUrl (url: string, t?: Sequelize.Transaction) {
185 const query: IFindOptions<VideoCommentModel> = {
191 if (t !== undefined) query.transaction = t
193 return VideoCommentModel.findOne(query)
196 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
200 order: [ getSort(sort) ],
203 inReplyToCommentId: null
207 return VideoCommentModel
208 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
209 .findAndCountAll(query)
210 .then(({ rows, count }) => {
211 return { total: count, data: rows }
215 static listThreadCommentsForApi (videoId: number, threadId: number) {
217 order: [ [ 'createdAt', 'DESC' ] ],
220 [ Sequelize.Op.or ]: [
222 { originCommentId: threadId }
227 return VideoCommentModel
228 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
229 .findAndCountAll(query)
230 .then(({ rows, count }) => {
231 return { total: count, data: rows }
240 threadId: this.originCommentId || this.id,
241 inReplyToCommentId: this.inReplyToCommentId || null,
242 videoId: this.videoId,
243 createdAt: this.createdAt,
244 updatedAt: this.updatedAt,
245 totalReplies: this.get('totalReplies') || 0,
247 name: this.Account.name,
248 host: this.Account.Actor.getHost()
253 toActivityPubObject (): VideoCommentObject {
254 let inReplyTo: string
255 // New thread, so in AS we reply to the video
256 if (this.inReplyToCommentId === null) {
257 inReplyTo = this.Video.url
259 inReplyTo = this.InReplyToVideoComment.url
263 type: 'Note' as 'Note',
267 updated: this.updatedAt.toISOString(),
268 published: this.createdAt.toISOString(),
270 attributedTo: this.Account.Actor.url