]>
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 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 | ||
398 | static async listThreadsForApi (parameters: { | |
399 | videoId: number | |
400 | isVideoOwned: boolean | |
401 | start: number | |
402 | count: number | |
403 | sort: string | |
404 | user?: MUserAccountId | |
405 | }) { | |
406 | const { videoId, isVideoOwned, start, count, sort, user } = parameters | |
407 | ||
408 | const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned }) | |
409 | ||
410 | const query = { | |
411 | offset: start, | |
412 | limit: count, | |
413 | order: getCommentSort(sort), | |
414 | where: { | |
415 | [Op.and]: [ | |
416 | { | |
417 | videoId | |
418 | }, | |
419 | { | |
420 | inReplyToCommentId: null | |
421 | }, | |
422 | { | |
423 | [Op.or]: [ | |
424 | { | |
425 | accountId: { | |
426 | [Op.notIn]: Sequelize.literal( | |
427 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
428 | ) | |
429 | } | |
430 | }, | |
431 | { | |
432 | accountId: null | |
433 | } | |
434 | ] | |
435 | } | |
436 | ] | |
437 | } | |
438 | } | |
439 | ||
440 | const scopes: (string | ScopeOptions)[] = [ | |
441 | ScopeNames.WITH_ACCOUNT_FOR_API, | |
442 | { | |
443 | method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ] | |
444 | } | |
445 | ] | |
446 | ||
447 | return VideoCommentModel | |
448 | .scope(scopes) | |
449 | .findAndCountAll(query) | |
450 | .then(({ rows, count }) => { | |
451 | return { total: count, data: rows } | |
452 | }) | |
453 | } | |
454 | ||
455 | static async listThreadCommentsForApi (parameters: { | |
456 | videoId: number | |
457 | isVideoOwned: boolean | |
458 | threadId: number | |
459 | user?: MUserAccountId | |
460 | }) { | |
461 | const { videoId, threadId, user, isVideoOwned } = parameters | |
462 | ||
463 | const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned }) | |
464 | ||
465 | const query = { | |
466 | order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order, | |
467 | where: { | |
468 | videoId, | |
469 | [Op.or]: [ | |
470 | { id: threadId }, | |
471 | { originCommentId: threadId } | |
472 | ], | |
473 | accountId: { | |
474 | [Op.notIn]: Sequelize.literal( | |
475 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
476 | ) | |
477 | } | |
478 | } | |
479 | } | |
480 | ||
481 | const scopes: any[] = [ | |
482 | ScopeNames.WITH_ACCOUNT_FOR_API, | |
483 | { | |
484 | method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ] | |
485 | } | |
486 | ] | |
487 | ||
488 | return VideoCommentModel | |
489 | .scope(scopes) | |
490 | .findAndCountAll(query) | |
491 | .then(({ rows, count }) => { | |
492 | return { total: count, data: rows } | |
493 | }) | |
494 | } | |
495 | ||
496 | static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> { | |
497 | const query = { | |
498 | order: [ [ 'createdAt', order ] ] as Order, | |
499 | where: { | |
500 | id: { | |
501 | [Op.in]: Sequelize.literal('(' + | |
502 | 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' + | |
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 | ') ' + | |
508 | 'SELECT id FROM children' + | |
509 | ')'), | |
510 | [Op.ne]: comment.id | |
511 | } | |
512 | }, | |
513 | transaction: t | |
514 | } | |
515 | ||
516 | return VideoCommentModel | |
517 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
518 | .findAll(query) | |
519 | } | |
520 | ||
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 | ||
527 | const query = { | |
528 | order: [ [ 'createdAt', 'ASC' ] ] as Order, | |
529 | offset: start, | |
530 | limit: count, | |
531 | where: { | |
532 | videoId: video.id, | |
533 | accountId: { | |
534 | [Op.notIn]: Sequelize.literal( | |
535 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
536 | ) | |
537 | } | |
538 | }, | |
539 | transaction: t | |
540 | } | |
541 | ||
542 | return VideoCommentModel.findAndCountAll<MComment>(query) | |
543 | } | |
544 | ||
545 | static async listForFeed (parameters: { | |
546 | start: number | |
547 | count: number | |
548 | videoId?: number | |
549 | accountId?: number | |
550 | videoChannelId?: number | |
551 | }): Promise<MCommentOwnerVideoFeed[]> { | |
552 | const serverActor = await getServerActor() | |
553 | const { start, count, videoId, accountId, videoChannelId } = parameters | |
554 | ||
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 | |
568 | } | |
569 | ||
570 | const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined | |
571 | ||
572 | const query = { | |
573 | order: [ [ 'createdAt', 'DESC' ] ] as Order, | |
574 | offset: start, | |
575 | limit: count, | |
576 | where: { | |
577 | deletedAt: null, | |
578 | accountId: accountWhere | |
579 | }, | |
580 | include: [ | |
581 | { | |
582 | attributes: [ 'name', 'uuid' ], | |
583 | model: VideoModel.unscoped(), | |
584 | required: true, | |
585 | where: { | |
586 | privacy: VideoPrivacy.PUBLIC | |
587 | }, | |
588 | include: [ | |
589 | { | |
590 | attributes: [ 'accountId' ], | |
591 | model: VideoChannelModel.unscoped(), | |
592 | required: true, | |
593 | where: videoChannelWhere | |
594 | } | |
595 | ] | |
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 | ||
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 | ||
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 | ||
670 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { | |
671 | const query = { | |
672 | where: { | |
673 | updatedAt: { | |
674 | [Op.lt]: beforeUpdatedAt | |
675 | }, | |
676 | videoId, | |
677 | accountId: { | |
678 | [Op.notIn]: buildLocalAccountIdsIn() | |
679 | }, | |
680 | // Do not delete Tombstones | |
681 | deletedAt: null | |
682 | } | |
683 | } | |
684 | ||
685 | return VideoCommentModel.destroy(query) | |
686 | } | |
687 | ||
688 | getCommentStaticPath () { | |
689 | return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() | |
690 | } | |
691 | ||
692 | getThreadId (): number { | |
693 | return this.originCommentId || this.id | |
694 | } | |
695 | ||
696 | isOwned () { | |
697 | if (!this.Account) { | |
698 | return false | |
699 | } | |
700 | ||
701 | return this.Account.isOwned() | |
702 | } | |
703 | ||
704 | isDeleted () { | |
705 | return this.deletedAt !== null | |
706 | } | |
707 | ||
708 | extractMentions () { | |
709 | let result: string[] = [] | |
710 | ||
711 | const localMention = `@(${actorNameAlphabet}+)` | |
712 | const remoteMention = `${localMention}@${WEBSERVER.HOST}` | |
713 | ||
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') | |
720 | const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g') | |
721 | ||
722 | result = result.concat( | |
723 | regexpCapture(this.text, firstMentionRegex) | |
724 | .map(([ , username1, username2 ]) => username1 || username2), | |
725 | ||
726 | regexpCapture(this.text, endMentionRegex) | |
727 | .map(([ , username1, username2 ]) => username1 || username2), | |
728 | ||
729 | regexpCapture(this.text, remoteMentionsRegex) | |
730 | .map(([ , username ]) => username) | |
731 | ) | |
732 | ||
733 | // Include local mentions | |
734 | if (this.isOwned()) { | |
735 | const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g') | |
736 | ||
737 | result = result.concat( | |
738 | regexpCapture(this.text, localMentionsRegex) | |
739 | .map(([ , username ]) => username) | |
740 | ) | |
741 | } | |
742 | ||
743 | return uniq(result) | |
744 | } | |
745 | ||
746 | toFormattedJSON (this: MCommentFormattable) { | |
747 | return { | |
748 | id: this.id, | |
749 | url: this.url, | |
750 | text: this.text, | |
751 | ||
752 | threadId: this.getThreadId(), | |
753 | inReplyToCommentId: this.inReplyToCommentId || null, | |
754 | videoId: this.videoId, | |
755 | ||
756 | createdAt: this.createdAt, | |
757 | updatedAt: this.updatedAt, | |
758 | deletedAt: this.deletedAt, | |
759 | ||
760 | isDeleted: this.isDeleted(), | |
761 | ||
762 | totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0, | |
763 | totalReplies: this.get('totalReplies') || 0, | |
764 | ||
765 | account: this.Account | |
766 | ? this.Account.toFormattedJSON() | |
767 | : null | |
768 | } as VideoComment | |
769 | } | |
770 | ||
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 | ||
796 | toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject { | |
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 | ||
805 | if (this.isDeleted()) { | |
806 | return { | |
807 | id: this.url, | |
808 | type: 'Tombstone', | |
809 | formerType: 'Note', | |
810 | inReplyTo, | |
811 | published: this.createdAt.toISOString(), | |
812 | updated: this.updatedAt.toISOString(), | |
813 | deleted: this.deletedAt.toISOString() | |
814 | } | |
815 | } | |
816 | ||
817 | const tag: ActivityTagObject[] = [] | |
818 | for (const parentComment of threadParentComments) { | |
819 | if (!parentComment.Account) continue | |
820 | ||
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 | ||
830 | return { | |
831 | type: 'Note' as 'Note', | |
832 | id: this.url, | |
833 | content: this.text, | |
834 | inReplyTo, | |
835 | updated: this.updatedAt.toISOString(), | |
836 | published: this.createdAt.toISOString(), | |
837 | url: this.url, | |
838 | attributedTo: this.Account.Actor.url, | |
839 | tag | |
840 | } | |
841 | } | |
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 | } | |
862 | } |