1 import * as Bluebird from 'bluebird'
2 import { uniq } from 'lodash'
3 import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
18 } from 'sequelize-typescript'
19 import { logger } from '@server/helpers/logger'
20 import { getServerActor } from '@server/models/application/application'
21 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
22 import { VideoPrivacy } from '@shared/models'
23 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
24 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
25 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
26 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
27 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
28 import { regexpCapture } from '../../helpers/regexp'
29 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
36 MCommentOwnerReplyVideoLight,
38 MCommentOwnerVideoFeed,
39 MCommentOwnerVideoReply,
41 } from '../../types/models/video'
42 import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
43 import { AccountModel } from '../account/account'
44 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
45 import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
46 import { VideoModel } from './video'
47 import { VideoChannelModel } from './video-channel'
50 WITH_ACCOUNT = 'WITH_ACCOUNT',
51 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
52 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
53 WITH_VIDEO = 'WITH_VIDEO',
54 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
58 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
65 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
66 'SELECT COUNT("replies"."id") - (' +
67 'SELECT COUNT("replies"."id") ' +
68 'FROM "videoComment" AS "replies" ' +
69 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
70 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
72 'FROM "videoComment" AS "replies" ' +
73 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
74 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
82 'SELECT COUNT("replies"."id") ' +
83 'FROM "videoComment" AS "replies" ' +
84 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
85 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
86 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
87 'AND "replies"."accountId" = "videoChannel"."accountId"' +
90 'totalRepliesFromVideoAuthor'
96 [ScopeNames.WITH_ACCOUNT]: {
103 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
106 model: AccountModel.unscoped(),
110 exclude: unusedActorAttributesForAPI
112 model: ActorModel, // Default scope includes avatar and server
119 [ScopeNames.WITH_IN_REPLY_TO]: {
122 model: VideoCommentModel,
123 as: 'InReplyToVideoComment'
127 [ScopeNames.WITH_VIDEO]: {
134 model: VideoChannelModel,
149 tableName: 'videoComment',
152 fields: [ 'videoId' ]
155 fields: [ 'videoId', 'originCommentId' ]
162 fields: [ 'accountId' ]
166 { name: 'createdAt', order: 'DESC' }
171 export class VideoCommentModel extends Model<VideoCommentModel> {
179 @Column(DataType.DATE)
183 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
184 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
188 @Column(DataType.TEXT)
191 @ForeignKey(() => VideoCommentModel)
193 originCommentId: number
195 @BelongsTo(() => VideoCommentModel, {
197 name: 'originCommentId',
200 as: 'OriginVideoComment',
203 OriginVideoComment: VideoCommentModel
205 @ForeignKey(() => VideoCommentModel)
207 inReplyToCommentId: number
209 @BelongsTo(() => VideoCommentModel, {
211 name: 'inReplyToCommentId',
214 as: 'InReplyToVideoComment',
217 InReplyToVideoComment: VideoCommentModel | null
219 @ForeignKey(() => VideoModel)
223 @BelongsTo(() => VideoModel, {
231 @ForeignKey(() => AccountModel)
235 @BelongsTo(() => AccountModel, {
241 Account: AccountModel
243 @HasMany(() => VideoCommentAbuseModel, {
250 CommentAbuses: VideoCommentAbuseModel[]
253 static async saveEssentialDataToAbuses (instance: VideoCommentModel, options) {
254 const tasks: Promise<any>[] = []
256 if (!Array.isArray(instance.CommentAbuses)) {
257 instance.CommentAbuses = await instance.$get('CommentAbuses')
259 if (instance.CommentAbuses.length === 0) return undefined
262 if (!instance.Video) {
263 instance.Video = await instance.$get('Video')
266 logger.info('Saving video comment %s for abuse.', instance.url)
268 const details = Object.assign(instance.toFormattedJSON(), {
270 id: instance.Video.id,
271 name: instance.Video.name,
272 uuid: instance.Video.uuid
276 for (const abuse of instance.CommentAbuses) {
277 abuse.deletedComment = details
279 tasks.push(abuse.save({ transaction: options.transaction }))
284 logger.error('Some errors when saving details of comment %s in its abuses before destroy hook.', instance.url, { err })
290 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
291 const query: FindOptions = {
297 if (t !== undefined) query.transaction = t
299 return VideoCommentModel.findOne(query)
302 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
303 const query: FindOptions = {
309 if (t !== undefined) query.transaction = t
311 return VideoCommentModel
312 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
316 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
317 const query: FindOptions = {
323 if (t !== undefined) query.transaction = t
325 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
328 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
329 const query: FindOptions = {
335 attributes: [ 'id', 'url' ],
336 model: VideoModel.unscoped()
341 if (t !== undefined) query.transaction = t
343 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
346 static async listThreadsForApi (parameters: {
348 isVideoOwned: boolean
352 user?: MUserAccountId
354 const { videoId, isVideoOwned, start, count, sort, user } = parameters
356 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
361 order: getCommentSort(sort),
368 inReplyToCommentId: null
374 [Op.notIn]: Sequelize.literal(
375 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
388 const scopes: (string | ScopeOptions)[] = [
389 ScopeNames.WITH_ACCOUNT_FOR_API,
391 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
395 return VideoCommentModel
397 .findAndCountAll(query)
398 .then(({ rows, count }) => {
399 return { total: count, data: rows }
403 static async listThreadCommentsForApi (parameters: {
405 isVideoOwned: boolean
407 user?: MUserAccountId
409 const { videoId, threadId, user, isVideoOwned } = parameters
411 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
414 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
419 { originCommentId: threadId }
422 [Op.notIn]: Sequelize.literal(
423 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
429 const scopes: any[] = [
430 ScopeNames.WITH_ACCOUNT_FOR_API,
432 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
436 return VideoCommentModel
438 .findAndCountAll(query)
439 .then(({ rows, count }) => {
440 return { total: count, data: rows }
444 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
446 order: [ [ 'createdAt', order ] ] as Order,
449 [Op.in]: Sequelize.literal('(' +
450 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
451 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
453 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
454 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
456 'SELECT id FROM children' +
464 return VideoCommentModel
465 .scope([ ScopeNames.WITH_ACCOUNT ])
469 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
470 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
472 isVideoOwned: video.isOwned()
476 order: [ [ 'createdAt', 'ASC' ] ] as Order,
482 [Op.notIn]: Sequelize.literal(
483 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
490 return VideoCommentModel.findAndCountAll<MComment>(query)
493 static async listForFeed (parameters: {
498 videoChannelId?: number
499 }): Promise<MCommentOwnerVideoFeed[]> {
500 const serverActor = await getServerActor()
501 const { start, count, videoId, accountId, videoChannelId } = parameters
503 const accountExclusion = {
504 [Op.notIn]: Sequelize.literal(
505 '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
508 const accountWhere = accountId
517 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
520 order: [ [ 'createdAt', 'DESC' ] ] as Order,
525 accountId: accountWhere
529 attributes: [ 'name', 'uuid' ],
530 model: VideoModel.unscoped(),
533 privacy: VideoPrivacy.PUBLIC
537 attributes: [ 'accountId' ],
538 model: VideoChannelModel.unscoped(),
540 where: videoChannelWhere
547 if (videoId) query.where['videoId'] = videoId
549 return VideoCommentModel
550 .scope([ ScopeNames.WITH_ACCOUNT ])
554 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
555 const accountWhere = filter.onVideosOfAccount
556 ? { id: filter.onVideosOfAccount.id }
563 accountId: ofAccount.id
571 model: VideoChannelModel,
586 return VideoCommentModel
587 .scope([ ScopeNames.WITH_ACCOUNT ])
591 static async getStats () {
592 const totalLocalVideoComments = await VideoCommentModel.count({
609 const totalVideoComments = await VideoCommentModel.count()
612 totalLocalVideoComments,
617 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
621 [Op.lt]: beforeUpdatedAt
625 [Op.notIn]: buildLocalAccountIdsIn()
627 // Do not delete Tombstones
632 return VideoCommentModel.destroy(query)
635 getCommentStaticPath () {
636 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
639 getThreadId (): number {
640 return this.originCommentId || this.id
648 return this.Account.isOwned()
652 return this.deletedAt !== null
656 let result: string[] = []
658 const localMention = `@(${actorNameAlphabet}+)`
659 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
661 const mentionRegex = this.isOwned()
662 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
663 : '(?:' + remoteMention + ')'
665 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
666 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
667 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
669 result = result.concat(
670 regexpCapture(this.text, firstMentionRegex)
671 .map(([ , username1, username2 ]) => username1 || username2),
673 regexpCapture(this.text, endMentionRegex)
674 .map(([ , username1, username2 ]) => username1 || username2),
676 regexpCapture(this.text, remoteMentionsRegex)
677 .map(([ , username ]) => username)
680 // Include local mentions
681 if (this.isOwned()) {
682 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
684 result = result.concat(
685 regexpCapture(this.text, localMentionsRegex)
686 .map(([ , username ]) => username)
693 toFormattedJSON (this: MCommentFormattable) {
698 threadId: this.originCommentId || this.id,
699 inReplyToCommentId: this.inReplyToCommentId || null,
700 videoId: this.videoId,
701 createdAt: this.createdAt,
702 updatedAt: this.updatedAt,
703 deletedAt: this.deletedAt,
704 isDeleted: this.isDeleted(),
705 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
706 totalReplies: this.get('totalReplies') || 0,
707 account: this.Account ? this.Account.toFormattedJSON() : null
711 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
712 let inReplyTo: string
713 // New thread, so in AS we reply to the video
714 if (this.inReplyToCommentId === null) {
715 inReplyTo = this.Video.url
717 inReplyTo = this.InReplyToVideoComment.url
720 if (this.isDeleted()) {
726 published: this.createdAt.toISOString(),
727 updated: this.updatedAt.toISOString(),
728 deleted: this.deletedAt.toISOString()
732 const tag: ActivityTagObject[] = []
733 for (const parentComment of threadParentComments) {
734 if (!parentComment.Account) continue
736 const actor = parentComment.Account.Actor
741 name: `@${actor.preferredUsername}@${actor.getHost()}`
746 type: 'Note' as 'Note',
750 updated: this.updatedAt.toISOString(),
751 published: this.createdAt.toISOString(),
753 attributedTo: this.Account.Actor.url,
758 private static async buildBlockerAccountIds (options: {
760 isVideoOwned: boolean
761 user?: MUserAccountId
763 const { videoId, user, isVideoOwned } = options
765 const serverActor = await getServerActor()
766 const blockerAccountIds = [ serverActor.Account.id ]
768 if (user) blockerAccountIds.push(user.Account.id)
771 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
772 blockerAccountIds.push(videoOwnerAccount.id)
775 return blockerAccountIds