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