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,
488 { originCommentId: threadId }
493 [Op.notIn]: Sequelize.literal(
494 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
505 const scopes: any[] = [
506 ScopeNames.WITH_ACCOUNT_FOR_API,
508 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
512 return VideoCommentModel.scope(scopes)
513 .findAndCountAll(query)
514 .then(({ rows, count }) => {
515 return { total: count, data: rows }
519 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
521 order: [ [ 'createdAt', order ] ] as Order,
524 [Op.in]: Sequelize.literal('(' +
525 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
526 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
528 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
529 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
531 'SELECT id FROM children' +
539 return VideoCommentModel
540 .scope([ ScopeNames.WITH_ACCOUNT ])
544 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
545 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
547 isVideoOwned: video.isOwned()
551 order: [ [ 'createdAt', 'ASC' ] ] as Order,
557 [Op.notIn]: Sequelize.literal(
558 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
565 return VideoCommentModel.findAndCountAll<MComment>(query)
568 static async listForFeed (parameters: {
573 videoChannelId?: number
574 }): Promise<MCommentOwnerVideoFeed[]> {
575 const serverActor = await getServerActor()
576 const { start, count, videoId, accountId, videoChannelId } = parameters
578 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
579 '"VideoCommentModel"."accountId"',
580 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
589 const accountWhere = {
593 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
596 order: [ [ 'createdAt', 'DESC' ] ] as Order,
601 accountId: accountWhere
605 attributes: [ 'name', 'uuid' ],
606 model: VideoModel.unscoped(),
609 privacy: VideoPrivacy.PUBLIC
613 attributes: [ 'accountId' ],
614 model: VideoChannelModel.unscoped(),
616 where: videoChannelWhere
623 if (videoId) query.where['videoId'] = videoId
625 return VideoCommentModel
626 .scope([ ScopeNames.WITH_ACCOUNT ])
630 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
631 const accountWhere = filter.onVideosOfAccount
632 ? { id: filter.onVideosOfAccount.id }
639 accountId: ofAccount.id
647 model: VideoChannelModel,
662 return VideoCommentModel
663 .scope([ ScopeNames.WITH_ACCOUNT ])
667 static async getStats () {
668 const totalLocalVideoComments = await VideoCommentModel.count({
685 const totalVideoComments = await VideoCommentModel.count()
688 totalLocalVideoComments,
693 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
697 [Op.lt]: beforeUpdatedAt
701 [Op.notIn]: buildLocalAccountIdsIn()
703 // Do not delete Tombstones
708 return VideoCommentModel.destroy(query)
711 getCommentStaticPath () {
712 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
715 getThreadId (): number {
716 return this.originCommentId || this.id
724 return this.Account.isOwned()
728 return this.deletedAt !== null
732 let result: string[] = []
734 const localMention = `@(${actorNameAlphabet}+)`
735 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
737 const mentionRegex = this.isOwned()
738 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
739 : '(?:' + remoteMention + ')'
741 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
742 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
743 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
745 result = result.concat(
746 regexpCapture(this.text, firstMentionRegex)
747 .map(([ , username1, username2 ]) => username1 || username2),
749 regexpCapture(this.text, endMentionRegex)
750 .map(([ , username1, username2 ]) => username1 || username2),
752 regexpCapture(this.text, remoteMentionsRegex)
753 .map(([ , username ]) => username)
756 // Include local mentions
757 if (this.isOwned()) {
758 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
760 result = result.concat(
761 regexpCapture(this.text, localMentionsRegex)
762 .map(([ , username ]) => username)
769 toFormattedJSON (this: MCommentFormattable) {
775 threadId: this.getThreadId(),
776 inReplyToCommentId: this.inReplyToCommentId || null,
777 videoId: this.videoId,
779 createdAt: this.createdAt,
780 updatedAt: this.updatedAt,
781 deletedAt: this.deletedAt,
783 isDeleted: this.isDeleted(),
785 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
786 totalReplies: this.get('totalReplies') || 0,
788 account: this.Account
789 ? this.Account.toFormattedJSON()
794 toFormattedAdminJSON (this: MCommentAdminFormattable) {
800 threadId: this.getThreadId(),
801 inReplyToCommentId: this.inReplyToCommentId || null,
802 videoId: this.videoId,
804 createdAt: this.createdAt,
805 updatedAt: this.updatedAt,
809 uuid: this.Video.uuid,
810 name: this.Video.name
813 account: this.Account
814 ? this.Account.toFormattedJSON()
816 } as VideoCommentAdmin
819 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
820 let inReplyTo: string
821 // New thread, so in AS we reply to the video
822 if (this.inReplyToCommentId === null) {
823 inReplyTo = this.Video.url
825 inReplyTo = this.InReplyToVideoComment.url
828 if (this.isDeleted()) {
834 published: this.createdAt.toISOString(),
835 updated: this.updatedAt.toISOString(),
836 deleted: this.deletedAt.toISOString()
840 const tag: ActivityTagObject[] = []
841 for (const parentComment of threadParentComments) {
842 if (!parentComment.Account) continue
844 const actor = parentComment.Account.Actor
849 name: `@${actor.preferredUsername}@${actor.getHost()}`
854 type: 'Note' as 'Note',
858 updated: this.updatedAt.toISOString(),
859 published: this.createdAt.toISOString(),
861 attributedTo: this.Account.Actor.url,
866 private static async buildBlockerAccountIds (options: {
868 isVideoOwned: boolean
869 user?: MUserAccountId
871 const { videoId, user, isVideoOwned } = options
873 const serverActor = await getServerActor()
874 const blockerAccountIds = [ serverActor.Account.id ]
876 if (user) blockerAccountIds.push(user.Account.id)
879 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
880 blockerAccountIds.push(videoOwnerAccount.id)
883 return blockerAccountIds