]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-comment.ts
Handle playlist oembed
[github/Chocobozzz/PeerTube.git] / server / models / video / video-comment.ts
CommitLineData
444c0a0e
C
1import * as Bluebird from 'bluebird'
2import { uniq } from 'lodash'
3import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
57f6896f
C
4import {
5 AllowNull,
57f6896f
C
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 ForeignKey,
11 HasMany,
12 Is,
13 Model,
14 Scopes,
15 Table,
16 UpdatedAt
17} from 'sequelize-typescript'
444c0a0e 18import { getServerActor } from '@server/models/application/application'
26d6bf65 19import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
444c0a0e 20import { VideoPrivacy } from '@shared/models'
69222afa 21import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
ea44f375 22import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
bf1f6508 23import { VideoComment } from '../../../shared/models/videos/video-comment.model'
f7cc67b4 24import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
444c0a0e 25import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
f7cc67b4 26import { regexpCapture } from '../../helpers/regexp'
444c0a0e 27import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
453e83ea
C
28import {
29 MComment,
b5fecbf4 30 MCommentAP,
1ca9f7c3 31 MCommentFormattable,
453e83ea
C
32 MCommentId,
33 MCommentOwner,
34 MCommentOwnerReplyVideoLight,
35 MCommentOwnerVideo,
36 MCommentOwnerVideoFeed,
696d83fd
C
37 MCommentOwnerVideoReply,
38 MVideoImmutable
26d6bf65 39} from '../../types/models/video'
57f6896f 40import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
444c0a0e 41import { AccountModel } from '../account/account'
8adf0a76 42import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
444c0a0e
C
43import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
44import { VideoModel } from './video'
45import { VideoChannelModel } from './video-channel'
6d852470 46
594d3e48 47export enum ScopeNames {
ea44f375 48 WITH_ACCOUNT = 'WITH_ACCOUNT',
8adf0a76 49 WITH_ACCOUNT_FOR_API = 'WITH_ACCOUNT_FOR_API',
4635f59d 50 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
da854ddd 51 WITH_VIDEO = 'WITH_VIDEO',
4635f59d 52 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
bf1f6508
C
53}
54
3acc5084 55@Scopes(() => ({
696d83fd 56 [ScopeNames.ATTRIBUTES_FOR_API]: (blockerAccountIds: number[]) => {
7ad9b984
C
57 return {
58 attributes: {
59 include: [
60 [
61 Sequelize.literal(
62 '(' +
696d83fd 63 'WITH "blocklist" AS (' + buildBlockedAccountSQL(blockerAccountIds) + ')' +
7ad9b984
C
64 'SELECT COUNT("replies"."id") - (' +
65 'SELECT COUNT("replies"."id") ' +
66 'FROM "videoComment" AS "replies" ' +
67 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
68 'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
69 ')' +
70 'FROM "videoComment" AS "replies" ' +
71 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
72 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
73 ')'
74 ),
75 'totalReplies'
5b0413dd
RK
76 ],
77 [
78 Sequelize.literal(
79 '(' +
80 'SELECT COUNT("replies"."id") ' +
81 'FROM "videoComment" AS "replies" ' +
562724a1
C
82 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
83 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
5b0413dd 84 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
562724a1 85 'AND "replies"."accountId" = "videoChannel"."accountId"' +
5b0413dd
RK
86 ')'
87 ),
88 'totalRepliesFromVideoAuthor'
7ad9b984 89 ]
4635f59d 90 ]
7ad9b984 91 }
3acc5084 92 } as FindOptions
4635f59d 93 },
d3ea8975 94 [ScopeNames.WITH_ACCOUNT]: {
bf1f6508 95 include: [
4635f59d 96 {
453e83ea 97 model: AccountModel
4635f59d 98 }
3acc5084 99 ]
ea44f375 100 },
8adf0a76
C
101 [ScopeNames.WITH_ACCOUNT_FOR_API]: {
102 include: [
103 {
104 model: AccountModel.unscoped(),
105 include: [
106 {
107 attributes: {
108 exclude: unusedActorAttributesForAPI
109 },
110 model: ActorModel, // Default scope includes avatar and server
111 required: true
112 }
113 ]
114 }
115 ]
116 },
ea44f375
C
117 [ScopeNames.WITH_IN_REPLY_TO]: {
118 include: [
119 {
3acc5084 120 model: VideoCommentModel,
da854ddd
C
121 as: 'InReplyToVideoComment'
122 }
123 ]
124 },
125 [ScopeNames.WITH_VIDEO]: {
126 include: [
127 {
3acc5084 128 model: VideoModel,
4cb6d457
C
129 required: true,
130 include: [
131 {
453e83ea 132 model: VideoChannelModel,
4cb6d457
C
133 required: true,
134 include: [
135 {
3acc5084 136 model: AccountModel,
453e83ea 137 required: true
4cb6d457
C
138 }
139 ]
140 }
141 ]
ea44f375 142 }
3acc5084 143 ]
bf1f6508 144 }
3acc5084 145}))
6d852470
C
146@Table({
147 tableName: 'videoComment',
148 indexes: [
149 {
150 fields: [ 'videoId' ]
bf1f6508
C
151 },
152 {
153 fields: [ 'videoId', 'originCommentId' ]
0776d83f
C
154 },
155 {
156 fields: [ 'url' ],
157 unique: true
8cd72bd3
C
158 },
159 {
160 fields: [ 'accountId' ]
b84d4c80
C
161 },
162 {
163 fields: [
164 { name: 'createdAt', order: 'DESC' }
165 ]
6d852470
C
166 }
167 ]
168})
169export class VideoCommentModel extends Model<VideoCommentModel> {
170 @CreatedAt
171 createdAt: Date
172
173 @UpdatedAt
174 updatedAt: Date
175
69222afa
JM
176 @AllowNull(true)
177 @Column(DataType.DATE)
178 deletedAt: Date
179
6d852470
C
180 @AllowNull(false)
181 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
182 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
183 url: string
184
185 @AllowNull(false)
186 @Column(DataType.TEXT)
187 text: string
188
189 @ForeignKey(() => VideoCommentModel)
190 @Column
191 originCommentId: number
192
193 @BelongsTo(() => VideoCommentModel, {
194 foreignKey: {
db799da3 195 name: 'originCommentId',
6d852470
C
196 allowNull: true
197 },
db799da3 198 as: 'OriginVideoComment',
6d852470
C
199 onDelete: 'CASCADE'
200 })
201 OriginVideoComment: VideoCommentModel
202
203 @ForeignKey(() => VideoCommentModel)
204 @Column
205 inReplyToCommentId: number
206
207 @BelongsTo(() => VideoCommentModel, {
208 foreignKey: {
db799da3 209 name: 'inReplyToCommentId',
6d852470
C
210 allowNull: true
211 },
da854ddd 212 as: 'InReplyToVideoComment',
6d852470
C
213 onDelete: 'CASCADE'
214 })
c1e791ba 215 InReplyToVideoComment: VideoCommentModel | null
6d852470
C
216
217 @ForeignKey(() => VideoModel)
218 @Column
219 videoId: number
220
221 @BelongsTo(() => VideoModel, {
222 foreignKey: {
223 allowNull: false
224 },
225 onDelete: 'CASCADE'
226 })
227 Video: VideoModel
228
d3ea8975 229 @ForeignKey(() => AccountModel)
6d852470 230 @Column
d3ea8975 231 accountId: number
6d852470 232
d3ea8975 233 @BelongsTo(() => AccountModel, {
6d852470 234 foreignKey: {
69222afa 235 allowNull: true
6d852470
C
236 },
237 onDelete: 'CASCADE'
238 })
d3ea8975 239 Account: AccountModel
6d852470 240
57f6896f
C
241 @HasMany(() => VideoCommentAbuseModel, {
242 foreignKey: {
310b5219 243 name: 'videoCommentId',
57f6896f
C
244 allowNull: true
245 },
246 onDelete: 'set null'
247 })
248 CommentAbuses: VideoCommentAbuseModel[]
249
453e83ea 250 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
1735c825 251 const query: FindOptions = {
bf1f6508
C
252 where: {
253 id
254 }
255 }
256
257 if (t !== undefined) query.transaction = t
258
259 return VideoCommentModel.findOne(query)
260 }
261
453e83ea 262 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
1735c825 263 const query: FindOptions = {
da854ddd
C
264 where: {
265 id
266 }
267 }
268
269 if (t !== undefined) query.transaction = t
270
271 return VideoCommentModel
272 .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
273 .findOne(query)
274 }
275
453e83ea 276 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
1735c825 277 const query: FindOptions = {
6d852470
C
278 where: {
279 url
280 }
281 }
282
283 if (t !== undefined) query.transaction = t
284
511765c9 285 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
6d852470 286 }
bf1f6508 287
453e83ea 288 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
1735c825 289 const query: FindOptions = {
4cb6d457
C
290 where: {
291 url
6b9c966f
C
292 },
293 include: [
294 {
295 attributes: [ 'id', 'url' ],
296 model: VideoModel.unscoped()
297 }
298 ]
4cb6d457
C
299 }
300
301 if (t !== undefined) query.transaction = t
302
6b9c966f 303 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
4cb6d457
C
304 }
305
b4055e1c 306 static async listThreadsForApi (parameters: {
a1587156 307 videoId: number
696d83fd 308 isVideoOwned: boolean
a1587156
C
309 start: number
310 count: number
311 sort: string
453e83ea 312 user?: MUserAccountId
b4055e1c 313 }) {
696d83fd 314 const { videoId, isVideoOwned, start, count, sort, user } = parameters
b4055e1c 315
696d83fd 316 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
7ad9b984 317
bf1f6508
C
318 const query = {
319 offset: start,
320 limit: count,
c1125bca 321 order: getCommentSort(sort),
bf1f6508 322 where: {
8adf0a76
C
323 [Op.and]: [
324 {
325 videoId
326 },
327 {
328 inReplyToCommentId: null
329 },
330 {
331 [Op.or]: [
332 {
333 accountId: {
334 [Op.notIn]: Sequelize.literal(
696d83fd 335 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
8adf0a76
C
336 )
337 }
338 },
339 {
340 accountId: null
341 }
342 ]
343 }
344 ]
bf1f6508
C
345 }
346 }
347
3acc5084 348 const scopes: (string | ScopeOptions)[] = [
8adf0a76 349 ScopeNames.WITH_ACCOUNT_FOR_API,
7ad9b984 350 {
696d83fd 351 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
7ad9b984
C
352 }
353 ]
354
bf1f6508 355 return VideoCommentModel
7ad9b984 356 .scope(scopes)
bf1f6508
C
357 .findAndCountAll(query)
358 .then(({ rows, count }) => {
359 return { total: count, data: rows }
360 })
361 }
362
b4055e1c 363 static async listThreadCommentsForApi (parameters: {
a1587156 364 videoId: number
696d83fd 365 isVideoOwned: boolean
a1587156 366 threadId: number
453e83ea 367 user?: MUserAccountId
b4055e1c 368 }) {
696d83fd 369 const { videoId, threadId, user, isVideoOwned } = parameters
b4055e1c 370
696d83fd 371 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned })
7ad9b984 372
bf1f6508 373 const query = {
1735c825 374 order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
bf1f6508
C
375 where: {
376 videoId,
a1587156 377 [Op.or]: [
bf1f6508
C
378 { id: threadId },
379 { originCommentId: threadId }
7ad9b984
C
380 ],
381 accountId: {
1735c825 382 [Op.notIn]: Sequelize.literal(
696d83fd 383 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
7ad9b984
C
384 )
385 }
bf1f6508
C
386 }
387 }
388
7ad9b984 389 const scopes: any[] = [
8adf0a76 390 ScopeNames.WITH_ACCOUNT_FOR_API,
7ad9b984 391 {
696d83fd 392 method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ]
7ad9b984
C
393 }
394 ]
395
bf1f6508 396 return VideoCommentModel
7ad9b984 397 .scope(scopes)
bf1f6508
C
398 .findAndCountAll(query)
399 .then(({ rows, count }) => {
400 return { total: count, data: rows }
401 })
402 }
403
453e83ea 404 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
d7e70384 405 const query = {
1735c825 406 order: [ [ 'createdAt', order ] ] as Order,
d7e70384 407 where: {
d7e70384 408 id: {
a1587156 409 [Op.in]: Sequelize.literal('(' +
a3cffab4 410 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
f7cc67b4
C
411 `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
412 'UNION ' +
413 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
414 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
415 ') ' +
a3cffab4
C
416 'SELECT id FROM children' +
417 ')'),
a1587156 418 [Op.ne]: comment.id
d7e70384
C
419 }
420 },
421 transaction: t
422 }
423
424 return VideoCommentModel
425 .scope([ ScopeNames.WITH_ACCOUNT ])
426 .findAll(query)
427 }
428
696d83fd
C
429 static async listAndCountByVideoForAP (video: MVideoImmutable, start: number, count: number, t?: Transaction) {
430 const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({
431 videoId: video.id,
432 isVideoOwned: video.isOwned()
433 })
434
8fffe21a 435 const query = {
696d83fd 436 order: [ [ 'createdAt', 'ASC' ] ] as Order,
9a4a9b6c
C
437 offset: start,
438 limit: count,
8fffe21a 439 where: {
696d83fd
C
440 videoId: video.id,
441 accountId: {
442 [Op.notIn]: Sequelize.literal(
443 '(' + buildBlockedAccountSQL(blockerAccountIds) + ')'
444 )
445 }
8fffe21a
C
446 },
447 transaction: t
448 }
449
453e83ea 450 return VideoCommentModel.findAndCountAll<MComment>(query)
8fffe21a
C
451 }
452
00494d6e
RK
453 static async listForFeed (parameters: {
454 start: number
455 count: number
456 videoId?: number
457 accountId?: number
458 videoChannelId?: number
459 }): Promise<MCommentOwnerVideoFeed[]> {
1df8a4d7 460 const serverActor = await getServerActor()
00494d6e
RK
461 const { start, count, videoId, accountId, videoChannelId } = parameters
462
463 const accountExclusion = {
464 [Op.notIn]: Sequelize.literal(
465 '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')'
466 )
467 }
468 const accountWhere = accountId
469 ? {
d473fd94
RK
470 [Op.and]: {
471 ...accountExclusion,
472 [Op.eq]: accountId
00494d6e 473 }
d473fd94 474 }
00494d6e
RK
475 : accountExclusion
476
477 const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined
1df8a4d7 478
fe3a55b0 479 const query = {
1735c825 480 order: [ [ 'createdAt', 'DESC' ] ] as Order,
9a4a9b6c
C
481 offset: start,
482 limit: count,
193272b8 483 where: {
1df8a4d7 484 deletedAt: null,
00494d6e 485 accountId: accountWhere
193272b8 486 },
fe3a55b0
C
487 include: [
488 {
4dae00e6 489 attributes: [ 'name', 'uuid' ],
fe3a55b0 490 model: VideoModel.unscoped(),
68b6fd21
C
491 required: true,
492 where: {
493 privacy: VideoPrivacy.PUBLIC
696d83fd
C
494 },
495 include: [
496 {
497 attributes: [ 'accountId' ],
498 model: VideoChannelModel.unscoped(),
00494d6e
RK
499 required: true,
500 where: videoChannelWhere
696d83fd
C
501 }
502 ]
fe3a55b0
C
503 }
504 ]
505 }
506
507 if (videoId) query.where['videoId'] = videoId
508
509 return VideoCommentModel
510 .scope([ ScopeNames.WITH_ACCOUNT ])
511 .findAll(query)
512 }
513
444c0a0e
C
514 static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
515 const accountWhere = filter.onVideosOfAccount
516 ? { id: filter.onVideosOfAccount.id }
517 : {}
518
519 const query = {
520 limit: 1000,
521 where: {
522 deletedAt: null,
523 accountId: ofAccount.id
524 },
525 include: [
526 {
527 model: VideoModel,
528 required: true,
529 include: [
530 {
531 model: VideoChannelModel,
532 required: true,
533 include: [
534 {
535 model: AccountModel,
536 required: true,
537 where: accountWhere
538 }
539 ]
540 }
541 ]
542 }
543 ]
544 }
545
546 return VideoCommentModel
547 .scope([ ScopeNames.WITH_ACCOUNT ])
548 .findAll(query)
549 }
550
09cababd
C
551 static async getStats () {
552 const totalLocalVideoComments = await VideoCommentModel.count({
553 include: [
554 {
555 model: AccountModel,
556 required: true,
557 include: [
558 {
559 model: ActorModel,
560 required: true,
561 where: {
562 serverId: null
563 }
564 }
565 ]
566 }
567 ]
568 })
569 const totalVideoComments = await VideoCommentModel.count()
570
571 return {
572 totalLocalVideoComments,
573 totalVideoComments
574 }
575 }
576
2ba92871
C
577 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
578 const query = {
579 where: {
580 updatedAt: {
1735c825 581 [Op.lt]: beforeUpdatedAt
2ba92871 582 },
6b9c966f
C
583 videoId,
584 accountId: {
585 [Op.notIn]: buildLocalAccountIdsIn()
444c0a0e
C
586 },
587 // Do not delete Tombstones
588 deletedAt: null
6b9c966f 589 }
2ba92871
C
590 }
591
592 return VideoCommentModel.destroy(query)
593 }
594
cef534ed
C
595 getCommentStaticPath () {
596 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
597 }
598
d7e70384
C
599 getThreadId (): number {
600 return this.originCommentId || this.id
601 }
602
4cb6d457 603 isOwned () {
69222afa
JM
604 if (!this.Account) {
605 return false
606 }
607
4cb6d457
C
608 return this.Account.isOwned()
609 }
610
69222afa 611 isDeleted () {
a1587156 612 return this.deletedAt !== null
69222afa
JM
613 }
614
f7cc67b4 615 extractMentions () {
1f6d57e3 616 let result: string[] = []
f7cc67b4
C
617
618 const localMention = `@(${actorNameAlphabet}+)`
6dd9de95 619 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
f7cc67b4 620
1f6d57e3
C
621 const mentionRegex = this.isOwned()
622 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
623 : '(?:' + remoteMention + ')'
624
625 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
626 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
f7cc67b4 627 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
f7cc67b4 628
1f6d57e3
C
629 result = result.concat(
630 regexpCapture(this.text, firstMentionRegex)
631 .map(([ , username1, username2 ]) => username1 || username2),
f7cc67b4 632
1f6d57e3
C
633 regexpCapture(this.text, endMentionRegex)
634 .map(([ , username1, username2 ]) => username1 || username2),
635
636 regexpCapture(this.text, remoteMentionsRegex)
637 .map(([ , username ]) => username)
638 )
f7cc67b4 639
1f6d57e3
C
640 // Include local mentions
641 if (this.isOwned()) {
642 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
f7cc67b4 643
1f6d57e3
C
644 result = result.concat(
645 regexpCapture(this.text, localMentionsRegex)
646 .map(([ , username ]) => username)
f7cc67b4 647 )
1f6d57e3
C
648 }
649
650 return uniq(result)
f7cc67b4
C
651 }
652
1ca9f7c3 653 toFormattedJSON (this: MCommentFormattable) {
bf1f6508
C
654 return {
655 id: this.id,
656 url: this.url,
657 text: this.text,
8ca56654 658 threadId: this.getThreadId(),
d50acfab 659 inReplyToCommentId: this.inReplyToCommentId || null,
bf1f6508
C
660 videoId: this.videoId,
661 createdAt: this.createdAt,
d3ea8975 662 updatedAt: this.updatedAt,
69222afa
JM
663 deletedAt: this.deletedAt,
664 isDeleted: this.isDeleted(),
5b0413dd 665 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
4635f59d 666 totalReplies: this.get('totalReplies') || 0,
69222afa 667 account: this.Account ? this.Account.toFormattedJSON() : null
bf1f6508
C
668 } as VideoComment
669 }
ea44f375 670
69222afa 671 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
b5206dfc
JM
672 let inReplyTo: string
673 // New thread, so in AS we reply to the video
674 if (this.inReplyToCommentId === null) {
675 inReplyTo = this.Video.url
676 } else {
677 inReplyTo = this.InReplyToVideoComment.url
678 }
679
69222afa
JM
680 if (this.isDeleted()) {
681 return {
682 id: this.url,
683 type: 'Tombstone',
684 formerType: 'Note',
b5206dfc 685 inReplyTo,
69222afa
JM
686 published: this.createdAt.toISOString(),
687 updated: this.updatedAt.toISOString(),
688 deleted: this.deletedAt.toISOString()
689 }
690 }
691
d7e70384
C
692 const tag: ActivityTagObject[] = []
693 for (const parentComment of threadParentComments) {
b5206dfc
JM
694 if (!parentComment.Account) continue
695
d7e70384
C
696 const actor = parentComment.Account.Actor
697
698 tag.push({
699 type: 'Mention',
700 href: actor.url,
701 name: `@${actor.preferredUsername}@${actor.getHost()}`
702 })
703 }
704
ea44f375
C
705 return {
706 type: 'Note' as 'Note',
707 id: this.url,
708 content: this.text,
709 inReplyTo,
da854ddd 710 updated: this.updatedAt.toISOString(),
ea44f375 711 published: this.createdAt.toISOString(),
da854ddd 712 url: this.url,
d7e70384
C
713 attributedTo: this.Account.Actor.url,
714 tag
ea44f375
C
715 }
716 }
696d83fd
C
717
718 private static async buildBlockerAccountIds (options: {
719 videoId: number
720 isVideoOwned: boolean
721 user?: MUserAccountId
722 }) {
723 const { videoId, user, isVideoOwned } = options
724
725 const serverActor = await getServerActor()
726 const blockerAccountIds = [ serverActor.Account.id ]
727
728 if (user) blockerAccountIds.push(user.Account.id)
729
730 if (isVideoOwned) {
731 const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId)
732 blockerAccountIds.push(videoOwnerAccount.id)
733 }
734
735 return blockerAccountIds
736 }
6d852470 737}