]>
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) + ')' + |
99cb53fd | 72 | 'SELECT COUNT("replies"."id") ' + |
7ad9b984 C |
73 | 'FROM "videoComment" AS "replies" ' + |
74 | 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + | |
99cb53fd | 75 | 'AND "deletedAt" IS NULL ' + |
7ad9b984 C |
76 | 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' + |
77 | ')' | |
78 | ), | |
79 | 'totalReplies' | |
5b0413dd RK |
80 | ], |
81 | [ | |
82 | Sequelize.literal( | |
83 | '(' + | |
84 | 'SELECT COUNT("replies"."id") ' + | |
85 | 'FROM "videoComment" AS "replies" ' + | |
562724a1 C |
86 | 'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' + |
87 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
5b0413dd | 88 | 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + |
562724a1 | 89 | 'AND "replies"."accountId" = "videoChannel"."accountId"' + |
5b0413dd RK |
90 | ')' |
91 | ), | |
92 | 'totalRepliesFromVideoAuthor' | |
7ad9b984 | 93 | ] |
4635f59d | 94 | ] |
7ad9b984 | 95 | } |
3acc5084 | 96 | } as FindOptions |
4635f59d | 97 | }, |
d3ea8975 | 98 | [ScopeNames.WITH_ACCOUNT]: { |
bf1f6508 | 99 | include: [ |
4635f59d | 100 | { |
453e83ea | 101 | model: AccountModel |
4635f59d | 102 | } |
3acc5084 | 103 | ] |
ea44f375 | 104 | }, |
8adf0a76 C |
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 | }, | |
ea44f375 C |
121 | [ScopeNames.WITH_IN_REPLY_TO]: { |
122 | include: [ | |
123 | { | |
3acc5084 | 124 | model: VideoCommentModel, |
da854ddd C |
125 | as: 'InReplyToVideoComment' |
126 | } | |
127 | ] | |
128 | }, | |
129 | [ScopeNames.WITH_VIDEO]: { | |
130 | include: [ | |
131 | { | |
3acc5084 | 132 | model: VideoModel, |
4cb6d457 C |
133 | required: true, |
134 | include: [ | |
135 | { | |
453e83ea | 136 | model: VideoChannelModel, |
4cb6d457 C |
137 | required: true, |
138 | include: [ | |
139 | { | |
3acc5084 | 140 | model: AccountModel, |
453e83ea | 141 | required: true |
4cb6d457 C |
142 | } |
143 | ] | |
144 | } | |
145 | ] | |
ea44f375 | 146 | } |
3acc5084 | 147 | ] |
bf1f6508 | 148 | } |
3acc5084 | 149 | })) |
6d852470 C |
150 | @Table({ |
151 | tableName: 'videoComment', | |
152 | indexes: [ | |
153 | { | |
154 | fields: [ 'videoId' ] | |
bf1f6508 C |
155 | }, |
156 | { | |
157 | fields: [ 'videoId', 'originCommentId' ] | |
0776d83f C |
158 | }, |
159 | { | |
160 | fields: [ 'url' ], | |
161 | unique: true | |
8cd72bd3 C |
162 | }, |
163 | { | |
164 | fields: [ 'accountId' ] | |
b84d4c80 C |
165 | }, |
166 | { | |
167 | fields: [ | |
168 | { name: 'createdAt', order: 'DESC' } | |
169 | ] | |
6d852470 C |
170 | } |
171 | ] | |
172 | }) | |
16c016e8 | 173 | export class VideoCommentModel extends Model<Partial<AttributesOnly<VideoCommentModel>>> { |
6d852470 C |
174 | @CreatedAt |
175 | createdAt: Date | |
176 | ||
177 | @UpdatedAt | |
178 | updatedAt: Date | |
179 | ||
69222afa JM |
180 | @AllowNull(true) |
181 | @Column(DataType.DATE) | |
182 | deletedAt: Date | |
183 | ||
6d852470 C |
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: { | |
db799da3 | 199 | name: 'originCommentId', |
6d852470 C |
200 | allowNull: true |
201 | }, | |
db799da3 | 202 | as: 'OriginVideoComment', |
6d852470 C |
203 | onDelete: 'CASCADE' |
204 | }) | |
205 | OriginVideoComment: VideoCommentModel | |
206 | ||
207 | @ForeignKey(() => VideoCommentModel) | |
208 | @Column | |
209 | inReplyToCommentId: number | |
210 | ||
211 | @BelongsTo(() => VideoCommentModel, { | |
212 | foreignKey: { | |
db799da3 | 213 | name: 'inReplyToCommentId', |
6d852470 C |
214 | allowNull: true |
215 | }, | |
da854ddd | 216 | as: 'InReplyToVideoComment', |
6d852470 C |
217 | onDelete: 'CASCADE' |
218 | }) | |
c1e791ba | 219 | InReplyToVideoComment: VideoCommentModel | null |
6d852470 C |
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 | ||
d3ea8975 | 233 | @ForeignKey(() => AccountModel) |
6d852470 | 234 | @Column |
d3ea8975 | 235 | accountId: number |
6d852470 | 236 | |
d3ea8975 | 237 | @BelongsTo(() => AccountModel, { |
6d852470 | 238 | foreignKey: { |
69222afa | 239 | allowNull: true |
6d852470 C |
240 | }, |
241 | onDelete: 'CASCADE' | |
242 | }) | |
d3ea8975 | 243 | Account: AccountModel |
6d852470 | 244 | |
57f6896f C |
245 | @HasMany(() => VideoCommentAbuseModel, { |
246 | foreignKey: { | |
310b5219 | 247 | name: 'videoCommentId', |
57f6896f C |
248 | allowNull: true |
249 | }, | |
250 | onDelete: 'set null' | |
251 | }) | |
252 | CommentAbuses: VideoCommentAbuseModel[] | |
253 | ||
b49f22d8 | 254 | static loadById (id: number, t?: Transaction): Promise<MComment> { |
1735c825 | 255 | const query: FindOptions = { |
bf1f6508 C |
256 | where: { |
257 | id | |
258 | } | |
259 | } | |
260 | ||
261 | if (t !== undefined) query.transaction = t | |
262 | ||
263 | return VideoCommentModel.findOne(query) | |
264 | } | |
265 | ||
b49f22d8 | 266 | static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Promise<MCommentOwnerVideoReply> { |
1735c825 | 267 | const query: FindOptions = { |
da854ddd C |
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 | ||
b49f22d8 | 280 | static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Promise<MCommentOwnerVideo> { |
1735c825 | 281 | const query: FindOptions = { |
6d852470 C |
282 | where: { |
283 | url | |
284 | } | |
285 | } | |
286 | ||
287 | if (t !== undefined) query.transaction = t | |
288 | ||
511765c9 | 289 | return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) |
6d852470 | 290 | } |
bf1f6508 | 291 | |
b49f22d8 | 292 | static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Promise<MCommentOwnerReplyVideoLight> { |
1735c825 | 293 | const query: FindOptions = { |
4cb6d457 C |
294 | where: { |
295 | url | |
6b9c966f C |
296 | }, |
297 | include: [ | |
298 | { | |
299 | attributes: [ 'id', 'url' ], | |
300 | model: VideoModel.unscoped() | |
301 | } | |
302 | ] | |
4cb6d457 C |
303 | } |
304 | ||
305 | if (t !== undefined) query.transaction = t | |
306 | ||
6b9c966f | 307 | return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query) |
4cb6d457 C |
308 | } |
309 | ||
0f8d00e3 C |
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 | ||
0f8d00e3 | 322 | const where: WhereOptions = { |
f1273314 | 323 | deletedAt: null |
0f8d00e3 C |
324 | } |
325 | ||
326 | const whereAccount: WhereOptions = {} | |
327 | const whereActor: WhereOptions = {} | |
328 | const whereVideo: WhereOptions = {} | |
329 | ||
330 | if (isLocal === true) { | |
f1273314 | 331 | Object.assign(whereActor, { |
0f8d00e3 C |
332 | serverId: null |
333 | }) | |
334 | } else if (isLocal === false) { | |
f1273314 | 335 | Object.assign(whereActor, { |
0f8d00e3 C |
336 | serverId: { |
337 | [Op.ne]: null | |
338 | } | |
339 | }) | |
340 | } | |
341 | ||
342 | if (search) { | |
f1273314 C |
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 | }) | |
0f8d00e3 C |
351 | } |
352 | ||
353 | if (searchAccount) { | |
f1273314 C |
354 | Object.assign(whereActor, { |
355 | [Op.or]: [ | |
356 | searchAttribute(searchAccount, '$Account.Actor.preferredUsername$'), | |
357 | searchAttribute(searchAccount, '$Account.name$') | |
358 | ] | |
359 | }) | |
0f8d00e3 C |
360 | } |
361 | ||
362 | if (searchVideo) { | |
f1273314 | 363 | Object.assign(whereVideo, searchAttribute(searchVideo, 'name')) |
0f8d00e3 C |
364 | } |
365 | ||
f1273314 C |
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 | } | |
0f8d00e3 C |
394 | |
395 | return VideoCommentModel | |
396 | .findAndCountAll(query) | |
397 | .then(({ rows, count }) => { | |
398 | return { total: count, data: rows } | |
399 | }) | |
400 | } | |
401 | ||
b4055e1c | 402 | static async listThreadsForApi (parameters: { |
a1587156 | 403 | videoId: number |
696d83fd | 404 | isVideoOwned: boolean |
a1587156 C |
405 | start: number |
406 | count: number | |
407 | sort: string | |
453e83ea | 408 | user?: MUserAccountId |
b4055e1c | 409 | }) { |
696d83fd | 410 | const { videoId, isVideoOwned, start, count, sort, user } = parameters |
b4055e1c | 411 | |
696d83fd | 412 | const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned }) |
7ad9b984 | 413 | |
9d6b9d10 C |
414 | const accountBlockedWhere = { |
415 | accountId: { | |
416 | [Op.notIn]: Sequelize.literal( | |
417 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
418 | ) | |
419 | } | |
420 | } | |
421 | ||
422 | const queryList = { | |
bf1f6508 C |
423 | offset: start, |
424 | limit: count, | |
c1125bca | 425 | order: getCommentSort(sort), |
bf1f6508 | 426 | where: { |
8adf0a76 C |
427 | [Op.and]: [ |
428 | { | |
429 | videoId | |
430 | }, | |
431 | { | |
432 | inReplyToCommentId: null | |
433 | }, | |
434 | { | |
435 | [Op.or]: [ | |
9d6b9d10 | 436 | accountBlockedWhere, |
8adf0a76 C |
437 | { |
438 | accountId: null | |
439 | } | |
440 | ] | |
441 | } | |
442 | ] | |
bf1f6508 C |
443 | } |
444 | } | |
445 | ||
9d6b9d10 | 446 | const scopesList: (string | ScopeOptions)[] = [ |
8adf0a76 | 447 | ScopeNames.WITH_ACCOUNT_FOR_API, |
7ad9b984 | 448 | { |
696d83fd | 449 | method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ] |
7ad9b984 C |
450 | } |
451 | ] | |
452 | ||
9d6b9d10 C |
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 | }) | |
bf1f6508 C |
467 | } |
468 | ||
b4055e1c | 469 | static async listThreadCommentsForApi (parameters: { |
a1587156 | 470 | videoId: number |
696d83fd | 471 | isVideoOwned: boolean |
a1587156 | 472 | threadId: number |
453e83ea | 473 | user?: MUserAccountId |
b4055e1c | 474 | }) { |
696d83fd | 475 | const { videoId, threadId, user, isVideoOwned } = parameters |
b4055e1c | 476 | |
696d83fd | 477 | const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ videoId, user, isVideoOwned }) |
7ad9b984 | 478 | |
bf1f6508 | 479 | const query = { |
1735c825 | 480 | order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order, |
bf1f6508 C |
481 | where: { |
482 | videoId, | |
2a021e6c | 483 | [Op.and]: [ |
9d6b9d10 | 484 | { |
2a021e6c C |
485 | [Op.or]: [ |
486 | { id: threadId }, | |
487 | { originCommentId: threadId } | |
488 | ] | |
9d6b9d10 C |
489 | }, |
490 | { | |
2a021e6c C |
491 | [Op.or]: [ |
492 | { | |
493 | accountId: { | |
494 | [Op.notIn]: Sequelize.literal( | |
495 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
496 | ) | |
497 | } | |
498 | }, | |
499 | { | |
500 | accountId: null | |
501 | } | |
502 | ] | |
9d6b9d10 C |
503 | } |
504 | ] | |
bf1f6508 C |
505 | } |
506 | } | |
507 | ||
7ad9b984 | 508 | const scopes: any[] = [ |
8adf0a76 | 509 | ScopeNames.WITH_ACCOUNT_FOR_API, |
7ad9b984 | 510 | { |
696d83fd | 511 | method: [ ScopeNames.ATTRIBUTES_FOR_API, blockerAccountIds ] |
7ad9b984 C |
512 | } |
513 | ] | |
514 | ||
9d6b9d10 | 515 | return VideoCommentModel.scope(scopes) |
bf1f6508 C |
516 | .findAndCountAll(query) |
517 | .then(({ rows, count }) => { | |
518 | return { total: count, data: rows } | |
519 | }) | |
520 | } | |
521 | ||
b49f22d8 | 522 | static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Promise<MCommentOwner[]> { |
d7e70384 | 523 | const query = { |
1735c825 | 524 | order: [ [ 'createdAt', order ] ] as Order, |
d7e70384 | 525 | where: { |
d7e70384 | 526 | id: { |
a1587156 | 527 | [Op.in]: Sequelize.literal('(' + |
a3cffab4 | 528 | 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' + |
f7cc67b4 C |
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 | ') ' + | |
a3cffab4 C |
534 | 'SELECT id FROM children' + |
535 | ')'), | |
a1587156 | 536 | [Op.ne]: comment.id |
d7e70384 C |
537 | } |
538 | }, | |
539 | transaction: t | |
540 | } | |
541 | ||
542 | return VideoCommentModel | |
543 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
544 | .findAll(query) | |
545 | } | |
546 | ||
696d83fd C |
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 | ||
8fffe21a | 553 | const query = { |
696d83fd | 554 | order: [ [ 'createdAt', 'ASC' ] ] as Order, |
9a4a9b6c C |
555 | offset: start, |
556 | limit: count, | |
8fffe21a | 557 | where: { |
696d83fd C |
558 | videoId: video.id, |
559 | accountId: { | |
560 | [Op.notIn]: Sequelize.literal( | |
561 | '(' + buildBlockedAccountSQL(blockerAccountIds) + ')' | |
562 | ) | |
563 | } | |
8fffe21a C |
564 | }, |
565 | transaction: t | |
566 | } | |
567 | ||
453e83ea | 568 | return VideoCommentModel.findAndCountAll<MComment>(query) |
8fffe21a C |
569 | } |
570 | ||
00494d6e RK |
571 | static async listForFeed (parameters: { |
572 | start: number | |
573 | count: number | |
574 | videoId?: number | |
575 | accountId?: number | |
576 | videoChannelId?: number | |
577 | }): Promise<MCommentOwnerVideoFeed[]> { | |
1df8a4d7 | 578 | const serverActor = await getServerActor() |
00494d6e RK |
579 | const { start, count, videoId, accountId, videoChannelId } = parameters |
580 | ||
1c58423f C |
581 | const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized( |
582 | '"VideoCommentModel"."accountId"', | |
583 | [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ] | |
584 | ) | |
585 | ||
586 | if (accountId) { | |
587 | whereAnd.push({ | |
588 | [Op.eq]: accountId | |
589 | }) | |
590 | } | |
591 | ||
592 | const accountWhere = { | |
593 | [Op.and]: whereAnd | |
00494d6e | 594 | } |
00494d6e RK |
595 | |
596 | const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined | |
1df8a4d7 | 597 | |
fe3a55b0 | 598 | const query = { |
1735c825 | 599 | order: [ [ 'createdAt', 'DESC' ] ] as Order, |
9a4a9b6c C |
600 | offset: start, |
601 | limit: count, | |
193272b8 | 602 | where: { |
1df8a4d7 | 603 | deletedAt: null, |
00494d6e | 604 | accountId: accountWhere |
193272b8 | 605 | }, |
fe3a55b0 C |
606 | include: [ |
607 | { | |
4dae00e6 | 608 | attributes: [ 'name', 'uuid' ], |
fe3a55b0 | 609 | model: VideoModel.unscoped(), |
68b6fd21 C |
610 | required: true, |
611 | where: { | |
612 | privacy: VideoPrivacy.PUBLIC | |
696d83fd C |
613 | }, |
614 | include: [ | |
615 | { | |
616 | attributes: [ 'accountId' ], | |
617 | model: VideoChannelModel.unscoped(), | |
00494d6e RK |
618 | required: true, |
619 | where: videoChannelWhere | |
696d83fd C |
620 | } |
621 | ] | |
fe3a55b0 C |
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 | ||
444c0a0e C |
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 | ||
09cababd C |
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 | ||
74d249bc C |
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 | ||
2ba92871 C |
708 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { |
709 | const query = { | |
710 | where: { | |
711 | updatedAt: { | |
1735c825 | 712 | [Op.lt]: beforeUpdatedAt |
2ba92871 | 713 | }, |
6b9c966f C |
714 | videoId, |
715 | accountId: { | |
716 | [Op.notIn]: buildLocalAccountIdsIn() | |
444c0a0e C |
717 | }, |
718 | // Do not delete Tombstones | |
719 | deletedAt: null | |
6b9c966f | 720 | } |
2ba92871 C |
721 | } |
722 | ||
723 | return VideoCommentModel.destroy(query) | |
724 | } | |
725 | ||
cef534ed C |
726 | getCommentStaticPath () { |
727 | return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() | |
728 | } | |
729 | ||
d7e70384 C |
730 | getThreadId (): number { |
731 | return this.originCommentId || this.id | |
732 | } | |
733 | ||
4cb6d457 | 734 | isOwned () { |
69222afa JM |
735 | if (!this.Account) { |
736 | return false | |
737 | } | |
738 | ||
4cb6d457 C |
739 | return this.Account.isOwned() |
740 | } | |
741 | ||
69222afa | 742 | isDeleted () { |
a1587156 | 743 | return this.deletedAt !== null |
69222afa JM |
744 | } |
745 | ||
f7cc67b4 | 746 | extractMentions () { |
1f6d57e3 | 747 | let result: string[] = [] |
f7cc67b4 C |
748 | |
749 | const localMention = `@(${actorNameAlphabet}+)` | |
6dd9de95 | 750 | const remoteMention = `${localMention}@${WEBSERVER.HOST}` |
f7cc67b4 | 751 | |
1f6d57e3 C |
752 | const mentionRegex = this.isOwned() |
753 | ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions? | |
754 | : '(?:' + remoteMention + ')' | |
755 | ||
756 | const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g') | |
757 | const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g') | |
f7cc67b4 | 758 | const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g') |
f7cc67b4 | 759 | |
1f6d57e3 C |
760 | result = result.concat( |
761 | regexpCapture(this.text, firstMentionRegex) | |
762 | .map(([ , username1, username2 ]) => username1 || username2), | |
f7cc67b4 | 763 | |
1f6d57e3 C |
764 | regexpCapture(this.text, endMentionRegex) |
765 | .map(([ , username1, username2 ]) => username1 || username2), | |
766 | ||
767 | regexpCapture(this.text, remoteMentionsRegex) | |
768 | .map(([ , username ]) => username) | |
769 | ) | |
f7cc67b4 | 770 | |
1f6d57e3 C |
771 | // Include local mentions |
772 | if (this.isOwned()) { | |
773 | const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g') | |
f7cc67b4 | 774 | |
1f6d57e3 C |
775 | result = result.concat( |
776 | regexpCapture(this.text, localMentionsRegex) | |
777 | .map(([ , username ]) => username) | |
f7cc67b4 | 778 | ) |
1f6d57e3 C |
779 | } |
780 | ||
781 | return uniq(result) | |
f7cc67b4 C |
782 | } |
783 | ||
1ca9f7c3 | 784 | toFormattedJSON (this: MCommentFormattable) { |
bf1f6508 C |
785 | return { |
786 | id: this.id, | |
787 | url: this.url, | |
788 | text: this.text, | |
0f8d00e3 | 789 | |
8ca56654 | 790 | threadId: this.getThreadId(), |
d50acfab | 791 | inReplyToCommentId: this.inReplyToCommentId || null, |
bf1f6508 | 792 | videoId: this.videoId, |
0f8d00e3 | 793 | |
bf1f6508 | 794 | createdAt: this.createdAt, |
d3ea8975 | 795 | updatedAt: this.updatedAt, |
69222afa | 796 | deletedAt: this.deletedAt, |
0f8d00e3 | 797 | |
69222afa | 798 | isDeleted: this.isDeleted(), |
0f8d00e3 | 799 | |
5b0413dd | 800 | totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0, |
4635f59d | 801 | totalReplies: this.get('totalReplies') || 0, |
0f8d00e3 C |
802 | |
803 | account: this.Account | |
804 | ? this.Account.toFormattedJSON() | |
805 | : null | |
bf1f6508 C |
806 | } as VideoComment |
807 | } | |
ea44f375 | 808 | |
0f8d00e3 C |
809 | toFormattedAdminJSON (this: MCommentAdminFormattable) { |
810 | return { | |
811 | id: this.id, | |
812 | url: this.url, | |
813 | text: this.text, | |
814 | ||
815 | threadId: this.getThreadId(), | |
816 | inReplyToCommentId: this.inReplyToCommentId || null, | |
817 | videoId: this.videoId, | |
818 | ||
819 | createdAt: this.createdAt, | |
820 | updatedAt: this.updatedAt, | |
821 | ||
822 | video: { | |
823 | id: this.Video.id, | |
824 | uuid: this.Video.uuid, | |
825 | name: this.Video.name | |
826 | }, | |
827 | ||
828 | account: this.Account | |
829 | ? this.Account.toFormattedJSON() | |
830 | : null | |
831 | } as VideoCommentAdmin | |
832 | } | |
833 | ||
69222afa | 834 | toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject { |
b5206dfc JM |
835 | let inReplyTo: string |
836 | // New thread, so in AS we reply to the video | |
837 | if (this.inReplyToCommentId === null) { | |
838 | inReplyTo = this.Video.url | |
839 | } else { | |
840 | inReplyTo = this.InReplyToVideoComment.url | |
841 | } | |
842 | ||
69222afa JM |
843 | if (this.isDeleted()) { |
844 | return { | |
845 | id: this.url, | |
846 | type: 'Tombstone', | |
847 | formerType: 'Note', | |
b5206dfc | 848 | inReplyTo, |
69222afa JM |
849 | published: this.createdAt.toISOString(), |
850 | updated: this.updatedAt.toISOString(), | |
851 | deleted: this.deletedAt.toISOString() | |
852 | } | |
853 | } | |
854 | ||
d7e70384 C |
855 | const tag: ActivityTagObject[] = [] |
856 | for (const parentComment of threadParentComments) { | |
b5206dfc JM |
857 | if (!parentComment.Account) continue |
858 | ||
d7e70384 C |
859 | const actor = parentComment.Account.Actor |
860 | ||
861 | tag.push({ | |
862 | type: 'Mention', | |
863 | href: actor.url, | |
864 | name: `@${actor.preferredUsername}@${actor.getHost()}` | |
865 | }) | |
866 | } | |
867 | ||
ea44f375 C |
868 | return { |
869 | type: 'Note' as 'Note', | |
870 | id: this.url, | |
871 | content: this.text, | |
872 | inReplyTo, | |
da854ddd | 873 | updated: this.updatedAt.toISOString(), |
ea44f375 | 874 | published: this.createdAt.toISOString(), |
da854ddd | 875 | url: this.url, |
d7e70384 C |
876 | attributedTo: this.Account.Actor.url, |
877 | tag | |
ea44f375 C |
878 | } |
879 | } | |
696d83fd C |
880 | |
881 | private static async buildBlockerAccountIds (options: { | |
882 | videoId: number | |
883 | isVideoOwned: boolean | |
884 | user?: MUserAccountId | |
885 | }) { | |
886 | const { videoId, user, isVideoOwned } = options | |
887 | ||
888 | const serverActor = await getServerActor() | |
889 | const blockerAccountIds = [ serverActor.Account.id ] | |
890 | ||
891 | if (user) blockerAccountIds.push(user.Account.id) | |
892 | ||
893 | if (isVideoOwned) { | |
894 | const videoOwnerAccount = await AccountModel.loadAccountIdFromVideo(videoId) | |
895 | blockerAccountIds.push(videoOwnerAccount.id) | |
896 | } | |
897 | ||
898 | return blockerAccountIds | |
899 | } | |
6d852470 | 900 | } |