]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-comment.ts
b7ed6240e25f5c22018676f7535de4bd112b5624
[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 { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
13 import { regexpCapture } from '../../helpers/regexp'
14 import { uniq } from 'lodash'
15 import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
16 import * as Bluebird from 'bluebird'
17 import {
18 MComment,
19 MCommentAP,
20 MCommentFormattable,
21 MCommentId,
22 MCommentOwner,
23 MCommentOwnerReplyVideoLight,
24 MCommentOwnerVideo,
25 MCommentOwnerVideoFeed,
26 MCommentOwnerVideoReply
27 } from '../../typings/models/video'
28 import { MUserAccountId } from '@server/typings/models'
29 import { VideoPrivacy } from '@shared/models'
30 import { getServerActor } from '@server/models/application/application'
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 deletedAt: null
391 },
392 include: [
393 {
394 attributes: [ 'name', 'uuid' ],
395 model: VideoModel.unscoped(),
396 required: true,
397 where: {
398 privacy: VideoPrivacy.PUBLIC
399 }
400 }
401 ]
402 }
403
404 if (videoId) query.where['videoId'] = videoId
405
406 return VideoCommentModel
407 .scope([ ScopeNames.WITH_ACCOUNT ])
408 .findAll(query)
409 }
410
411 static async getStats () {
412 const totalLocalVideoComments = await VideoCommentModel.count({
413 include: [
414 {
415 model: AccountModel,
416 required: true,
417 include: [
418 {
419 model: ActorModel,
420 required: true,
421 where: {
422 serverId: null
423 }
424 }
425 ]
426 }
427 ]
428 })
429 const totalVideoComments = await VideoCommentModel.count()
430
431 return {
432 totalLocalVideoComments,
433 totalVideoComments
434 }
435 }
436
437 static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
438 const query = {
439 where: {
440 updatedAt: {
441 [Op.lt]: beforeUpdatedAt
442 },
443 videoId,
444 accountId: {
445 [Op.notIn]: buildLocalAccountIdsIn()
446 }
447 }
448 }
449
450 return VideoCommentModel.destroy(query)
451 }
452
453 getCommentStaticPath () {
454 return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
455 }
456
457 getThreadId (): number {
458 return this.originCommentId || this.id
459 }
460
461 isOwned () {
462 if (!this.Account) {
463 return false
464 }
465
466 return this.Account.isOwned()
467 }
468
469 isDeleted () {
470 return this.deletedAt !== null
471 }
472
473 extractMentions () {
474 let result: string[] = []
475
476 const localMention = `@(${actorNameAlphabet}+)`
477 const remoteMention = `${localMention}@${WEBSERVER.HOST}`
478
479 const mentionRegex = this.isOwned()
480 ? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
481 : '(?:' + remoteMention + ')'
482
483 const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
484 const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
485 const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
486
487 result = result.concat(
488 regexpCapture(this.text, firstMentionRegex)
489 .map(([ , username1, username2 ]) => username1 || username2),
490
491 regexpCapture(this.text, endMentionRegex)
492 .map(([ , username1, username2 ]) => username1 || username2),
493
494 regexpCapture(this.text, remoteMentionsRegex)
495 .map(([ , username ]) => username)
496 )
497
498 // Include local mentions
499 if (this.isOwned()) {
500 const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
501
502 result = result.concat(
503 regexpCapture(this.text, localMentionsRegex)
504 .map(([ , username ]) => username)
505 )
506 }
507
508 return uniq(result)
509 }
510
511 toFormattedJSON (this: MCommentFormattable) {
512 return {
513 id: this.id,
514 url: this.url,
515 text: this.text,
516 threadId: this.originCommentId || this.id,
517 inReplyToCommentId: this.inReplyToCommentId || null,
518 videoId: this.videoId,
519 createdAt: this.createdAt,
520 updatedAt: this.updatedAt,
521 deletedAt: this.deletedAt,
522 isDeleted: this.isDeleted(),
523 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
524 totalReplies: this.get('totalReplies') || 0,
525 account: this.Account ? this.Account.toFormattedJSON() : null
526 } as VideoComment
527 }
528
529 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
530 let inReplyTo: string
531 // New thread, so in AS we reply to the video
532 if (this.inReplyToCommentId === null) {
533 inReplyTo = this.Video.url
534 } else {
535 inReplyTo = this.InReplyToVideoComment.url
536 }
537
538 if (this.isDeleted()) {
539 return {
540 id: this.url,
541 type: 'Tombstone',
542 formerType: 'Note',
543 inReplyTo,
544 published: this.createdAt.toISOString(),
545 updated: this.updatedAt.toISOString(),
546 deleted: this.deletedAt.toISOString()
547 }
548 }
549
550 const tag: ActivityTagObject[] = []
551 for (const parentComment of threadParentComments) {
552 if (!parentComment.Account) continue
553
554 const actor = parentComment.Account.Actor
555
556 tag.push({
557 type: 'Mention',
558 href: actor.url,
559 name: `@${actor.preferredUsername}@${actor.getHost()}`
560 })
561 }
562
563 return {
564 type: 'Note' as 'Note',
565 id: this.url,
566 content: this.text,
567 inReplyTo,
568 updated: this.updatedAt.toISOString(),
569 published: this.createdAt.toISOString(),
570 url: this.url,
571 attributedTo: this.Account.Actor.url,
572 tag
573 }
574 }
575 }