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