1 import * as Bluebird from 'bluebird'
2 import { uniq } from 'lodash'
3 import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { getServerActor } from '@server/models/application/application'
19 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
20 import { VideoPrivacy } from '@shared/models'
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/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 '../activitypub/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 'SELECT COUNT("replies"."id") ' +
74 'FROM "videoComment" AS "replies" ' +
75 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
76 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
78 'FROM "videoComment" AS "replies" ' +
79 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
80 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
88 'SELECT COUNT("replies"."id") ' +
89 'FROM "videoComment" AS "replies" ' +
90 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
91 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
92 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
93 'AND "replies"."accountId" = "videoChannel"."accountId"' +
96 'totalRepliesFromVideoAuthor'
102 [ScopeNames.WITH_ACCOUNT]: {
109 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
112 model: AccountModel.unscoped(),
116 exclude: unusedActorAttributesForAPI
118 model: ActorModel, // Default scope includes avatar and server
125 [ScopeNames.WITH_IN_REPLY_TO]: {
128 model: VideoCommentModel,
129 as: 'InReplyToVideoComment'
133 [ScopeNames.WITH_VIDEO]: {
140 model: VideoChannelModel,
155 tableName: 'videoComment',
158 fields: [ 'videoId' ]
161 fields: [ 'videoId', 'originCommentId' ]
168 fields: [ 'accountId' ]
172 { name: 'createdAt', order: 'DESC' }
177 export class VideoCommentModel extends Model<VideoCommentModel> {
185 @Column(DataType.DATE)
189 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
190 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
194 @Column(DataType.TEXT)
197 @ForeignKey(() => VideoCommentModel)
199 originCommentId: number
201 @BelongsTo(() => VideoCommentModel, {
203 name: 'originCommentId',
206 as: 'OriginVideoComment',
209 OriginVideoComment: VideoCommentModel
211 @ForeignKey(() => VideoCommentModel)
213 inReplyToCommentId: number
215 @BelongsTo(() => VideoCommentModel, {
217 name: 'inReplyToCommentId',
220 as: 'InReplyToVideoComment',
223 InReplyToVideoComment: VideoCommentModel | null
225 @ForeignKey(() => VideoModel)
229 @BelongsTo(() => VideoModel, {
237 @ForeignKey(() => AccountModel)
241 @BelongsTo(() => AccountModel, {
247 Account: AccountModel
249 @HasMany(() => VideoCommentAbuseModel, {
251 name: 'videoCommentId',
256 CommentAbuses: VideoCommentAbuseModel[]
258 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
259 const query: FindOptions = {
265 if (t !== undefined) query.transaction = t
267 return VideoCommentModel.findOne(query)
270 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
271 const query: FindOptions = {
277 if (t !== undefined) query.transaction = t
279 return VideoCommentModel
280 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
284 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
285 const query: FindOptions = {
291 if (t !== undefined) query.transaction = t
293 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
296 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
297 const query: FindOptions = {
303 attributes: [ 'id', 'url' ],
304 model: VideoModel.unscoped()
309 if (t !== undefined) query.transaction = t
311 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
314 static listCommentsForApi (parameters: {
321 searchAccount?: string
324 const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters
326 const where: WhereOptions = {
330 const whereAccount: WhereOptions = {}
331 const whereActor: WhereOptions = {}
332 const whereVideo: WhereOptions = {}
334 if (isLocal === true) {
335 Object.assign(whereActor, {
338 } else if (isLocal === false) {
339 Object.assign(whereActor, {
347 Object.assign(where, {
349 searchAttribute(search, 'text'),
350 searchAttribute(search, '$Account.Actor.preferredUsername$'),
351 searchAttribute(search, '$Account.name$'),
352 searchAttribute(search, '$Video.name$')
358 Object.assign(whereActor, {
360 searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
361 searchAttribute(searchAccount, '$Account.name$')
367 Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
370 const query: FindAndCountOptions = {
373 order: getCommentSort(sort),
377 model: AccountModel.unscoped(),
383 exclude: unusedActorAttributesForAPI
385 model: ActorModel, // Default scope includes avatar and server
392 model: VideoModel.unscoped(),
399 return VideoCommentModel
400 .findAndCountAll(query)
401 .then(({ rows, count }) => {
402 return { total: count, data: rows }
406 static async listThreadsForApi (parameters: {
408 isVideoOwned: boolean
412 user?: MUserAccountId
414 const { videoId, isVideoOwned, start, count, sort, user } = parameters
416 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
421 order: getCommentSort(sort),
428 inReplyToCommentId: null
434 [Op.notIn]: Sequelize.literal(
435 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
448 const scopes: (string | ScopeOptions)[] = [
449 ScopeNames.WITH_ACCOUNT_FOR_API,
451 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
455 return VideoCommentModel
457 .findAndCountAll(query)
458 .then(({ rows, count }) => {
459 return { total: count, data: rows }
463 static async listThreadCommentsForApi (parameters: {
465 isVideoOwned: boolean
467 user?: MUserAccountId
469 const { videoId, threadId, user, isVideoOwned } = parameters
471 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
474 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
479 { originCommentId: threadId }
482 [Op.notIn]: Sequelize.literal(
483 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
489 const scopes: any[] = [
490 ScopeNames.WITH_ACCOUNT_FOR_API,
492 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
496 return VideoCommentModel
498 .findAndCountAll(query)
499 .then(({ rows, count }) => {
500 return { total: count, data: rows }
504 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
506 order: [ [ 'createdAt', order ] ] as Order,
509 [Op.in]: Sequelize.literal('(' +
510 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
511 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
513 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
514 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
516 'SELECT id FROM children' +
524 return VideoCommentModel
525 .scope([ ScopeNames.WITH_ACCOUNT ])
529 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
530 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
532 isVideoOwned: video.isOwned()
536 order: [ [ 'createdAt', 'ASC' ] ] as Order,
542 [Op.notIn]: Sequelize.literal(
543 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
550 return VideoCommentModel.findAndCountAll<MComment>(query)
553 static async listForFeed (parameters: {
558 videoChannelId?: number
559 }): Promise<MCommentOwnerVideoFeed[]> {
560 const serverActor = await getServerActor()
561 const { start, count, videoId, accountId, videoChannelId } = parameters
563 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
564 '"VideoCommentModel"."accountId"',
565 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
574 const accountWhere = {
578 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
581 order: [ [ 'createdAt', 'DESC' ] ] as Order,
586 accountId: accountWhere
590 attributes: [ 'name', 'uuid' ],
591 model: VideoModel.unscoped(),
594 privacy: VideoPrivacy.PUBLIC
598 attributes: [ 'accountId' ],
599 model: VideoChannelModel.unscoped(),
601 where: videoChannelWhere
608 if (videoId) query.where['videoId'] = videoId
610 return VideoCommentModel
611 .scope([ ScopeNames.WITH_ACCOUNT ])
615 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
616 const accountWhere = filter.onVideosOfAccount
617 ? { id: filter.onVideosOfAccount.id }
624 accountId: ofAccount.id
632 model: VideoChannelModel,
647 return VideoCommentModel
648 .scope([ ScopeNames.WITH_ACCOUNT ])
652 static async getStats () {
653 const totalLocalVideoComments = await VideoCommentModel.count({
670 const totalVideoComments = await VideoCommentModel.count()
673 totalLocalVideoComments,
678 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
682 [Op.lt]: beforeUpdatedAt
686 [Op.notIn]: buildLocalAccountIdsIn()
688 // Do not delete Tombstones
693 return VideoCommentModel.destroy(query)
696 getCommentStaticPath () {
697 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
700 getThreadId (): number {
701 return this.originCommentId || this.id
709 return this.Account.isOwned()
713 return this.deletedAt !== null
717 let result: string[] = []
719 const localMention = `@(${actorNameAlphabet}+)`
720 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
722 const mentionRegex = this.isOwned()
723 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
724 : '(?:' + remoteMention + ')'
726 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
727 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
728 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
730 result = result.concat(
731 regexpCapture(this.text, firstMentionRegex)
732 .map(([ , username1, username2 ]) => username1 || username2),
734 regexpCapture(this.text, endMentionRegex)
735 .map(([ , username1, username2 ]) => username1 || username2),
737 regexpCapture(this.text, remoteMentionsRegex)
738 .map(([ , username ]) => username)
741 // Include local mentions
742 if (this.isOwned()) {
743 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
745 result = result.concat(
746 regexpCapture(this.text, localMentionsRegex)
747 .map(([ , username ]) => username)
754 toFormattedJSON (this: MCommentFormattable) {
760 threadId: this.getThreadId(),
761 inReplyToCommentId: this.inReplyToCommentId || null,
762 videoId: this.videoId,
764 createdAt: this.createdAt,
765 updatedAt: this.updatedAt,
766 deletedAt: this.deletedAt,
768 isDeleted: this.isDeleted(),
770 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
771 totalReplies: this.get('totalReplies') || 0,
773 account: this.Account
774 ? this.Account.toFormattedJSON()
779 toFormattedAdminJSON (this: MCommentAdminFormattable) {
785 threadId: this.getThreadId(),
786 inReplyToCommentId: this.inReplyToCommentId || null,
787 videoId: this.videoId,
789 createdAt: this.createdAt,
790 updatedAt: this.updatedAt,
794 uuid: this.Video.uuid,
795 name: this.Video.name
798 account: this.Account
799 ? this.Account.toFormattedJSON()
801 } as VideoCommentAdmin
804 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
805 let inReplyTo: string
806 // New thread, so in AS we reply to the video
807 if (this.inReplyToCommentId === null) {
808 inReplyTo = this.Video.url
810 inReplyTo = this.InReplyToVideoComment.url
813 if (this.isDeleted()) {
819 published: this.createdAt.toISOString(),
820 updated: this.updatedAt.toISOString(),
821 deleted: this.deletedAt.toISOString()
825 const tag: ActivityTagObject[] = []
826 for (const parentComment of threadParentComments) {
827 if (!parentComment.Account) continue
829 const actor = parentComment.Account.Actor
834 name: `@${actor.preferredUsername}@${actor.getHost()}`
839 type: 'Note' as 'Note',
843 updated: this.updatedAt.toISOString(),
844 published: this.createdAt.toISOString(),
846 attributedTo: this.Account.Actor.url,
851 private static async buildBlockerAccountIds (options: {
853 isVideoOwned: boolean
854 user?: MUserAccountId
856 const { videoId, user, isVideoOwned } = options
858 const serverActor = await getServerActor()
859 const blockerAccountIds = [ serverActor.Account.id ]
861 if (user) blockerAccountIds.push(user.Account.id)
864 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
865 blockerAccountIds.push(videoOwnerAccount.id)
868 return blockerAccountIds