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