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