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