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