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