]>
Commit | Line | Data |
---|---|---|
453e83ea | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
d7e70384 | 2 | import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects' |
ea44f375 | 3 | import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' |
bf1f6508 | 4 | import { VideoComment } from '../../../shared/models/videos/video-comment.model' |
da854ddd | 5 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
74dc3bca | 6 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
d3ea8975 | 7 | import { AccountModel } from '../account/account' |
4635f59d | 8 | import { ActorModel } from '../activitypub/actor' |
6b9c966f | 9 | import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' |
6d852470 | 10 | import { VideoModel } from './video' |
4cb6d457 | 11 | import { VideoChannelModel } from './video-channel' |
7ad9b984 | 12 | import { getServerActor } from '../../helpers/utils' |
f7cc67b4 C |
13 | import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor' |
14 | import { regexpCapture } from '../../helpers/regexp' | |
15 | import { uniq } from 'lodash' | |
453e83ea C |
16 | import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' |
17 | import * as Bluebird from 'bluebird' | |
18 | import { | |
19 | MComment, | |
b5fecbf4 | 20 | MCommentAP, |
1ca9f7c3 | 21 | MCommentFormattable, |
453e83ea C |
22 | MCommentId, |
23 | MCommentOwner, | |
24 | MCommentOwnerReplyVideoLight, | |
25 | MCommentOwnerVideo, | |
26 | MCommentOwnerVideoFeed, | |
27 | MCommentOwnerVideoReply | |
28 | } from '../../typings/models/video' | |
29 | import { MUserAccountId } from '@server/typings/models' | |
6d852470 | 30 | |
bf1f6508 | 31 | enum ScopeNames { |
ea44f375 | 32 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
4635f59d | 33 | WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO', |
da854ddd | 34 | WITH_VIDEO = 'WITH_VIDEO', |
4635f59d | 35 | ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API' |
bf1f6508 C |
36 | } |
37 | ||
3acc5084 | 38 | @Scopes(() => ({ |
7ad9b984 C |
39 | [ScopeNames.ATTRIBUTES_FOR_API]: (serverAccountId: number, userAccountId?: number) => { |
40 | return { | |
41 | attributes: { | |
42 | include: [ | |
43 | [ | |
44 | Sequelize.literal( | |
45 | '(' + | |
46 | 'WITH "blocklist" AS (' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' + | |
47 | 'SELECT COUNT("replies"."id") - (' + | |
48 | 'SELECT COUNT("replies"."id") ' + | |
49 | 'FROM "videoComment" AS "replies" ' + | |
50 | 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + | |
51 | 'AND "accountId" IN (SELECT "id" FROM "blocklist")' + | |
52 | ')' + | |
53 | 'FROM "videoComment" AS "replies" ' + | |
54 | 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' + | |
55 | 'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' + | |
56 | ')' | |
57 | ), | |
58 | 'totalReplies' | |
59 | ] | |
4635f59d | 60 | ] |
7ad9b984 | 61 | } |
3acc5084 | 62 | } as FindOptions |
4635f59d | 63 | }, |
d3ea8975 | 64 | [ScopeNames.WITH_ACCOUNT]: { |
bf1f6508 | 65 | include: [ |
4635f59d | 66 | { |
453e83ea | 67 | model: AccountModel |
4635f59d | 68 | } |
3acc5084 | 69 | ] |
ea44f375 C |
70 | }, |
71 | [ScopeNames.WITH_IN_REPLY_TO]: { | |
72 | include: [ | |
73 | { | |
3acc5084 | 74 | model: VideoCommentModel, |
da854ddd C |
75 | as: 'InReplyToVideoComment' |
76 | } | |
77 | ] | |
78 | }, | |
79 | [ScopeNames.WITH_VIDEO]: { | |
80 | include: [ | |
81 | { | |
3acc5084 | 82 | model: VideoModel, |
4cb6d457 C |
83 | required: true, |
84 | include: [ | |
85 | { | |
453e83ea | 86 | model: VideoChannelModel, |
4cb6d457 C |
87 | required: true, |
88 | include: [ | |
89 | { | |
3acc5084 | 90 | model: AccountModel, |
453e83ea | 91 | required: true |
4cb6d457 C |
92 | } |
93 | ] | |
94 | } | |
95 | ] | |
ea44f375 | 96 | } |
3acc5084 | 97 | ] |
bf1f6508 | 98 | } |
3acc5084 | 99 | })) |
6d852470 C |
100 | @Table({ |
101 | tableName: 'videoComment', | |
102 | indexes: [ | |
103 | { | |
104 | fields: [ 'videoId' ] | |
bf1f6508 C |
105 | }, |
106 | { | |
107 | fields: [ 'videoId', 'originCommentId' ] | |
0776d83f C |
108 | }, |
109 | { | |
110 | fields: [ 'url' ], | |
111 | unique: true | |
8cd72bd3 C |
112 | }, |
113 | { | |
114 | fields: [ 'accountId' ] | |
6d852470 C |
115 | } |
116 | ] | |
117 | }) | |
118 | export class VideoCommentModel extends Model<VideoCommentModel> { | |
119 | @CreatedAt | |
120 | createdAt: Date | |
121 | ||
122 | @UpdatedAt | |
123 | updatedAt: Date | |
124 | ||
125 | @AllowNull(false) | |
126 | @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
127 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max)) | |
128 | url: string | |
129 | ||
130 | @AllowNull(false) | |
131 | @Column(DataType.TEXT) | |
132 | text: string | |
133 | ||
134 | @ForeignKey(() => VideoCommentModel) | |
135 | @Column | |
136 | originCommentId: number | |
137 | ||
138 | @BelongsTo(() => VideoCommentModel, { | |
139 | foreignKey: { | |
db799da3 | 140 | name: 'originCommentId', |
6d852470 C |
141 | allowNull: true |
142 | }, | |
db799da3 | 143 | as: 'OriginVideoComment', |
6d852470 C |
144 | onDelete: 'CASCADE' |
145 | }) | |
146 | OriginVideoComment: VideoCommentModel | |
147 | ||
148 | @ForeignKey(() => VideoCommentModel) | |
149 | @Column | |
150 | inReplyToCommentId: number | |
151 | ||
152 | @BelongsTo(() => VideoCommentModel, { | |
153 | foreignKey: { | |
db799da3 | 154 | name: 'inReplyToCommentId', |
6d852470 C |
155 | allowNull: true |
156 | }, | |
da854ddd | 157 | as: 'InReplyToVideoComment', |
6d852470 C |
158 | onDelete: 'CASCADE' |
159 | }) | |
c1e791ba | 160 | InReplyToVideoComment: VideoCommentModel | null |
6d852470 C |
161 | |
162 | @ForeignKey(() => VideoModel) | |
163 | @Column | |
164 | videoId: number | |
165 | ||
166 | @BelongsTo(() => VideoModel, { | |
167 | foreignKey: { | |
168 | allowNull: false | |
169 | }, | |
170 | onDelete: 'CASCADE' | |
171 | }) | |
172 | Video: VideoModel | |
173 | ||
d3ea8975 | 174 | @ForeignKey(() => AccountModel) |
6d852470 | 175 | @Column |
d3ea8975 | 176 | accountId: number |
6d852470 | 177 | |
d3ea8975 | 178 | @BelongsTo(() => AccountModel, { |
6d852470 C |
179 | foreignKey: { |
180 | allowNull: false | |
181 | }, | |
182 | onDelete: 'CASCADE' | |
183 | }) | |
d3ea8975 | 184 | Account: AccountModel |
6d852470 | 185 | |
453e83ea | 186 | static loadById (id: number, t?: Transaction): Bluebird<MComment> { |
1735c825 | 187 | const query: FindOptions = { |
bf1f6508 C |
188 | where: { |
189 | id | |
190 | } | |
191 | } | |
192 | ||
193 | if (t !== undefined) query.transaction = t | |
194 | ||
195 | return VideoCommentModel.findOne(query) | |
196 | } | |
197 | ||
453e83ea | 198 | static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> { |
1735c825 | 199 | const query: FindOptions = { |
da854ddd C |
200 | where: { |
201 | id | |
202 | } | |
203 | } | |
204 | ||
205 | if (t !== undefined) query.transaction = t | |
206 | ||
207 | return VideoCommentModel | |
208 | .scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ]) | |
209 | .findOne(query) | |
210 | } | |
211 | ||
453e83ea | 212 | static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> { |
1735c825 | 213 | const query: FindOptions = { |
6d852470 C |
214 | where: { |
215 | url | |
216 | } | |
217 | } | |
218 | ||
219 | if (t !== undefined) query.transaction = t | |
220 | ||
511765c9 | 221 | return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) |
6d852470 | 222 | } |
bf1f6508 | 223 | |
453e83ea | 224 | static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> { |
1735c825 | 225 | const query: FindOptions = { |
4cb6d457 C |
226 | where: { |
227 | url | |
6b9c966f C |
228 | }, |
229 | include: [ | |
230 | { | |
231 | attributes: [ 'id', 'url' ], | |
232 | model: VideoModel.unscoped() | |
233 | } | |
234 | ] | |
4cb6d457 C |
235 | } |
236 | ||
237 | if (t !== undefined) query.transaction = t | |
238 | ||
6b9c966f | 239 | return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query) |
4cb6d457 C |
240 | } |
241 | ||
b4055e1c C |
242 | static async listThreadsForApi (parameters: { |
243 | videoId: number, | |
244 | start: number, | |
245 | count: number, | |
246 | sort: string, | |
453e83ea | 247 | user?: MUserAccountId |
b4055e1c C |
248 | }) { |
249 | const { videoId, start, count, sort, user } = parameters | |
250 | ||
7ad9b984 C |
251 | const serverActor = await getServerActor() |
252 | const serverAccountId = serverActor.Account.id | |
65b21c96 | 253 | const userAccountId = user ? user.Account.id : undefined |
7ad9b984 | 254 | |
bf1f6508 C |
255 | const query = { |
256 | offset: start, | |
257 | limit: count, | |
3bb6c526 | 258 | order: getSort(sort), |
bf1f6508 | 259 | where: { |
d3ea8975 | 260 | videoId, |
7ad9b984 C |
261 | inReplyToCommentId: null, |
262 | accountId: { | |
1735c825 | 263 | [Op.notIn]: Sequelize.literal( |
7ad9b984 C |
264 | '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' |
265 | ) | |
266 | } | |
bf1f6508 C |
267 | } |
268 | } | |
269 | ||
3acc5084 | 270 | const scopes: (string | ScopeOptions)[] = [ |
7ad9b984 C |
271 | ScopeNames.WITH_ACCOUNT, |
272 | { | |
273 | method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ] | |
274 | } | |
275 | ] | |
276 | ||
bf1f6508 | 277 | return VideoCommentModel |
7ad9b984 | 278 | .scope(scopes) |
bf1f6508 C |
279 | .findAndCountAll(query) |
280 | .then(({ rows, count }) => { | |
281 | return { total: count, data: rows } | |
282 | }) | |
283 | } | |
284 | ||
b4055e1c C |
285 | static async listThreadCommentsForApi (parameters: { |
286 | videoId: number, | |
287 | threadId: number, | |
453e83ea | 288 | user?: MUserAccountId |
b4055e1c C |
289 | }) { |
290 | const { videoId, threadId, user } = parameters | |
291 | ||
7ad9b984 C |
292 | const serverActor = await getServerActor() |
293 | const serverAccountId = serverActor.Account.id | |
65b21c96 | 294 | const userAccountId = user ? user.Account.id : undefined |
7ad9b984 | 295 | |
bf1f6508 | 296 | const query = { |
1735c825 | 297 | order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order, |
bf1f6508 C |
298 | where: { |
299 | videoId, | |
1735c825 | 300 | [ Op.or ]: [ |
bf1f6508 C |
301 | { id: threadId }, |
302 | { originCommentId: threadId } | |
7ad9b984 C |
303 | ], |
304 | accountId: { | |
1735c825 | 305 | [Op.notIn]: Sequelize.literal( |
7ad9b984 C |
306 | '(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' |
307 | ) | |
308 | } | |
bf1f6508 C |
309 | } |
310 | } | |
311 | ||
7ad9b984 C |
312 | const scopes: any[] = [ |
313 | ScopeNames.WITH_ACCOUNT, | |
314 | { | |
315 | method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ] | |
316 | } | |
317 | ] | |
318 | ||
bf1f6508 | 319 | return VideoCommentModel |
7ad9b984 | 320 | .scope(scopes) |
bf1f6508 C |
321 | .findAndCountAll(query) |
322 | .then(({ rows, count }) => { | |
323 | return { total: count, data: rows } | |
324 | }) | |
325 | } | |
326 | ||
453e83ea | 327 | static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> { |
d7e70384 | 328 | const query = { |
1735c825 | 329 | order: [ [ 'createdAt', order ] ] as Order, |
d7e70384 | 330 | where: { |
d7e70384 | 331 | id: { |
1735c825 | 332 | [ Op.in ]: Sequelize.literal('(' + |
a3cffab4 | 333 | 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' + |
f7cc67b4 C |
334 | `SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` + |
335 | 'UNION ' + | |
336 | 'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' + | |
337 | 'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' + | |
338 | ') ' + | |
a3cffab4 C |
339 | 'SELECT id FROM children' + |
340 | ')'), | |
1735c825 | 341 | [ Op.ne ]: comment.id |
d7e70384 C |
342 | } |
343 | }, | |
344 | transaction: t | |
345 | } | |
346 | ||
347 | return VideoCommentModel | |
348 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
349 | .findAll(query) | |
350 | } | |
351 | ||
1735c825 | 352 | static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction, order: 'ASC' | 'DESC' = 'ASC') { |
8fffe21a | 353 | const query = { |
1735c825 | 354 | order: [ [ 'createdAt', order ] ] as Order, |
9a4a9b6c C |
355 | offset: start, |
356 | limit: count, | |
8fffe21a C |
357 | where: { |
358 | videoId | |
359 | }, | |
360 | transaction: t | |
361 | } | |
362 | ||
453e83ea | 363 | return VideoCommentModel.findAndCountAll<MComment>(query) |
8fffe21a C |
364 | } |
365 | ||
453e83ea | 366 | static listForFeed (start: number, count: number, videoId?: number): Bluebird<MCommentOwnerVideoFeed[]> { |
fe3a55b0 | 367 | const query = { |
1735c825 | 368 | order: [ [ 'createdAt', 'DESC' ] ] as Order, |
9a4a9b6c C |
369 | offset: start, |
370 | limit: count, | |
fe3a55b0 C |
371 | where: {}, |
372 | include: [ | |
373 | { | |
4dae00e6 | 374 | attributes: [ 'name', 'uuid' ], |
fe3a55b0 C |
375 | model: VideoModel.unscoped(), |
376 | required: true | |
377 | } | |
378 | ] | |
379 | } | |
380 | ||
381 | if (videoId) query.where['videoId'] = videoId | |
382 | ||
383 | return VideoCommentModel | |
384 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
385 | .findAll(query) | |
386 | } | |
387 | ||
09cababd C |
388 | static async getStats () { |
389 | const totalLocalVideoComments = await VideoCommentModel.count({ | |
390 | include: [ | |
391 | { | |
392 | model: AccountModel, | |
393 | required: true, | |
394 | include: [ | |
395 | { | |
396 | model: ActorModel, | |
397 | required: true, | |
398 | where: { | |
399 | serverId: null | |
400 | } | |
401 | } | |
402 | ] | |
403 | } | |
404 | ] | |
405 | }) | |
406 | const totalVideoComments = await VideoCommentModel.count() | |
407 | ||
408 | return { | |
409 | totalLocalVideoComments, | |
410 | totalVideoComments | |
411 | } | |
412 | } | |
413 | ||
2ba92871 C |
414 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { |
415 | const query = { | |
416 | where: { | |
417 | updatedAt: { | |
1735c825 | 418 | [Op.lt]: beforeUpdatedAt |
2ba92871 | 419 | }, |
6b9c966f C |
420 | videoId, |
421 | accountId: { | |
422 | [Op.notIn]: buildLocalAccountIdsIn() | |
970ceac0 | 423 | } |
6b9c966f | 424 | } |
2ba92871 C |
425 | } |
426 | ||
427 | return VideoCommentModel.destroy(query) | |
428 | } | |
429 | ||
cef534ed C |
430 | getCommentStaticPath () { |
431 | return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() | |
432 | } | |
433 | ||
d7e70384 C |
434 | getThreadId (): number { |
435 | return this.originCommentId || this.id | |
436 | } | |
437 | ||
4cb6d457 C |
438 | isOwned () { |
439 | return this.Account.isOwned() | |
440 | } | |
441 | ||
f7cc67b4 | 442 | extractMentions () { |
1f6d57e3 | 443 | let result: string[] = [] |
f7cc67b4 C |
444 | |
445 | const localMention = `@(${actorNameAlphabet}+)` | |
6dd9de95 | 446 | const remoteMention = `${localMention}@${WEBSERVER.HOST}` |
f7cc67b4 | 447 | |
1f6d57e3 C |
448 | const mentionRegex = this.isOwned() |
449 | ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions? | |
450 | : '(?:' + remoteMention + ')' | |
451 | ||
452 | const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g') | |
453 | const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g') | |
f7cc67b4 | 454 | const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g') |
f7cc67b4 | 455 | |
1f6d57e3 C |
456 | result = result.concat( |
457 | regexpCapture(this.text, firstMentionRegex) | |
458 | .map(([ , username1, username2 ]) => username1 || username2), | |
f7cc67b4 | 459 | |
1f6d57e3 C |
460 | regexpCapture(this.text, endMentionRegex) |
461 | .map(([ , username1, username2 ]) => username1 || username2), | |
462 | ||
463 | regexpCapture(this.text, remoteMentionsRegex) | |
464 | .map(([ , username ]) => username) | |
465 | ) | |
f7cc67b4 | 466 | |
1f6d57e3 C |
467 | // Include local mentions |
468 | if (this.isOwned()) { | |
469 | const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g') | |
f7cc67b4 | 470 | |
1f6d57e3 C |
471 | result = result.concat( |
472 | regexpCapture(this.text, localMentionsRegex) | |
473 | .map(([ , username ]) => username) | |
f7cc67b4 | 474 | ) |
1f6d57e3 C |
475 | } |
476 | ||
477 | return uniq(result) | |
f7cc67b4 C |
478 | } |
479 | ||
1ca9f7c3 | 480 | toFormattedJSON (this: MCommentFormattable) { |
bf1f6508 C |
481 | return { |
482 | id: this.id, | |
483 | url: this.url, | |
484 | text: this.text, | |
485 | threadId: this.originCommentId || this.id, | |
d50acfab | 486 | inReplyToCommentId: this.inReplyToCommentId || null, |
bf1f6508 C |
487 | videoId: this.videoId, |
488 | createdAt: this.createdAt, | |
d3ea8975 | 489 | updatedAt: this.updatedAt, |
4635f59d | 490 | totalReplies: this.get('totalReplies') || 0, |
cf117aaa | 491 | account: this.Account.toFormattedJSON() |
bf1f6508 C |
492 | } as VideoComment |
493 | } | |
ea44f375 | 494 | |
b5fecbf4 | 495 | toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject { |
ea44f375 C |
496 | let inReplyTo: string |
497 | // New thread, so in AS we reply to the video | |
2cebd797 | 498 | if (this.inReplyToCommentId === null) { |
ea44f375 C |
499 | inReplyTo = this.Video.url |
500 | } else { | |
501 | inReplyTo = this.InReplyToVideoComment.url | |
502 | } | |
503 | ||
d7e70384 C |
504 | const tag: ActivityTagObject[] = [] |
505 | for (const parentComment of threadParentComments) { | |
506 | const actor = parentComment.Account.Actor | |
507 | ||
508 | tag.push({ | |
509 | type: 'Mention', | |
510 | href: actor.url, | |
511 | name: `@${actor.preferredUsername}@${actor.getHost()}` | |
512 | }) | |
513 | } | |
514 | ||
ea44f375 C |
515 | return { |
516 | type: 'Note' as 'Note', | |
517 | id: this.url, | |
518 | content: this.text, | |
519 | inReplyTo, | |
da854ddd | 520 | updated: this.updatedAt.toISOString(), |
ea44f375 | 521 | published: this.createdAt.toISOString(), |
da854ddd | 522 | url: this.url, |
d7e70384 C |
523 | attributedTo: this.Account.Actor.url, |
524 | tag | |
ea44f375 C |
525 | } |
526 | } | |
6d852470 | 527 | } |