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