]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-comment.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
1 import { FindOptions, Op, Order, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
2 import {
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 ForeignKey,
9 HasMany,
10 Is,
11 Model,
12 Scopes,
13 Table,
14 UpdatedAt
15 } from 'sequelize-typescript'
16 import { exists } from '@server/helpers/custom-validators/misc'
17 import { getServerActor } from '@server/models/application/application'
18 import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
19 import { uniqify } from '@shared/core-utils'
20 import { VideoPrivacy } from '@shared/models'
21 import { AttributesOnly } from '@shared/typescript-utils'
22 import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
23 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
24 import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/comment/video-comment.model'
25 import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
26 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
27 import { regexpCapture } from '../../helpers/regexp'
28 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
29 import {
30 MComment,
31 MCommentAdminFormattable,
32 MCommentAP,
33 MCommentFormattable,
34 MCommentId,
35 MCommentOwner,
36 MCommentOwnerReplyVideoLight,
37 MCommentOwnerVideo,
38 MCommentOwnerVideoFeed,
39 MCommentOwnerVideoReply,
40 MVideoImmutable
41 } from '../../types/models/video'
42 import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
43 import { AccountModel } from '../account/account'
44 import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor'
45 import {
46 buildBlockedAccountSQL,
47 buildBlockedAccountSQLOptimized,
48 buildLocalAccountIdsIn,
49 getCommentSort,
50 searchAttribute,
51 throwIfNotValid
52 } from '../utils'
53 import { VideoModel } from './video'
54 import { VideoChannelModel } from './video-channel'
55
56 export enum ScopeNames {
57 WITH_ACCOUNT = 'WITH_ACCOUNT',
58 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
59 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
60 WITH_VIDEO = 'WITH_VIDEO',
61 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
62 }
63
64 @Scopes(() => ({
65 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
66 return {
67 attributes: {
68 include: [
69 [
70 Sequelize.literal(
71 '(' +
72 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
73 'SELECT COUNT("replies"."id") ' +
74 'FROM "videoComment" AS "replies" ' +
75 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
76 'AND "deletedAt" IS NULL ' +
77 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
78 ')'
79 ),
80 'totalReplies'
81 ],
82 [
83 Sequelize.literal(
84 '(' +
85 'SELECT COUNT("replies"."id") ' +
86 'FROM "videoComment" AS "replies" ' +
87 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
88 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
89 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
90 'AND "replies"."accountId" = "videoChannel"."accountId"' +
91 ')'
92 ),
93 'totalRepliesFromVideoAuthor'
94 ]
95 ]
96 }
97 } as FindOptions
98 },
99 [ScopeNames.WITH_ACCOUNT]: {
100 include: [
101 {
102 model: AccountModel
103 }
104 ]
105 },
106 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
107 include: [
108 {
109 model: AccountModel.unscoped(),
110 include: [
111 {
112 attributes: {
113 exclude: unusedActorAttributesForAPI
114 },
115 model: ActorModel, // Default scope includes avatar and server
116 required: true
117 }
118 ]
119 }
120 ]
121 },
122 [ScopeNames.WITH_IN_REPLY_TO]: {
123 include: [
124 {
125 model: VideoCommentModel,
126 as: 'InReplyToVideoComment'
127 }
128 ]
129 },
130 [ScopeNames.WITH_VIDEO]: {
131 include: [
132 {
133 model: VideoModel,
134 required: true,
135 include: [
136 {
137 model: VideoChannelModel,
138 required: true,
139 include: [
140 {
141 model: AccountModel,
142 required: true
143 }
144 ]
145 }
146 ]
147 }
148 ]
149 }
150 }))
151 @Table({
152 tableName: 'videoComment',
153 indexes: [
154 {
155 fields: [ 'videoId' ]
156 },
157 {
158 fields: [ 'videoId', 'originCommentId' ]
159 },
160 {
161 fields: [ 'url' ],
162 unique: true
163 },
164 {
165 fields: [ 'accountId' ]
166 },
167 {
168 fields: [
169 { name: 'createdAt', order: 'DESC' }
170 ]
171 }
172 ]
173 })
174 export class VideoCommentModel extends Model<Partial<AttributesOnly<VideoCommentModel>>> {
175 @CreatedAt
176 createdAt: Date
177
178 @UpdatedAt
179 updatedAt: Date
180
181 @AllowNull(true)
182 @Column(DataType.DATE)
183 deletedAt: Date
184
185 @AllowNull(false)
186 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
187 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
188 url: string
189
190 @AllowNull(false)
191 @Column(DataType.TEXT)
192 text: string
193
194 @ForeignKey(() => VideoCommentModel)
195 @Column
196 originCommentId: number
197
198 @BelongsTo(() => VideoCommentModel, {
199 foreignKey: {
200 name: 'originCommentId',
201 allowNull: true
202 },
203 as: 'OriginVideoComment',
204 onDelete: 'CASCADE'
205 })
206 OriginVideoComment: VideoCommentModel
207
208 @ForeignKey(() => VideoCommentModel)
209 @Column
210 inReplyToCommentId: number
211
212 @BelongsTo(() => VideoCommentModel, {
213 foreignKey: {
214 name: 'inReplyToCommentId',
215 allowNull: true
216 },
217 as: 'InReplyToVideoComment',
218 onDelete: 'CASCADE'
219 })
220 InReplyToVideoComment: VideoCommentModel | null
221
222 @ForeignKey(() => VideoModel)
223 @Column
224 videoId: number
225
226 @BelongsTo(() => VideoModel, {
227 foreignKey: {
228 allowNull: false
229 },
230 onDelete: 'CASCADE'
231 })
232 Video: VideoModel
233
234 @ForeignKey(() => AccountModel)
235 @Column
236 accountId: number
237
238 @BelongsTo(() => AccountModel, {
239 foreignKey: {
240 allowNull: true
241 },
242 onDelete: 'CASCADE'
243 })
244 Account: AccountModel
245
246 @HasMany(() => VideoCommentAbuseModel, {
247 foreignKey: {
248 name: 'videoCommentId',
249 allowNull: true
250 },
251 onDelete: 'set null'
252 })
253 CommentAbuses: VideoCommentAbuseModel[]
254
255 static loadById (id: number, t?: Transaction): Promise<MComment> {
256 const query: FindOptions = {
257 where: {
258 id
259 }
260 }
261
262 if (t !== undefined) query.transaction = t
263
264 return VideoCommentModel.findOne(query)
265 }
266
267 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> {
268 const query: FindOptions = {
269 where: {
270 id
271 }
272 }
273
274 if (t !== undefined) query.transaction = t
275
276 return VideoCommentModel
277 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
278 .findOne(query)
279 }
280
281 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> {
282 const query: FindOptions = {
283 where: {
284 url
285 }
286 }
287
288 if (t !== undefined) query.transaction = t
289
290 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
291 }
292
293 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> {
294 const query: FindOptions = {
295 where: {
296 url
297 },
298 include: [
299 {
300 attributes: [ 'id', 'url' ],
301 model: VideoModel.unscoped()
302 }
303 ]
304 }
305
306 if (t !== undefined) query.transaction = t
307
308 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
309 }
310
311 static listCommentsForApi (parameters: {
312 start: number
313 count: number
314 sort: string
315
316 onLocalVideo?: boolean
317 isLocal?: boolean
318 search?: string
319 searchAccount?: string
320 searchVideo?: string
321 }) {
322 const { start, count, sort, isLocal, search, searchAccount, searchVideo, onLocalVideo } = parameters
323
324 const where: WhereOptions = {
325 deletedAt: null
326 }
327
328 const whereAccount: WhereOptions = {}
329 const whereActor: WhereOptions = {}
330 const whereVideo: WhereOptions = {}
331
332 if (isLocal === true) {
333 Object.assign(whereActor, {
334 serverId: null
335 })
336 } else if (isLocal === false) {
337 Object.assign(whereActor, {
338 serverId: {
339 [Op.ne]: null
340 }
341 })
342 }
343
344 if (search) {
345 Object.assign(where, {
346 [Op.or]: [
347 searchAttribute(search, 'text'),
348 searchAttribute(search, '$Account.Actor.preferredUsername$'),
349 searchAttribute(search, '$Account.name$'),
350 searchAttribute(search, '$Video.name$')
351 ]
352 })
353 }
354
355 if (searchAccount) {
356 Object.assign(whereActor, {
357 [Op.or]: [
358 searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'),
359 searchAttribute(searchAccount, '$Account.name$')
360 ]
361 })
362 }
363
364 if (searchVideo) {
365 Object.assign(whereVideo, searchAttribute(searchVideo, 'name'))
366 }
367
368 if (exists(onLocalVideo)) {
369 Object.assign(whereVideo, { remote: !onLocalVideo })
370 }
371
372 const getQuery = (forCount: boolean) => {
373 return {
374 offset: start,
375 limit: count,
376 order: getCommentSort(sort),
377 where,
378 include: [
379 {
380 model: AccountModel.unscoped(),
381 required: true,
382 where: whereAccount,
383 include: [
384 {
385 attributes: {
386 exclude: unusedActorAttributesForAPI
387 },
388 model: forCount === true
389 ? ActorModel.unscoped() // Default scope includes avatar and server
390 : ActorModel,
391 required: true,
392 where: whereActor
393 }
394 ]
395 },
396 {
397 model: VideoModel.unscoped(),
398 required: true,
399 where: whereVideo
400 }
401 ]
402 }
403 }
404
405 return Promise.all([
406 VideoCommentModel.count(getQuery(true)),
407 VideoCommentModel.findAll(getQuery(false))
408 ]).then(([ total, data ]) => ({ total, data }))
409 }
410
411 static async listThreadsForApi (parameters: {
412 videoId: number
413 isVideoOwned: boolean
414 start: number
415 count: number
416 sort: string
417 user?: MUserAccountId
418 }) {
419 const { videoId, isVideoOwned, start, count, sort, user } = parameters
420
421 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
422
423 const accountBlockedWhere = {
424 accountId: {
425 [Op.notIn]: Sequelize.literal(
426 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
427 )
428 }
429 }
430
431 const queryList = {
432 offset: start,
433 limit: count,
434 order: getCommentSort(sort),
435 where: {
436 [Op.and]: [
437 {
438 videoId
439 },
440 {
441 inReplyToCommentId: null
442 },
443 {
444 [Op.or]: [
445 accountBlockedWhere,
446 {
447 accountId: null
448 }
449 ]
450 }
451 ]
452 }
453 }
454
455 const findScopesList: (string | ScopeOptions)[] = [
456 ScopeNames.WITH_ACCOUNT_FOR_API,
457 {
458 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
459 }
460 ]
461
462 const countScopesList: ScopeOptions[] = [
463 {
464 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
465 }
466 ]
467
468 const notDeletedQueryCount = {
469 where: {
470 videoId,
471 deletedAt: null,
472 ...accountBlockedWhere
473 }
474 }
475
476 return Promise.all([
477 VideoCommentModel.scope(findScopesList).findAll(queryList),
478 VideoCommentModel.scope(countScopesList).count(queryList),
479 VideoCommentModel.count(notDeletedQueryCount)
480 ]).then(([ rows, count, totalNotDeletedComments ]) => {
481 return { total: count, data: rows, totalNotDeletedComments }
482 })
483 }
484
485 static async listThreadCommentsForApi (parameters: {
486 videoId: number
487 isVideoOwned: boolean
488 threadId: number
489 user?: MUserAccountId
490 }) {
491 const { videoId, threadId, user, isVideoOwned } = parameters
492
493 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
494
495 const query = {
496 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
497 where: {
498 videoId,
499 [Op.and]: [
500 {
501 [Op.or]: [
502 { id: threadId },
503 { originCommentId: threadId }
504 ]
505 },
506 {
507 [Op.or]: [
508 {
509 accountId: {
510 [Op.notIn]: Sequelize.literal(
511 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
512 )
513 }
514 },
515 {
516 accountId: null
517 }
518 ]
519 }
520 ]
521 }
522 }
523
524 const scopes: any[] = [
525 ScopeNames.WITH_ACCOUNT_FOR_API,
526 {
527 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
528 }
529 ]
530
531 return Promise.all([
532 VideoCommentModel.count(query),
533 VideoCommentModel.scope(scopes).findAll(query)
534 ]).then(([ total, data ]) => ({ total, data }))
535 }
536
537 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> {
538 const query = {
539 order: [ [ 'createdAt', order ] ] as Order,
540 where: {
541 id: {
542 [Op.in]: Sequelize.literal('(' +
543 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
544 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
545 'UNION ' +
546 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
547 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
548 ') ' +
549 'SELECT id FROM children' +
550 ')'),
551 [Op.ne]: comment.id
552 }
553 },
554 transaction: t
555 }
556
557 return VideoCommentModel
558 .scope([ ScopeNames.WITH_ACCOUNT ])
559 .findAll(query)
560 }
561
562 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
563 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
564 videoId: video.id,
565 isVideoOwned: video.isOwned()
566 })
567
568 const query = {
569 order: [ [ 'createdAt', 'ASC' ] ] as Order,
570 offset: start,
571 limit: count,
572 where: {
573 videoId: video.id,
574 accountId: {
575 [Op.notIn]: Sequelize.literal(
576 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
577 )
578 }
579 },
580 transaction: t
581 }
582
583 return Promise.all([
584 VideoCommentModel.count(query),
585 VideoCommentModel.findAll<MComment>(query)
586 ]).then(([ total, data ]) => ({ total, data }))
587 }
588
589 static async listForFeed (parameters: {
590 start: number
591 count: number
592 videoId?: number
593 accountId?: number
594 videoChannelId?: number
595 }): Promise<MCommentOwnerVideoFeed[]> {
596 const serverActor = await getServerActor()
597 const { start, count, videoId, accountId, videoChannelId } = parameters
598
599 const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
600 '"VideoCommentModel"."accountId"',
601 [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
602 )
603
604 if (accountId) {
605 whereAnd.push({
606 accountId
607 })
608 }
609
610 const accountWhere = {
611 [Op.and]: whereAnd
612 }
613
614 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
615
616 const query = {
617 order: [ [ 'createdAt', 'DESC' ] ] as Order,
618 offset: start,
619 limit: count,
620 where: {
621 deletedAt: null,
622 accountId: accountWhere
623 },
624 include: [
625 {
626 attributes: [ 'name', 'uuid' ],
627 model: VideoModel.unscoped(),
628 required: true,
629 where: {
630 privacy: VideoPrivacy.PUBLIC
631 },
632 include: [
633 {
634 attributes: [ 'accountId' ],
635 model: VideoChannelModel.unscoped(),
636 required: true,
637 where: videoChannelWhere
638 }
639 ]
640 }
641 ]
642 }
643
644 if (videoId) query.where['videoId'] = videoId
645
646 return VideoCommentModel
647 .scope([ ScopeNames.WITH_ACCOUNT ])
648 .findAll(query)
649 }
650
651 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
652 const accountWhere = filter.onVideosOfAccount
653 ? { id: filter.onVideosOfAccount.id }
654 : {}
655
656 const query = {
657 limit: 1000,
658 where: {
659 deletedAt: null,
660 accountId: ofAccount.id
661 },
662 include: [
663 {
664 model: VideoModel,
665 required: true,
666 include: [
667 {
668 model: VideoChannelModel,
669 required: true,
670 include: [
671 {
672 model: AccountModel,
673 required: true,
674 where: accountWhere
675 }
676 ]
677 }
678 ]
679 }
680 ]
681 }
682
683 return VideoCommentModel
684 .scope([ ScopeNames.WITH_ACCOUNT ])
685 .findAll(query)
686 }
687
688 static async getStats () {
689 const totalLocalVideoComments = await VideoCommentModel.count({
690 include: [
691 {
692 model: AccountModel.unscoped(),
693 required: true,
694 include: [
695 {
696 model: ActorModel.unscoped(),
697 required: true,
698 where: {
699 serverId: null
700 }
701 }
702 ]
703 }
704 ]
705 })
706 const totalVideoComments = await VideoCommentModel.count()
707
708 return {
709 totalLocalVideoComments,
710 totalVideoComments
711 }
712 }
713
714 static listRemoteCommentUrlsOfLocalVideos () {
715 const query = `SELECT "videoComment".url FROM "videoComment" ` +
716 `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
717 `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
718 `INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
719
720 return VideoCommentModel.sequelize.query<{ url: string }>(query, {
721 type: QueryTypes.SELECT,
722 raw: true
723 }).then(rows => rows.map(r => r.url))
724 }
725
726 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
727 const query = {
728 where: {
729 updatedAt: {
730 [Op.lt]: beforeUpdatedAt
731 },
732 videoId,
733 accountId: {
734 [Op.notIn]: buildLocalAccountIdsIn()
735 },
736 // Do not delete Tombstones
737 deletedAt: null
738 }
739 }
740
741 return VideoCommentModel.destroy(query)
742 }
743
744 getCommentStaticPath () {
745 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
746 }
747
748 getThreadId (): number {
749 return this.originCommentId || this.id
750 }
751
752 isOwned () {
753 if (!this.Account) {
754 return false
755 }
756
757 return this.Account.isOwned()
758 }
759
760 markAsDeleted () {
761 this.text = ''
762 this.deletedAt = new Date()
763 this.accountId = null
764 }
765
766 isDeleted () {
767 return this.deletedAt !== null
768 }
769
770 extractMentions () {
771 let result: string[] = []
772
773 const localMention = `@(${actorNameAlphabet}+)`
774 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
775
776 const mentionRegex = this.isOwned()
777 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
778 : '(?:' + remoteMention + ')'
779
780 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
781 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
782 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
783
784 result = result.concat(
785 regexpCapture(this.text, firstMentionRegex)
786 .map(([ , username1, username2 ]) => username1 || username2),
787
788 regexpCapture(this.text, endMentionRegex)
789 .map(([ , username1, username2 ]) => username1 || username2),
790
791 regexpCapture(this.text, remoteMentionsRegex)
792 .map(([ , username ]) => username)
793 )
794
795 // Include local mentions
796 if (this.isOwned()) {
797 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
798
799 result = result.concat(
800 regexpCapture(this.text, localMentionsRegex)
801 .map(([ , username ]) => username)
802 )
803 }
804
805 return uniqify(result)
806 }
807
808 toFormattedJSON (this: MCommentFormattable) {
809 return {
810 id: this.id,
811 url: this.url,
812 text: this.text,
813
814 threadId: this.getThreadId(),
815 inReplyToCommentId: this.inReplyToCommentId || null,
816 videoId: this.videoId,
817
818 createdAt: this.createdAt,
819 updatedAt: this.updatedAt,
820 deletedAt: this.deletedAt,
821
822 isDeleted: this.isDeleted(),
823
824 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
825 totalReplies: this.get('totalReplies') || 0,
826
827 account: this.Account
828 ? this.Account.toFormattedJSON()
829 : null
830 } as VideoComment
831 }
832
833 toFormattedAdminJSON (this: MCommentAdminFormattable) {
834 return {
835 id: this.id,
836 url: this.url,
837 text: this.text,
838
839 threadId: this.getThreadId(),
840 inReplyToCommentId: this.inReplyToCommentId || null,
841 videoId: this.videoId,
842
843 createdAt: this.createdAt,
844 updatedAt: this.updatedAt,
845
846 video: {
847 id: this.Video.id,
848 uuid: this.Video.uuid,
849 name: this.Video.name
850 },
851
852 account: this.Account
853 ? this.Account.toFormattedJSON()
854 : null
855 } as VideoCommentAdmin
856 }
857
858 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
859 let inReplyTo: string
860 // New thread, so in AS we reply to the video
861 if (this.inReplyToCommentId === null) {
862 inReplyTo = this.Video.url
863 } else {
864 inReplyTo = this.InReplyToVideoComment.url
865 }
866
867 if (this.isDeleted()) {
868 return {
869 id: this.url,
870 type: 'Tombstone',
871 formerType: 'Note',
872 inReplyTo,
873 published: this.createdAt.toISOString(),
874 updated: this.updatedAt.toISOString(),
875 deleted: this.deletedAt.toISOString()
876 }
877 }
878
879 const tag: ActivityTagObject[] = []
880 for (const parentComment of threadParentComments) {
881 if (!parentComment.Account) continue
882
883 const actor = parentComment.Account.Actor
884
885 tag.push({
886 type: 'Mention',
887 href: actor.url,
888 name: `@${actor.preferredUsername}@${actor.getHost()}`
889 })
890 }
891
892 return {
893 type: 'Note' as 'Note',
894 id: this.url,
895
896 content: this.text,
897 mediaType: 'text/markdown',
898
899 inReplyTo,
900 updated: this.updatedAt.toISOString(),
901 published: this.createdAt.toISOString(),
902 url: this.url,
903 attributedTo: this.Account.Actor.url,
904 tag
905 }
906 }
907
908 private static async buildBlockerAccountIds (options: {
909 videoId: number
910 isVideoOwned: boolean
911 user?: MUserAccountId
912 }) {
913 const { videoId, user, isVideoOwned } = options
914
915 const serverActor = await getServerActor()
916 const blockerAccountIds = [ serverActor.Account.id ]
917
918 if (user) blockerAccountIds.push(user.Account.id)
919
920 if (isVideoOwned) {
921 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
922 if (videoOwnerAccount) blockerAccountIds.push(videoOwnerAccount.id)
923 }
924
925 return blockerAccountIds
926 }
927 }