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