1 import { uniq } from 'lodash'
2 import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
16 } from 'sequelize-typescript'
17 import { getServerActor } from '@server/models/application/application'
18 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
19 import { VideoPrivacy } from '@shared/models'
20 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
21 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
22 import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/video-comment.model'
23 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
24 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
25 import { regexpCapture } from '../../helpers/regexp'
26 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
29 MCommentAdminFormattable,
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'
44 buildBlockedAccountSQL,
45 buildBlockedAccountSQLOptimized,
46 buildLocalAccountIdsIn,
51 import { VideoModel } from './video'
52 import { VideoChannelModel } from './video-channel'
54 export enum ScopeNames {
55 WITH_ACCOUNT = 'WITH_ACCOUNT',
56 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
57 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
58 WITH_VIDEO = 'WITH_VIDEO',
59 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
63 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
70 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
71 'SELECT COUNT("replies"."id") - (' +
72 'SELECT COUNT("replies"."id") ' +
73 'FROM "videoComment" AS "replies" ' +
74 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
75 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
77 'FROM "videoComment" AS "replies" ' +
78 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
79 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
87 'SELECT COUNT("replies"."id") ' +
88 'FROM "videoComment" AS "replies" ' +
89 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
90 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
91 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
92 'AND "replies"."accountId" = "videoChannel"."accountId"' +
95 'totalRepliesFromVideoAuthor'
101 [ScopeNames.WITH_ACCOUNT]: {
108 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
111 model: AccountModel.unscoped(),
115 exclude: unusedActorAttributesForAPI
117 model: ActorModel, // Default scope includes avatar and server
124 [ScopeNames.WITH_IN_REPLY_TO]: {
127 model: VideoCommentModel,
128 as: 'InReplyToVideoComment'
132 [ScopeNames.WITH_VIDEO]: {
139 model: VideoChannelModel,
154 tableName: 'videoComment',
157 fields: [ 'videoId' ]
160 fields: [ 'videoId', 'originCommentId' ]
167 fields: [ 'accountId' ]
171 { name: 'createdAt', order: 'DESC' }
176 export class VideoCommentModel extends Model {
184 @Column(DataType.DATE)
188 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
189 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
193 @Column(DataType.TEXT)
196 @ForeignKey(() => VideoCommentModel)
198 originCommentId: number
200 @BelongsTo(() => VideoCommentModel, {
202 name: 'originCommentId',
205 as: 'OriginVideoComment',
208 OriginVideoComment: VideoCommentModel
210 @ForeignKey(() => VideoCommentModel)
212 inReplyToCommentId: number
214 @BelongsTo(() => VideoCommentModel, {
216 name: 'inReplyToCommentId',
219 as: 'InReplyToVideoComment',
222 InReplyToVideoComment: VideoCommentModel | null
224 @ForeignKey(() => VideoModel)
228 @BelongsTo(() => VideoModel, {
236 @ForeignKey(() => AccountModel)
240 @BelongsTo(() => AccountModel, {
246 Account: AccountModel
248 @HasMany(() => VideoCommentAbuseModel, {
250 name: 'videoCommentId',
255 CommentAbuses: VideoCommentAbuseModel[]
257 static loadById (id: number, t?: Transaction): Promise<MComment> {
258 const query: FindOptions = {
264 if (t !== undefined) query.transaction = t
266 return VideoCommentModel.findOne(query)
269 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> {
270 const query: FindOptions = {
276 if (t !== undefined) query.transaction = t
278 return VideoCommentModel
279 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
283 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> {
284 const query: FindOptions = {
290 if (t !== undefined) query.transaction = t
292 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
295 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> {
296 const query: FindOptions = {
302 attributes: [ 'id', 'url' ],
303 model: VideoModel.unscoped()
308 if (t !== undefined) query.transaction = t
310 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
313 static listCommentsForApi (parameters: {
320 searchAccount?: string
323 const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters
325 const where: WhereOptions = {
329 const whereAccount: WhereOptions = {}
330 const whereActor: WhereOptions = {}
331 const whereVideo: WhereOptions = {}
333 if (isLocal === true) {
334 Object.assign(whereActor, {
337 } else if (isLocal === false) {
338 Object.assign(whereActor, {
346 Object.assign(where, {
348 searchAttribute(search, 'text'),
349 searchAttribute(search, '$Account.Actor.preferredUsername$'),
350 searchAttribute(search, '$Account.name$'),
351 searchAttribute(search, '$Video.name$')
357 Object.assign(whereActor, {
359 searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
360 searchAttribute(searchAccount, '$Account.name$')
366 Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
369 const query: FindAndCountOptions = {
372 order: getCommentSort(sort),
376 model: AccountModel.unscoped(),
382 exclude: unusedActorAttributesForAPI
384 model: ActorModel, // Default scope includes avatar and server
391 model: VideoModel.unscoped(),
398 return VideoCommentModel
399 .findAndCountAll(query)
400 .then(({ rows, count }) => {
401 return { total: count, data: rows }
405 static async listThreadsForApi (parameters: {
407 isVideoOwned: boolean
411 user?: MUserAccountId
413 const { videoId, isVideoOwned, start, count, sort, user } = parameters
415 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
420 order: getCommentSort(sort),
427 inReplyToCommentId: null
433 [Op.notIn]: Sequelize.literal(
434 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
447 const scopes: (string | ScopeOptions)[] = [
448 ScopeNames.WITH_ACCOUNT_FOR_API,
450 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
454 return VideoCommentModel
456 .findAndCountAll(query)
457 .then(({ rows, count }) => {
458 return { total: count, data: rows }
462 static async listThreadCommentsForApi (parameters: {
464 isVideoOwned: boolean
466 user?: MUserAccountId
468 const { videoId, threadId, user, isVideoOwned } = parameters
470 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
473 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
478 { originCommentId: threadId }
481 [Op.notIn]: Sequelize.literal(
482 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
488 const scopes: any[] = [
489 ScopeNames.WITH_ACCOUNT_FOR_API,
491 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
495 return VideoCommentModel
497 .findAndCountAll(query)
498 .then(({ rows, count }) => {
499 return { total: count, data: rows }
503 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
505 order: [ [ 'createdAt', order ] ] as Order,
508 [Op.in]: Sequelize.literal('(' +
509 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
510 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
512 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
513 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
515 'SELECT id FROM children' +
523 return VideoCommentModel
524 .scope([ ScopeNames.WITH_ACCOUNT ])
528 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
529 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
531 isVideoOwned: video.isOwned()
535 order: [ [ 'createdAt', 'ASC' ] ] as Order,
541 [Op.notIn]: Sequelize.literal(
542 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
549 return VideoCommentModel.findAndCountAll<MComment>(query)
552 static async listForFeed (parameters: {
557 videoChannelId?: number
558 }): Promise<MCommentOwnerVideoFeed[]> {
559 const serverActor = await getServerActor()
560 const { start, count, videoId, accountId, videoChannelId } = parameters
562 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
563 '"VideoCommentModel"."accountId"',
564 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
573 const accountWhere = {
577 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
580 order: [ [ 'createdAt', 'DESC' ] ] as Order,
585 accountId: accountWhere
589 attributes: [ 'name', 'uuid' ],
590 model: VideoModel.unscoped(),
593 privacy: VideoPrivacy.PUBLIC
597 attributes: [ 'accountId' ],
598 model: VideoChannelModel.unscoped(),
600 where: videoChannelWhere
607 if (videoId) query.where['videoId'] = videoId
609 return VideoCommentModel
610 .scope([ ScopeNames.WITH_ACCOUNT ])
614 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
615 const accountWhere = filter.onVideosOfAccount
616 ? { id: filter.onVideosOfAccount.id }
623 accountId: ofAccount.id
631 model: VideoChannelModel,
646 return VideoCommentModel
647 .scope([ ScopeNames.WITH_ACCOUNT ])
651 static async getStats () {
652 const totalLocalVideoComments = await VideoCommentModel.count({
669 const totalVideoComments = await VideoCommentModel.count()
672 totalLocalVideoComments,
677 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
681 [Op.lt]: beforeUpdatedAt
685 [Op.notIn]: buildLocalAccountIdsIn()
687 // Do not delete Tombstones
692 return VideoCommentModel.destroy(query)
695 getCommentStaticPath () {
696 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
699 getThreadId (): number {
700 return this.originCommentId || this.id
708 return this.Account.isOwned()
712 return this.deletedAt !== null
716 let result: string[] = []
718 const localMention = `@(${actorNameAlphabet}+)`
719 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
721 const mentionRegex = this.isOwned()
722 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
723 : '(?:' + remoteMention + ')'
725 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
726 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
727 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
729 result = result.concat(
730 regexpCapture(this.text, firstMentionRegex)
731 .map(([ , username1, username2 ]) => username1 || username2),
733 regexpCapture(this.text, endMentionRegex)
734 .map(([ , username1, username2 ]) => username1 || username2),
736 regexpCapture(this.text, remoteMentionsRegex)
737 .map(([ , username ]) => username)
740 // Include local mentions
741 if (this.isOwned()) {
742 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
744 result = result.concat(
745 regexpCapture(this.text, localMentionsRegex)
746 .map(([ , username ]) => username)
753 toFormattedJSON (this: MCommentFormattable) {
759 threadId: this.getThreadId(),
760 inReplyToCommentId: this.inReplyToCommentId || null,
761 videoId: this.videoId,
763 createdAt: this.createdAt,
764 updatedAt: this.updatedAt,
765 deletedAt: this.deletedAt,
767 isDeleted: this.isDeleted(),
769 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
770 totalReplies: this.get('totalReplies') || 0,
772 account: this.Account
773 ? this.Account.toFormattedJSON()
778 toFormattedAdminJSON (this: MCommentAdminFormattable) {
784 threadId: this.getThreadId(),
785 inReplyToCommentId: this.inReplyToCommentId || null,
786 videoId: this.videoId,
788 createdAt: this.createdAt,
789 updatedAt: this.updatedAt,
793 uuid: this.Video.uuid,
794 name: this.Video.name
797 account: this.Account
798 ? this.Account.toFormattedJSON()
800 } as VideoCommentAdmin
803 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
804 let inReplyTo: string
805 // New thread, so in AS we reply to the video
806 if (this.inReplyToCommentId === null) {
807 inReplyTo = this.Video.url
809 inReplyTo = this.InReplyToVideoComment.url
812 if (this.isDeleted()) {
818 published: this.createdAt.toISOString(),
819 updated: this.updatedAt.toISOString(),
820 deleted: this.deletedAt.toISOString()
824 const tag: ActivityTagObject[] = []
825 for (const parentComment of threadParentComments) {
826 if (!parentComment.Account) continue
828 const actor = parentComment.Account.Actor
833 name: `@${actor.preferredUsername}@${actor.getHost()}`
838 type: 'Note' as 'Note',
842 updated: this.updatedAt.toISOString(),
843 published: this.createdAt.toISOString(),
845 attributedTo: this.Account.Actor.url,
850 private static async buildBlockerAccountIds (options: {
852 isVideoOwned: boolean
853 user?: MUserAccountId
855 const { videoId, user, isVideoOwned } = options
857 const serverActor = await getServerActor()
858 const blockerAccountIds = [ serverActor.Account.id ]
860 if (user) blockerAccountIds.push(user.Account.id)
863 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
864 blockerAccountIds.push(videoOwnerAccount.id)
867 return blockerAccountIds