1 import { FindOptions, Op, Order, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
15 } from 'sequelize-typescript'
16 import { exists } from '@server/helpers/custom-validators/misc'
17 import { getServerActor } from '@server/models/application/application'
18 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
19 import { uniqify } from '@shared/core-utils'
20 import { VideoPrivacy } from '@shared/models'
21 import { AttributesOnly } from '@shared/typescript-utils'
22 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
23 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
24 import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/comment/video-comment.model'
25 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
26 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
27 import { regexpCapture } from '../../helpers/regexp'
28 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
31 MCommentAdminFormattable,
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 '../actor/actor'
46 buildBlockedAccountSQL,
47 buildBlockedAccountSQLOptimized,
48 buildLocalAccountIdsIn,
53 import { VideoModel } from './video'
54 import { VideoChannelModel } from './video-channel'
56 export enum ScopeNames {
57 WITH_ACCOUNT = 'WITH_ACCOUNT',
58 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
59 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
60 WITH_VIDEO = 'WITH_VIDEO',
61 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
65 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
72 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
73 'SELECT COUNT("replies"."id") ' +
74 'FROM "videoComment" AS "replies" ' +
75 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
76 'AND "deletedAt" IS NULL ' +
77 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
85 'SELECT COUNT("replies"."id") ' +
86 'FROM "videoComment" AS "replies" ' +
87 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
88 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
89 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
90 'AND "replies"."accountId" = "videoChannel"."accountId"' +
93 'totalRepliesFromVideoAuthor'
99 [ScopeNames.WITH_ACCOUNT]: {
106 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
109 model: AccountModel.unscoped(),
113 exclude: unusedActorAttributesForAPI
115 model: ActorModel, // Default scope includes avatar and server
122 [ScopeNames.WITH_IN_REPLY_TO]: {
125 model: VideoCommentModel,
126 as: 'InReplyToVideoComment'
130 [ScopeNames.WITH_VIDEO]: {
137 model: VideoChannelModel,
152 tableName: 'videoComment',
155 fields: [ 'videoId' ]
158 fields: [ 'videoId', 'originCommentId' ]
165 fields: [ 'accountId' ]
169 { name: 'createdAt', order: 'DESC' }
174 export class VideoCommentModel extends Model<Partial<AttributesOnly<VideoCommentModel>>> {
182 @Column(DataType.DATE)
186 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
187 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
191 @Column(DataType.TEXT)
194 @ForeignKey(() => VideoCommentModel)
196 originCommentId: number
198 @BelongsTo(() => VideoCommentModel, {
200 name: 'originCommentId',
203 as: 'OriginVideoComment',
206 OriginVideoComment: VideoCommentModel
208 @ForeignKey(() => VideoCommentModel)
210 inReplyToCommentId: number
212 @BelongsTo(() => VideoCommentModel, {
214 name: 'inReplyToCommentId',
217 as: 'InReplyToVideoComment',
220 InReplyToVideoComment: VideoCommentModel | null
222 @ForeignKey(() => VideoModel)
226 @BelongsTo(() => VideoModel, {
234 @ForeignKey(() => AccountModel)
238 @BelongsTo(() => AccountModel, {
244 Account: AccountModel
246 @HasMany(() => VideoCommentAbuseModel, {
248 name: 'videoCommentId',
253 CommentAbuses: VideoCommentAbuseModel[]
255 static loadById (id: number, t?: Transaction): Promise<MComment> {
256 const query: FindOptions = {
262 if (t !== undefined) query.transaction = t
264 return VideoCommentModel.findOne(query)
267 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> {
268 const query: FindOptions = {
274 if (t !== undefined) query.transaction = t
276 return VideoCommentModel
277 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
281 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> {
282 const query: FindOptions = {
288 if (t !== undefined) query.transaction = t
290 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
293 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> {
294 const query: FindOptions = {
300 attributes: [ 'id', 'url' ],
301 model: VideoModel.unscoped()
306 if (t !== undefined) query.transaction = t
308 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
311 static listCommentsForApi (parameters: {
316 onLocalVideo?: boolean
319 searchAccount?: string
322 const { start, count, sort, isLocal, search, searchAccount, searchVideo, onLocalVideo } = parameters
324 const where: WhereOptions = {
328 const whereAccount: WhereOptions = {}
329 const whereActor: WhereOptions = {}
330 const whereVideo: WhereOptions = {}
332 if (isLocal === true) {
333 Object.assign(whereActor, {
336 } else if (isLocal === false) {
337 Object.assign(whereActor, {
345 Object.assign(where, {
347 searchAttribute(search, 'text'),
348 searchAttribute(search, '$Account.Actor.preferredUsername$'),
349 searchAttribute(search, '$Account.name$'),
350 searchAttribute(search, '$Video.name$')
356 Object.assign(whereActor, {
358 searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
359 searchAttribute(searchAccount, '$Account.name$')
365 Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
368 if (exists(onLocalVideo)) {
369 Object.assign(whereVideo, { remote: !onLocalVideo })
372 const getQuery = (forCount: boolean) => {
376 order: getCommentSort(sort),
380 model: AccountModel.unscoped(),
386 exclude: unusedActorAttributesForAPI
388 model: forCount === true
389 ? ActorModel.unscoped() // Default scope includes avatar and server
397 model: VideoModel.unscoped(),
406 VideoCommentModel.count(getQuery(true)),
407 VideoCommentModel.findAll(getQuery(false))
408 ]).then(([ total, data ]) => ({ total, data }))
411 static async listThreadsForApi (parameters: {
413 isVideoOwned: boolean
417 user?: MUserAccountId
419 const { videoId, isVideoOwned, start, count, sort, user } = parameters
421 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
423 const accountBlockedWhere = {
425 [Op.notIn]: Sequelize.literal(
426 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
434 order: getCommentSort(sort),
441 inReplyToCommentId: null
455 const findScopesList: (string | ScopeOptions)[] = [
456 ScopeNames.WITH_ACCOUNT_FOR_API,
458 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
462 const countScopesList: ScopeOptions[] = [
464 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
468 const notDeletedQueryCount = {
472 ...accountBlockedWhere
477 VideoCommentModel.scope(findScopesList).findAll(queryList),
478 VideoCommentModel.scope(countScopesList).count(queryList),
479 VideoCommentModel.count(notDeletedQueryCount)
480 ]).then(([ rows, count, totalNotDeletedComments ]) => {
481 return { total: count, data: rows, totalNotDeletedComments }
485 static async listThreadCommentsForApi (parameters: {
487 isVideoOwned: boolean
489 user?: MUserAccountId
491 const { videoId, threadId, user, isVideoOwned } = parameters
493 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
496 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
503 { originCommentId: threadId }
510 [Op.notIn]: Sequelize.literal(
511 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
524 const scopes: any[] = [
525 ScopeNames.WITH_ACCOUNT_FOR_API,
527 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
532 VideoCommentModel.count(query),
533 VideoCommentModel.scope(scopes).findAll(query)
534 ]).then(([ total, data ]) => ({ total, data }))
537 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
539 order: [ [ 'createdAt', order ] ] as Order,
542 [Op.in]: Sequelize.literal('(' +
543 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
544 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
546 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
547 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
549 'SELECT id FROM children' +
557 return VideoCommentModel
558 .scope([ ScopeNames.WITH_ACCOUNT ])
562 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
563 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
565 isVideoOwned: video.isOwned()
569 order: [ [ 'createdAt', 'ASC' ] ] as Order,
575 [Op.notIn]: Sequelize.literal(
576 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
584 VideoCommentModel.count(query),
585 VideoCommentModel.findAll<MComment>(query)
586 ]).then(([ total, data ]) => ({ total, data }))
589 static async listForFeed (parameters: {
594 videoChannelId?: number
595 }): Promise<MCommentOwnerVideoFeed[]> {
596 const serverActor = await getServerActor()
597 const { start, count, videoId, accountId, videoChannelId } = parameters
599 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
600 '"VideoCommentModel"."accountId"',
601 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
610 const accountWhere = {
614 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
617 order: [ [ 'createdAt', 'DESC' ] ] as Order,
622 accountId: accountWhere
626 attributes: [ 'name', 'uuid' ],
627 model: VideoModel.unscoped(),
630 privacy: VideoPrivacy.PUBLIC
634 attributes: [ 'accountId' ],
635 model: VideoChannelModel.unscoped(),
637 where: videoChannelWhere
644 if (videoId) query.where['videoId'] = videoId
646 return VideoCommentModel
647 .scope([ ScopeNames.WITH_ACCOUNT ])
651 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
652 const accountWhere = filter.onVideosOfAccount
653 ? { id: filter.onVideosOfAccount.id }
660 accountId: ofAccount.id
668 model: VideoChannelModel,
683 return VideoCommentModel
684 .scope([ ScopeNames.WITH_ACCOUNT ])
688 static async getStats () {
689 const totalLocalVideoComments = await VideoCommentModel.count({
692 model: AccountModel.unscoped(),
696 model: ActorModel.unscoped(),
706 const totalVideoComments = await VideoCommentModel.count()
709 totalLocalVideoComments,
714 static listRemoteCommentUrlsOfLocalVideos () {
715 const query = `SELECT "videoComment".url FROM "videoComment" ` +
716 `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
717 `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
718 `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
720 return VideoCommentModel.sequelize.query<{ url: string }>(query, {
721 type: QueryTypes.SELECT,
723 }).then(rows => rows.map(r => r.url))
726 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
730 [Op.lt]: beforeUpdatedAt
734 [Op.notIn]: buildLocalAccountIdsIn()
736 // Do not delete Tombstones
741 return VideoCommentModel.destroy(query)
744 getCommentStaticPath () {
745 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
748 getThreadId (): number {
749 return this.originCommentId || this.id
757 return this.Account.isOwned()
762 this.deletedAt = new Date()
763 this.accountId = null
767 return this.deletedAt !== null
771 let result: string[] = []
773 const localMention = `@(${actorNameAlphabet}+)`
774 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
776 const mentionRegex = this.isOwned()
777 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
778 : '(?:' + remoteMention + ')'
780 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
781 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
782 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
784 result = result.concat(
785 regexpCapture(this.text, firstMentionRegex)
786 .map(([ , username1, username2 ]) => username1 || username2),
788 regexpCapture(this.text, endMentionRegex)
789 .map(([ , username1, username2 ]) => username1 || username2),
791 regexpCapture(this.text, remoteMentionsRegex)
792 .map(([ , username ]) => username)
795 // Include local mentions
796 if (this.isOwned()) {
797 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
799 result = result.concat(
800 regexpCapture(this.text, localMentionsRegex)
801 .map(([ , username ]) => username)
805 return uniqify(result)
808 toFormattedJSON (this: MCommentFormattable) {
814 threadId: this.getThreadId(),
815 inReplyToCommentId: this.inReplyToCommentId || null,
816 videoId: this.videoId,
818 createdAt: this.createdAt,
819 updatedAt: this.updatedAt,
820 deletedAt: this.deletedAt,
822 isDeleted: this.isDeleted(),
824 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
825 totalReplies: this.get('totalReplies') || 0,
827 account: this.Account
828 ? this.Account.toFormattedJSON()
833 toFormattedAdminJSON (this: MCommentAdminFormattable) {
839 threadId: this.getThreadId(),
840 inReplyToCommentId: this.inReplyToCommentId || null,
841 videoId: this.videoId,
843 createdAt: this.createdAt,
844 updatedAt: this.updatedAt,
848 uuid: this.Video.uuid,
849 name: this.Video.name
852 account: this.Account
853 ? this.Account.toFormattedJSON()
855 } as VideoCommentAdmin
858 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
859 let inReplyTo: string
860 // New thread, so in AS we reply to the video
861 if (this.inReplyToCommentId === null) {
862 inReplyTo = this.Video.url
864 inReplyTo = this.InReplyToVideoComment.url
867 if (this.isDeleted()) {
873 published: this.createdAt.toISOString(),
874 updated: this.updatedAt.toISOString(),
875 deleted: this.deletedAt.toISOString()
879 const tag: ActivityTagObject[] = []
880 for (const parentComment of threadParentComments) {
881 if (!parentComment.Account) continue
883 const actor = parentComment.Account.Actor
888 name: `@${actor.preferredUsername}@${actor.getHost()}`
893 type: 'Note' as 'Note',
897 mediaType: 'text/markdown',
900 updated: this.updatedAt.toISOString(),
901 published: this.createdAt.toISOString(),
903 attributedTo: this.Account.Actor.url,
908 private static async buildBlockerAccountIds (options: {
910 isVideoOwned: boolean
911 user?: MUserAccountId
913 const { videoId, user, isVideoOwned } = options
915 const serverActor = await getServerActor()
916 const blockerAccountIds = [ serverActor.Account.id ]
918 if (user) blockerAccountIds.push(user.Account.id)
921 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
922 if (videoOwnerAccount) blockerAccountIds.push(videoOwnerAccount.id)
925 return blockerAccountIds