1 import { uniq } from 'lodash'
2 import { FindAndCountOptions, 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 { 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 listRemoteCommentUrlsOfLocalVideos () {
700 const query = `SELECT "videoComment".url FROM "videoComment" ` +
701 `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
702 `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
703 `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
705 return VideoCommentModel.sequelize.query<{ url: string }>(query, {
706 type: QueryTypes.SELECT,
708 }).then(rows => rows.map(r => r.url))
711 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
715 [Op.lt]: beforeUpdatedAt
719 [Op.notIn]: buildLocalAccountIdsIn()
721 // Do not delete Tombstones
726 return VideoCommentModel.destroy(query)
729 getCommentStaticPath () {
730 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
733 getThreadId (): number {
734 return this.originCommentId || this.id
742 return this.Account.isOwned()
746 return this.deletedAt !== null
750 let result: string[] = []
752 const localMention = `@(${actorNameAlphabet}+)`
753 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
755 const mentionRegex = this.isOwned()
756 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
757 : '(?:' + remoteMention + ')'
759 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
760 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
761 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
763 result = result.concat(
764 regexpCapture(this.text, firstMentionRegex)
765 .map(([ , username1, username2 ]) => username1 || username2),
767 regexpCapture(this.text, endMentionRegex)
768 .map(([ , username1, username2 ]) => username1 || username2),
770 regexpCapture(this.text, remoteMentionsRegex)
771 .map(([ , username ]) => username)
774 // Include local mentions
775 if (this.isOwned()) {
776 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
778 result = result.concat(
779 regexpCapture(this.text, localMentionsRegex)
780 .map(([ , username ]) => username)
787 toFormattedJSON (this: MCommentFormattable) {
793 threadId: this.getThreadId(),
794 inReplyToCommentId: this.inReplyToCommentId || null,
795 videoId: this.videoId,
797 createdAt: this.createdAt,
798 updatedAt: this.updatedAt,
799 deletedAt: this.deletedAt,
801 isDeleted: this.isDeleted(),
803 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
804 totalReplies: this.get('totalReplies') || 0,
806 account: this.Account
807 ? this.Account.toFormattedJSON()
812 toFormattedAdminJSON (this: MCommentAdminFormattable) {
818 threadId: this.getThreadId(),
819 inReplyToCommentId: this.inReplyToCommentId || null,
820 videoId: this.videoId,
822 createdAt: this.createdAt,
823 updatedAt: this.updatedAt,
827 uuid: this.Video.uuid,
828 name: this.Video.name
831 account: this.Account
832 ? this.Account.toFormattedJSON()
834 } as VideoCommentAdmin
837 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
838 let inReplyTo: string
839 // New thread, so in AS we reply to the video
840 if (this.inReplyToCommentId === null) {
841 inReplyTo = this.Video.url
843 inReplyTo = this.InReplyToVideoComment.url
846 if (this.isDeleted()) {
852 published: this.createdAt.toISOString(),
853 updated: this.updatedAt.toISOString(),
854 deleted: this.deletedAt.toISOString()
858 const tag: ActivityTagObject[] = []
859 for (const parentComment of threadParentComments) {
860 if (!parentComment.Account) continue
862 const actor = parentComment.Account.Actor
867 name: `@${actor.preferredUsername}@${actor.getHost()}`
872 type: 'Note' as 'Note',
876 updated: this.updatedAt.toISOString(),
877 published: this.createdAt.toISOString(),
879 attributedTo: this.Account.Actor.url,
884 private static async buildBlockerAccountIds (options: {
886 isVideoOwned: boolean
887 user?: MUserAccountId
889 const { videoId, user, isVideoOwned } = options
891 const serverActor = await getServerActor()
892 const blockerAccountIds = [ serverActor.Account.id ]
894 if (user) blockerAccountIds.push(user.Account.id)
897 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
898 blockerAccountIds.push(videoOwnerAccount.id)
901 return blockerAccountIds