1 import * as Bluebird from 'bluebird'
2 import { uniq } from 'lodash'
3 import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { getServerActor } from '@server/models/application/application'
19 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
20 import { VideoPrivacy } from '@shared/models'
21 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
22 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
23 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
24 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
25 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
26 import { regexpCapture } from '../../helpers/regexp'
27 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
34 MCommentOwnerReplyVideoLight,
36 MCommentOwnerVideoFeed,
37 MCommentOwnerVideoReply,
39 } from '../../types/models/video'
40 import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
41 import { AccountModel } from '../account/account'
42 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
43 import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
44 import { VideoModel } from './video'
45 import { VideoChannelModel } from './video-channel'
48 WITH_ACCOUNT = 'WITH_ACCOUNT',
49 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
50 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
51 WITH_VIDEO = 'WITH_VIDEO',
52 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
56 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
63 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
64 'SELECT COUNT("replies"."id") - (' +
65 'SELECT COUNT("replies"."id") ' +
66 'FROM "videoComment" AS "replies" ' +
67 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
68 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
70 'FROM "videoComment" AS "replies" ' +
71 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
72 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
80 'SELECT COUNT("replies"."id") ' +
81 'FROM "videoComment" AS "replies" ' +
82 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
83 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
84 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
85 'AND "replies"."accountId" = "videoChannel"."accountId"' +
88 'totalRepliesFromVideoAuthor'
94 [ScopeNames.WITH_ACCOUNT]: {
101 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
104 model: AccountModel.unscoped(),
108 exclude: unusedActorAttributesForAPI
110 model: ActorModel, // Default scope includes avatar and server
117 [ScopeNames.WITH_IN_REPLY_TO]: {
120 model: VideoCommentModel,
121 as: 'InReplyToVideoComment'
125 [ScopeNames.WITH_VIDEO]: {
132 model: VideoChannelModel,
147 tableName: 'videoComment',
150 fields: [ 'videoId' ]
153 fields: [ 'videoId', 'originCommentId' ]
160 fields: [ 'accountId' ]
164 { name: 'createdAt', order: 'DESC' }
169 export class VideoCommentModel extends Model<VideoCommentModel> {
177 @Column(DataType.DATE)
181 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
182 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
186 @Column(DataType.TEXT)
189 @ForeignKey(() => VideoCommentModel)
191 originCommentId: number
193 @BelongsTo(() => VideoCommentModel, {
195 name: 'originCommentId',
198 as: 'OriginVideoComment',
201 OriginVideoComment: VideoCommentModel
203 @ForeignKey(() => VideoCommentModel)
205 inReplyToCommentId: number
207 @BelongsTo(() => VideoCommentModel, {
209 name: 'inReplyToCommentId',
212 as: 'InReplyToVideoComment',
215 InReplyToVideoComment: VideoCommentModel | null
217 @ForeignKey(() => VideoModel)
221 @BelongsTo(() => VideoModel, {
229 @ForeignKey(() => AccountModel)
233 @BelongsTo(() => AccountModel, {
239 Account: AccountModel
241 @HasMany(() => VideoCommentAbuseModel, {
243 name: 'videoCommentId',
248 CommentAbuses: VideoCommentAbuseModel[]
250 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
251 const query: FindOptions = {
257 if (t !== undefined) query.transaction = t
259 return VideoCommentModel.findOne(query)
262 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
263 const query: FindOptions = {
269 if (t !== undefined) query.transaction = t
271 return VideoCommentModel
272 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
276 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
277 const query: FindOptions = {
283 if (t !== undefined) query.transaction = t
285 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
288 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
289 const query: FindOptions = {
295 attributes: [ 'id', 'url' ],
296 model: VideoModel.unscoped()
301 if (t !== undefined) query.transaction = t
303 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
306 static async listThreadsForApi (parameters: {
308 isVideoOwned: boolean
312 user?: MUserAccountId
314 const { videoId, isVideoOwned, start, count, sort, user } = parameters
316 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
321 order: getCommentSort(sort),
328 inReplyToCommentId: null
334 [Op.notIn]: Sequelize.literal(
335 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
348 const scopes: (string | ScopeOptions)[] = [
349 ScopeNames.WITH_ACCOUNT_FOR_API,
351 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
355 return VideoCommentModel
357 .findAndCountAll(query)
358 .then(({ rows, count }) => {
359 return { total: count, data: rows }
363 static async listThreadCommentsForApi (parameters: {
365 isVideoOwned: boolean
367 user?: MUserAccountId
369 const { videoId, threadId, user, isVideoOwned } = parameters
371 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
374 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
379 { originCommentId: threadId }
382 [Op.notIn]: Sequelize.literal(
383 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
389 const scopes: any[] = [
390 ScopeNames.WITH_ACCOUNT_FOR_API,
392 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
396 return VideoCommentModel
398 .findAndCountAll(query)
399 .then(({ rows, count }) => {
400 return { total: count, data: rows }
404 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
406 order: [ [ 'createdAt', order ] ] as Order,
409 [Op.in]: Sequelize.literal('(' +
410 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
411 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
413 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
414 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
416 'SELECT id FROM children' +
424 return VideoCommentModel
425 .scope([ ScopeNames.WITH_ACCOUNT ])
429 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
430 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
432 isVideoOwned: video.isOwned()
436 order: [ [ 'createdAt', 'ASC' ] ] as Order,
442 [Op.notIn]: Sequelize.literal(
443 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
450 return VideoCommentModel.findAndCountAll<MComment>(query)
453 static async listForFeed (parameters: {
458 videoChannelId?: number
459 }): Promise<MCommentOwnerVideoFeed[]> {
460 const serverActor = await getServerActor()
461 const { start, count, videoId, accountId, videoChannelId } = parameters
463 const accountExclusion = {
464 [Op.notIn]: Sequelize.literal(
465 '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
468 const accountWhere = accountId
477 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
480 order: [ [ 'createdAt', 'DESC' ] ] as Order,
485 accountId: accountWhere
489 attributes: [ 'name', 'uuid' ],
490 model: VideoModel.unscoped(),
493 privacy: VideoPrivacy.PUBLIC
497 attributes: [ 'accountId' ],
498 model: VideoChannelModel.unscoped(),
500 where: videoChannelWhere
507 if (videoId) query.where['videoId'] = videoId
509 return VideoCommentModel
510 .scope([ ScopeNames.WITH_ACCOUNT ])
514 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
515 const accountWhere = filter.onVideosOfAccount
516 ? { id: filter.onVideosOfAccount.id }
523 accountId: ofAccount.id
531 model: VideoChannelModel,
546 return VideoCommentModel
547 .scope([ ScopeNames.WITH_ACCOUNT ])
551 static async getStats () {
552 const totalLocalVideoComments = await VideoCommentModel.count({
569 const totalVideoComments = await VideoCommentModel.count()
572 totalLocalVideoComments,
577 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
581 [Op.lt]: beforeUpdatedAt
585 [Op.notIn]: buildLocalAccountIdsIn()
587 // Do not delete Tombstones
592 return VideoCommentModel.destroy(query)
595 getCommentStaticPath () {
596 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
599 getThreadId (): number {
600 return this.originCommentId || this.id
608 return this.Account.isOwned()
612 return this.deletedAt !== null
616 let result: string[] = []
618 const localMention = `@(${actorNameAlphabet}+)`
619 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
621 const mentionRegex = this.isOwned()
622 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
623 : '(?:' + remoteMention + ')'
625 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
626 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
627 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
629 result = result.concat(
630 regexpCapture(this.text, firstMentionRegex)
631 .map(([ , username1, username2 ]) => username1 || username2),
633 regexpCapture(this.text, endMentionRegex)
634 .map(([ , username1, username2 ]) => username1 || username2),
636 regexpCapture(this.text, remoteMentionsRegex)
637 .map(([ , username ]) => username)
640 // Include local mentions
641 if (this.isOwned()) {
642 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
644 result = result.concat(
645 regexpCapture(this.text, localMentionsRegex)
646 .map(([ , username ]) => username)
653 toFormattedJSON (this: MCommentFormattable) {
658 threadId: this.getThreadId(),
659 inReplyToCommentId: this.inReplyToCommentId || null,
660 videoId: this.videoId,
661 createdAt: this.createdAt,
662 updatedAt: this.updatedAt,
663 deletedAt: this.deletedAt,
664 isDeleted: this.isDeleted(),
665 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
666 totalReplies: this.get('totalReplies') || 0,
667 account: this.Account ? this.Account.toFormattedJSON() : null
671 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
672 let inReplyTo: string
673 // New thread, so in AS we reply to the video
674 if (this.inReplyToCommentId === null) {
675 inReplyTo = this.Video.url
677 inReplyTo = this.InReplyToVideoComment.url
680 if (this.isDeleted()) {
686 published: this.createdAt.toISOString(),
687 updated: this.updatedAt.toISOString(),
688 deleted: this.deletedAt.toISOString()
692 const tag: ActivityTagObject[] = []
693 for (const parentComment of threadParentComments) {
694 if (!parentComment.Account) continue
696 const actor = parentComment.Account.Actor
701 name: `@${actor.preferredUsername}@${actor.getHost()}`
706 type: 'Note' as 'Note',
710 updated: this.updatedAt.toISOString(),
711 published: this.createdAt.toISOString(),
713 attributedTo: this.Account.Actor.url,
718 private static async buildBlockerAccountIds (options: {
720 isVideoOwned: boolean
721 user?: MUserAccountId
723 const { videoId, user, isVideoOwned } = options
725 const serverActor = await getServerActor()
726 const blockerAccountIds = [ serverActor.Account.id ]
728 if (user) blockerAccountIds.push(user.Account.id)
731 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
732 blockerAccountIds.push(videoOwnerAccount.id)
735 return blockerAccountIds