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 })
417 const accountBlockedWhere = {
419 [Op.notIn]: Sequelize.literal(
420 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
428 order: getCommentSort(sort),
435 inReplyToCommentId: null
449 const scopesList: (string | ScopeOptions)[] = [
450 ScopeNames.WITH_ACCOUNT_FOR_API,
452 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
460 ...accountBlockedWhere
465 VideoCommentModel.scope(scopesList).findAndCountAll(queryList),
466 VideoCommentModel.count(queryCount)
467 ]).then(([ { rows, count }, totalNotDeletedComments ]) => {
468 return { total: count, data: rows, totalNotDeletedComments }
472 static async listThreadCommentsForApi (parameters: {
474 isVideoOwned: boolean
476 user?: MUserAccountId
478 const { videoId, threadId, user, isVideoOwned } = parameters
480 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
483 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
490 { originCommentId: threadId }
497 [Op.notIn]: Sequelize.literal(
498 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
511 const scopes: any[] = [
512 ScopeNames.WITH_ACCOUNT_FOR_API,
514 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
518 return VideoCommentModel.scope(scopes)
519 .findAndCountAll(query)
520 .then(({ rows, count }) => {
521 return { total: count, data: rows }
525 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
527 order: [ [ 'createdAt', order ] ] as Order,
530 [Op.in]: Sequelize.literal('(' +
531 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
532 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
534 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
535 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
537 'SELECT id FROM children' +
545 return VideoCommentModel
546 .scope([ ScopeNames.WITH_ACCOUNT ])
550 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
551 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
553 isVideoOwned: video.isOwned()
557 order: [ [ 'createdAt', 'ASC' ] ] as Order,
563 [Op.notIn]: Sequelize.literal(
564 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
571 return VideoCommentModel.findAndCountAll<MComment>(query)
574 static async listForFeed (parameters: {
579 videoChannelId?: number
580 }): Promise<MCommentOwnerVideoFeed[]> {
581 const serverActor = await getServerActor()
582 const { start, count, videoId, accountId, videoChannelId } = parameters
584 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
585 '"VideoCommentModel"."accountId"',
586 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
595 const accountWhere = {
599 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
602 order: [ [ 'createdAt', 'DESC' ] ] as Order,
607 accountId: accountWhere
611 attributes: [ 'name', 'uuid' ],
612 model: VideoModel.unscoped(),
615 privacy: VideoPrivacy.PUBLIC
619 attributes: [ 'accountId' ],
620 model: VideoChannelModel.unscoped(),
622 where: videoChannelWhere
629 if (videoId) query.where['videoId'] = videoId
631 return VideoCommentModel
632 .scope([ ScopeNames.WITH_ACCOUNT ])
636 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
637 const accountWhere = filter.onVideosOfAccount
638 ? { id: filter.onVideosOfAccount.id }
645 accountId: ofAccount.id
653 model: VideoChannelModel,
668 return VideoCommentModel
669 .scope([ ScopeNames.WITH_ACCOUNT ])
673 static async getStats () {
674 const totalLocalVideoComments = await VideoCommentModel.count({
691 const totalVideoComments = await VideoCommentModel.count()
694 totalLocalVideoComments,
699 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
703 [Op.lt]: beforeUpdatedAt
707 [Op.notIn]: buildLocalAccountIdsIn()
709 // Do not delete Tombstones
714 return VideoCommentModel.destroy(query)
717 getCommentStaticPath () {
718 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
721 getThreadId (): number {
722 return this.originCommentId || this.id
730 return this.Account.isOwned()
734 return this.deletedAt !== null
738 let result: string[] = []
740 const localMention = `@(${actorNameAlphabet}+)`
741 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
743 const mentionRegex = this.isOwned()
744 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
745 : '(?:' + remoteMention + ')'
747 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
748 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
749 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
751 result = result.concat(
752 regexpCapture(this.text, firstMentionRegex)
753 .map(([ , username1, username2 ]) => username1 || username2),
755 regexpCapture(this.text, endMentionRegex)
756 .map(([ , username1, username2 ]) => username1 || username2),
758 regexpCapture(this.text, remoteMentionsRegex)
759 .map(([ , username ]) => username)
762 // Include local mentions
763 if (this.isOwned()) {
764 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
766 result = result.concat(
767 regexpCapture(this.text, localMentionsRegex)
768 .map(([ , username ]) => username)
775 toFormattedJSON (this: MCommentFormattable) {
781 threadId: this.getThreadId(),
782 inReplyToCommentId: this.inReplyToCommentId || null,
783 videoId: this.videoId,
785 createdAt: this.createdAt,
786 updatedAt: this.updatedAt,
787 deletedAt: this.deletedAt,
789 isDeleted: this.isDeleted(),
791 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
792 totalReplies: this.get('totalReplies') || 0,
794 account: this.Account
795 ? this.Account.toFormattedJSON()
800 toFormattedAdminJSON (this: MCommentAdminFormattable) {
806 threadId: this.getThreadId(),
807 inReplyToCommentId: this.inReplyToCommentId || null,
808 videoId: this.videoId,
810 createdAt: this.createdAt,
811 updatedAt: this.updatedAt,
815 uuid: this.Video.uuid,
816 name: this.Video.name
819 account: this.Account
820 ? this.Account.toFormattedJSON()
822 } as VideoCommentAdmin
825 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
826 let inReplyTo: string
827 // New thread, so in AS we reply to the video
828 if (this.inReplyToCommentId === null) {
829 inReplyTo = this.Video.url
831 inReplyTo = this.InReplyToVideoComment.url
834 if (this.isDeleted()) {
840 published: this.createdAt.toISOString(),
841 updated: this.updatedAt.toISOString(),
842 deleted: this.deletedAt.toISOString()
846 const tag: ActivityTagObject[] = []
847 for (const parentComment of threadParentComments) {
848 if (!parentComment.Account) continue
850 const actor = parentComment.Account.Actor
855 name: `@${actor.preferredUsername}@${actor.getHost()}`
860 type: 'Note' as 'Note',
864 updated: this.updatedAt.toISOString(),
865 published: this.createdAt.toISOString(),
867 attributedTo: this.Account.Actor.url,
872 private static async buildBlockerAccountIds (options: {
874 isVideoOwned: boolean
875 user?: MUserAccountId
877 const { videoId, user, isVideoOwned } = options
879 const serverActor = await getServerActor()
880 const blockerAccountIds = [ serverActor.Account.id ]
882 if (user) blockerAccountIds.push(user.Account.id)
885 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
886 blockerAccountIds.push(videoOwnerAccount.id)
889 return blockerAccountIds