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