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