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