1 import * as Bluebird from 'bluebird'
2 import { uniq } from 'lodash'
3 import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
4 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
5 import { getServerActor } from '@server/models/application/application'
6 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
7 import { VideoPrivacy } from '@shared/models'
8 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
9 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
10 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
11 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
12 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13 import { regexpCapture } from '../../helpers/regexp'
14 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
21 MCommentOwnerReplyVideoLight,
23 MCommentOwnerVideoFeed,
24 MCommentOwnerVideoReply,
26 } from '../../types/models/video'
27 import { AccountModel } from '../account/account'
28 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
29 import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
30 import { VideoModel } from './video'
31 import { VideoChannelModel } from './video-channel'
34 WITH_ACCOUNT = 'WITH_ACCOUNT',
35 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
36 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
37 WITH_VIDEO = 'WITH_VIDEO',
38 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
42 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
49 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
50 'SELECT COUNT("replies"."id") - (' +
51 'SELECT COUNT("replies"."id") ' +
52 'FROM "videoComment" AS "replies" ' +
53 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
54 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
56 'FROM "videoComment" AS "replies" ' +
57 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
58 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
66 'SELECT COUNT("replies"."id") ' +
67 'FROM "videoComment" AS "replies" ' +
68 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
69 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
70 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
71 'AND "replies"."accountId" = "videoChannel"."accountId"' +
74 'totalRepliesFromVideoAuthor'
80 [ScopeNames.WITH_ACCOUNT]: {
87 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
90 model: AccountModel.unscoped(),
94 exclude: unusedActorAttributesForAPI
96 model: ActorModel, // Default scope includes avatar and server
103 [ScopeNames.WITH_IN_REPLY_TO]: {
106 model: VideoCommentModel,
107 as: 'InReplyToVideoComment'
111 [ScopeNames.WITH_VIDEO]: {
118 model: VideoChannelModel,
133 tableName: 'videoComment',
136 fields: [ 'videoId' ]
139 fields: [ 'videoId', 'originCommentId' ]
146 fields: [ 'accountId' ]
150 { name: 'createdAt', order: 'DESC' }
155 export class VideoCommentModel extends Model<VideoCommentModel> {
163 @Column(DataType.DATE)
167 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
168 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
172 @Column(DataType.TEXT)
175 @ForeignKey(() => VideoCommentModel)
177 originCommentId: number
179 @BelongsTo(() => VideoCommentModel, {
181 name: 'originCommentId',
184 as: 'OriginVideoComment',
187 OriginVideoComment: VideoCommentModel
189 @ForeignKey(() => VideoCommentModel)
191 inReplyToCommentId: number
193 @BelongsTo(() => VideoCommentModel, {
195 name: 'inReplyToCommentId',
198 as: 'InReplyToVideoComment',
201 InReplyToVideoComment: VideoCommentModel | null
203 @ForeignKey(() => VideoModel)
207 @BelongsTo(() => VideoModel, {
215 @ForeignKey(() => AccountModel)
219 @BelongsTo(() => AccountModel, {
225 Account: AccountModel
227 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
228 const query: FindOptions = {
234 if (t !== undefined) query.transaction = t
236 return VideoCommentModel.findOne(query)
239 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
240 const query: FindOptions = {
246 if (t !== undefined) query.transaction = t
248 return VideoCommentModel
249 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
253 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
254 const query: FindOptions = {
260 if (t !== undefined) query.transaction = t
262 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
265 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
266 const query: FindOptions = {
272 attributes: [ 'id', 'url' ],
273 model: VideoModel.unscoped()
278 if (t !== undefined) query.transaction = t
280 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
283 static async listThreadsForApi (parameters: {
285 isVideoOwned: boolean
289 user?: MUserAccountId
291 const { videoId, isVideoOwned, start, count, sort, user } = parameters
293 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
298 order: getCommentSort(sort),
305 inReplyToCommentId: null
311 [Op.notIn]: Sequelize.literal(
312 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
325 const scopes: (string | ScopeOptions)[] = [
326 ScopeNames.WITH_ACCOUNT_FOR_API,
328 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
332 return VideoCommentModel
334 .findAndCountAll(query)
335 .then(({ rows, count }) => {
336 return { total: count, data: rows }
340 static async listThreadCommentsForApi (parameters: {
342 isVideoOwned: boolean
344 user?: MUserAccountId
346 const { videoId, threadId, user, isVideoOwned } = parameters
348 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
351 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
356 { originCommentId: threadId }
359 [Op.notIn]: Sequelize.literal(
360 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
366 const scopes: any[] = [
367 ScopeNames.WITH_ACCOUNT_FOR_API,
369 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
373 return VideoCommentModel
375 .findAndCountAll(query)
376 .then(({ rows, count }) => {
377 return { total: count, data: rows }
381 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
383 order: [ [ 'createdAt', order ] ] as Order,
386 [Op.in]: Sequelize.literal('(' +
387 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
388 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
390 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
391 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
393 'SELECT id FROM children' +
401 return VideoCommentModel
402 .scope([ ScopeNames.WITH_ACCOUNT ])
406 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
407 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
409 isVideoOwned: video.isOwned()
413 order: [ [ 'createdAt', 'ASC' ] ] as Order,
419 [Op.notIn]: Sequelize.literal(
420 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
427 return VideoCommentModel.findAndCountAll<MComment>(query)
430 static async listForFeed (parameters: {
435 videoChannelId?: number
436 }): Promise<MCommentOwnerVideoFeed[]> {
437 const serverActor = await getServerActor()
438 const { start, count, videoId, accountId, videoChannelId } = parameters
440 const accountExclusion = {
441 [Op.notIn]: Sequelize.literal(
442 '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
445 const accountWhere = accountId
454 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
457 order: [ [ 'createdAt', 'DESC' ] ] as Order,
462 accountId: accountWhere
466 attributes: [ 'name', 'uuid' ],
467 model: VideoModel.unscoped(),
470 privacy: VideoPrivacy.PUBLIC
474 attributes: [ 'accountId' ],
475 model: VideoChannelModel.unscoped(),
477 where: videoChannelWhere
484 if (videoId) query.where['videoId'] = videoId
486 return VideoCommentModel
487 .scope([ ScopeNames.WITH_ACCOUNT ])
491 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
492 const accountWhere = filter.onVideosOfAccount
493 ? { id: filter.onVideosOfAccount.id }
500 accountId: ofAccount.id
508 model: VideoChannelModel,
523 return VideoCommentModel
524 .scope([ ScopeNames.WITH_ACCOUNT ])
528 static async getStats () {
529 const totalLocalVideoComments = await VideoCommentModel.count({
546 const totalVideoComments = await VideoCommentModel.count()
549 totalLocalVideoComments,
554 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
558 [Op.lt]: beforeUpdatedAt
562 [Op.notIn]: buildLocalAccountIdsIn()
564 // Do not delete Tombstones
569 return VideoCommentModel.destroy(query)
572 getCommentStaticPath () {
573 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
576 getThreadId (): number {
577 return this.originCommentId || this.id
585 return this.Account.isOwned()
589 return this.deletedAt !== null
593 let result: string[] = []
595 const localMention = `@(${actorNameAlphabet}+)`
596 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
598 const mentionRegex = this.isOwned()
599 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
600 : '(?:' + remoteMention + ')'
602 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
603 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
604 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
606 result = result.concat(
607 regexpCapture(this.text, firstMentionRegex)
608 .map(([ , username1, username2 ]) => username1 || username2),
610 regexpCapture(this.text, endMentionRegex)
611 .map(([ , username1, username2 ]) => username1 || username2),
613 regexpCapture(this.text, remoteMentionsRegex)
614 .map(([ , username ]) => username)
617 // Include local mentions
618 if (this.isOwned()) {
619 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
621 result = result.concat(
622 regexpCapture(this.text, localMentionsRegex)
623 .map(([ , username ]) => username)
630 toFormattedJSON (this: MCommentFormattable) {
635 threadId: this.originCommentId || this.id,
636 inReplyToCommentId: this.inReplyToCommentId || null,
637 videoId: this.videoId,
638 createdAt: this.createdAt,
639 updatedAt: this.updatedAt,
640 deletedAt: this.deletedAt,
641 isDeleted: this.isDeleted(),
642 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
643 totalReplies: this.get('totalReplies') || 0,
644 account: this.Account ? this.Account.toFormattedJSON() : null
648 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
649 let inReplyTo: string
650 // New thread, so in AS we reply to the video
651 if (this.inReplyToCommentId === null) {
652 inReplyTo = this.Video.url
654 inReplyTo = this.InReplyToVideoComment.url
657 if (this.isDeleted()) {
663 published: this.createdAt.toISOString(),
664 updated: this.updatedAt.toISOString(),
665 deleted: this.deletedAt.toISOString()
669 const tag: ActivityTagObject[] = []
670 for (const parentComment of threadParentComments) {
671 if (!parentComment.Account) continue
673 const actor = parentComment.Account.Actor
678 name: `@${actor.preferredUsername}@${actor.getHost()}`
683 type: 'Note' as 'Note',
687 updated: this.updatedAt.toISOString(),
688 published: this.createdAt.toISOString(),
690 attributedTo: this.Account.Actor.url,
695 private static async buildBlockerAccountIds (options: {
697 isVideoOwned: boolean
698 user?: MUserAccountId
700 const { videoId, user, isVideoOwned } = options
702 const serverActor = await getServerActor()
703 const blockerAccountIds = [ serverActor.Account.id ]
705 if (user) blockerAccountIds.push(user.Account.id)
708 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
709 blockerAccountIds.push(videoOwnerAccount.id)
712 return blockerAccountIds