1 import { uniq } from 'lodash'
2 import { FindOptions, Op, Order, QueryTypes, 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 { AttributesOnly } from '@shared/typescript-utils'
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, VideoCommentAdmin } from '../../../shared/models/videos/comment/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'
30 MCommentAdminFormattable,
35 MCommentOwnerReplyVideoLight,
37 MCommentOwnerVideoFeed,
38 MCommentOwnerVideoReply,
40 } from '../../types/models/video'
41 import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
42 import { AccountModel } from '../account/account'
43 import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
45 buildBlockedAccountSQL,
46 buildBlockedAccountSQLOptimized,
47 buildLocalAccountIdsIn,
52 import { VideoModel } from './video'
53 import { VideoChannelModel } from './video-channel'
55 export enum ScopeNames {
56 WITH_ACCOUNT = 'WITH_ACCOUNT',
57 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
58 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
59 WITH_VIDEO = 'WITH_VIDEO',
60 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
64 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
71 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
72 'SELECT COUNT("replies"."id") ' +
73 'FROM "videoComment" AS "replies" ' +
74 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
75 'AND "deletedAt" IS NULL ' +
76 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
84 'SELECT COUNT("replies"."id") ' +
85 'FROM "videoComment" AS "replies" ' +
86 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
87 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
88 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
89 'AND "replies"."accountId" = "videoChannel"."accountId"' +
92 'totalRepliesFromVideoAuthor'
98 [ScopeNames.WITH_ACCOUNT]: {
105 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
108 model: AccountModel.unscoped(),
112 exclude: unusedActorAttributesForAPI
114 model: ActorModel, // Default scope includes avatar and server
121 [ScopeNames.WITH_IN_REPLY_TO]: {
124 model: VideoCommentModel,
125 as: 'InReplyToVideoComment'
129 [ScopeNames.WITH_VIDEO]: {
136 model: VideoChannelModel,
151 tableName: 'videoComment',
154 fields: [ 'videoId' ]
157 fields: [ 'videoId', 'originCommentId' ]
164 fields: [ 'accountId' ]
168 { name: 'createdAt', order: 'DESC' }
173 export class VideoCommentModel extends Model<Partial<AttributesOnly<VideoCommentModel>>> {
181 @Column(DataType.DATE)
185 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
186 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
190 @Column(DataType.TEXT)
193 @ForeignKey(() => VideoCommentModel)
195 originCommentId: number
197 @BelongsTo(() => VideoCommentModel, {
199 name: 'originCommentId',
202 as: 'OriginVideoComment',
205 OriginVideoComment: VideoCommentModel
207 @ForeignKey(() => VideoCommentModel)
209 inReplyToCommentId: number
211 @BelongsTo(() => VideoCommentModel, {
213 name: 'inReplyToCommentId',
216 as: 'InReplyToVideoComment',
219 InReplyToVideoComment: VideoCommentModel | null
221 @ForeignKey(() => VideoModel)
225 @BelongsTo(() => VideoModel, {
233 @ForeignKey(() => AccountModel)
237 @BelongsTo(() => AccountModel, {
243 Account: AccountModel
245 @HasMany(() => VideoCommentAbuseModel, {
247 name: 'videoCommentId',
252 CommentAbuses: VideoCommentAbuseModel[]
254 static loadById (id: number, t?: Transaction): Promise<MComment> {
255 const query: FindOptions = {
261 if (t !== undefined) query.transaction = t
263 return VideoCommentModel.findOne(query)
266 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> {
267 const query: FindOptions = {
273 if (t !== undefined) query.transaction = t
275 return VideoCommentModel
276 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
280 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> {
281 const query: FindOptions = {
287 if (t !== undefined) query.transaction = t
289 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
292 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> {
293 const query: FindOptions = {
299 attributes: [ 'id', 'url' ],
300 model: VideoModel.unscoped()
305 if (t !== undefined) query.transaction = t
307 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
310 static listCommentsForApi (parameters: {
317 searchAccount?: string
320 const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters
322 const where: WhereOptions = {
326 const whereAccount: WhereOptions = {}
327 const whereActor: WhereOptions = {}
328 const whereVideo: WhereOptions = {}
330 if (isLocal === true) {
331 Object.assign(whereActor, {
334 } else if (isLocal === false) {
335 Object.assign(whereActor, {
343 Object.assign(where, {
345 searchAttribute(search, 'text'),
346 searchAttribute(search, '$Account.Actor.preferredUsername$'),
347 searchAttribute(search, '$Account.name$'),
348 searchAttribute(search, '$Video.name$')
354 Object.assign(whereActor, {
356 searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
357 searchAttribute(searchAccount, '$Account.name$')
363 Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
366 const getQuery = (forCount: boolean) => {
370 order: getCommentSort(sort),
374 model: AccountModel.unscoped(),
380 exclude: unusedActorAttributesForAPI
382 model: forCount === true
383 ? ActorModel.unscoped() // Default scope includes avatar and server
391 model: VideoModel.unscoped(),
400 VideoCommentModel.count(getQuery(true)),
401 VideoCommentModel.findAll(getQuery(false))
402 ]).then(([ total, data ]) => ({ total, data }))
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 findScopesList: (string | ScopeOptions)[] = [
450 ScopeNames.WITH_ACCOUNT_FOR_API,
452 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
456 const countScopesList: ScopeOptions[] = [
458 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
462 const notDeletedQueryCount = {
466 ...accountBlockedWhere
471 VideoCommentModel.scope(findScopesList).findAll(queryList),
472 VideoCommentModel.scope(countScopesList).count(queryList),
473 VideoCommentModel.count(notDeletedQueryCount)
474 ]).then(([ rows, count, totalNotDeletedComments ]) => {
475 return { total: count, data: rows, totalNotDeletedComments }
479 static async listThreadCommentsForApi (parameters: {
481 isVideoOwned: boolean
483 user?: MUserAccountId
485 const { videoId, threadId, user, isVideoOwned } = parameters
487 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
490 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
497 { originCommentId: threadId }
504 [Op.notIn]: Sequelize.literal(
505 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
518 const scopes: any[] = [
519 ScopeNames.WITH_ACCOUNT_FOR_API,
521 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
526 VideoCommentModel.count(query),
527 VideoCommentModel.scope(scopes).findAll(query)
528 ]).then(([ total, data ]) => ({ total, data }))
531 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
533 order: [ [ 'createdAt', order ] ] as Order,
536 [Op.in]: Sequelize.literal('(' +
537 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
538 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
540 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
541 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
543 'SELECT id FROM children' +
551 return VideoCommentModel
552 .scope([ ScopeNames.WITH_ACCOUNT ])
556 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
557 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
559 isVideoOwned: video.isOwned()
563 order: [ [ 'createdAt', 'ASC' ] ] as Order,
569 [Op.notIn]: Sequelize.literal(
570 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
578 VideoCommentModel.count(query),
579 VideoCommentModel.findAll<MComment>(query)
580 ]).then(([ total, data ]) => ({ total, data }))
583 static async listForFeed (parameters: {
588 videoChannelId?: number
589 }): Promise<MCommentOwnerVideoFeed[]> {
590 const serverActor = await getServerActor()
591 const { start, count, videoId, accountId, videoChannelId } = parameters
593 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
594 '"VideoCommentModel"."accountId"',
595 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
604 const accountWhere = {
608 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
611 order: [ [ 'createdAt', 'DESC' ] ] as Order,
616 accountId: accountWhere
620 attributes: [ 'name', 'uuid' ],
621 model: VideoModel.unscoped(),
624 privacy: VideoPrivacy.PUBLIC
628 attributes: [ 'accountId' ],
629 model: VideoChannelModel.unscoped(),
631 where: videoChannelWhere
638 if (videoId) query.where['videoId'] = videoId
640 return VideoCommentModel
641 .scope([ ScopeNames.WITH_ACCOUNT ])
645 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
646 const accountWhere = filter.onVideosOfAccount
647 ? { id: filter.onVideosOfAccount.id }
654 accountId: ofAccount.id
662 model: VideoChannelModel,
677 return VideoCommentModel
678 .scope([ ScopeNames.WITH_ACCOUNT ])
682 static async getStats () {
683 const totalLocalVideoComments = await VideoCommentModel.count({
686 model: AccountModel.unscoped(),
690 model: ActorModel.unscoped(),
700 const totalVideoComments = await VideoCommentModel.count()
703 totalLocalVideoComments,
708 static listRemoteCommentUrlsOfLocalVideos () {
709 const query = `SELECT "videoComment".url FROM "videoComment" ` +
710 `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
711 `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
712 `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
714 return VideoCommentModel.sequelize.query<{ url: string }>(query, {
715 type: QueryTypes.SELECT,
717 }).then(rows => rows.map(r => r.url))
720 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
724 [Op.lt]: beforeUpdatedAt
728 [Op.notIn]: buildLocalAccountIdsIn()
730 // Do not delete Tombstones
735 return VideoCommentModel.destroy(query)
738 getCommentStaticPath () {
739 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
742 getThreadId (): number {
743 return this.originCommentId || this.id
751 return this.Account.isOwned()
756 this.deletedAt = new Date()
757 this.accountId = null
761 return this.deletedAt !== null
765 let result: string[] = []
767 const localMention = `@(${actorNameAlphabet}+)`
768 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
770 const mentionRegex = this.isOwned()
771 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
772 : '(?:' + remoteMention + ')'
774 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
775 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
776 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
778 result = result.concat(
779 regexpCapture(this.text, firstMentionRegex)
780 .map(([ , username1, username2 ]) => username1 || username2),
782 regexpCapture(this.text, endMentionRegex)
783 .map(([ , username1, username2 ]) => username1 || username2),
785 regexpCapture(this.text, remoteMentionsRegex)
786 .map(([ , username ]) => username)
789 // Include local mentions
790 if (this.isOwned()) {
791 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
793 result = result.concat(
794 regexpCapture(this.text, localMentionsRegex)
795 .map(([ , username ]) => username)
802 toFormattedJSON (this: MCommentFormattable) {
808 threadId: this.getThreadId(),
809 inReplyToCommentId: this.inReplyToCommentId || null,
810 videoId: this.videoId,
812 createdAt: this.createdAt,
813 updatedAt: this.updatedAt,
814 deletedAt: this.deletedAt,
816 isDeleted: this.isDeleted(),
818 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
819 totalReplies: this.get('totalReplies') || 0,
821 account: this.Account
822 ? this.Account.toFormattedJSON()
827 toFormattedAdminJSON (this: MCommentAdminFormattable) {
833 threadId: this.getThreadId(),
834 inReplyToCommentId: this.inReplyToCommentId || null,
835 videoId: this.videoId,
837 createdAt: this.createdAt,
838 updatedAt: this.updatedAt,
842 uuid: this.Video.uuid,
843 name: this.Video.name
846 account: this.Account
847 ? this.Account.toFormattedJSON()
849 } as VideoCommentAdmin
852 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
853 let inReplyTo: string
854 // New thread, so in AS we reply to the video
855 if (this.inReplyToCommentId === null) {
856 inReplyTo = this.Video.url
858 inReplyTo = this.InReplyToVideoComment.url
861 if (this.isDeleted()) {
867 published: this.createdAt.toISOString(),
868 updated: this.updatedAt.toISOString(),
869 deleted: this.deletedAt.toISOString()
873 const tag: ActivityTagObject[] = []
874 for (const parentComment of threadParentComments) {
875 if (!parentComment.Account) continue
877 const actor = parentComment.Account.Actor
882 name: `@${actor.preferredUsername}@${actor.getHost()}`
887 type: 'Note' as 'Note',
891 mediaType: 'text/markdown',
894 updated: this.updatedAt.toISOString(),
895 published: this.createdAt.toISOString(),
897 attributedTo: this.Account.Actor.url,
902 private static async buildBlockerAccountIds (options: {
904 isVideoOwned: boolean
905 user?: MUserAccountId
907 const { videoId, user, isVideoOwned } = options
909 const serverActor = await getServerActor()
910 const blockerAccountIds = [ serverActor.Account.id ]
912 if (user) blockerAccountIds.push(user.Account.id)
915 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
916 if (videoOwnerAccount) blockerAccountIds.push(videoOwnerAccount.id)
919 return blockerAccountIds