aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-comment.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/video-comment.ts')
-rw-r--r--server/models/video/video-comment.ts87
1 files changed, 29 insertions, 58 deletions
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts
index 58b75510d..2e4220434 100644
--- a/server/models/video/video-comment.ts
+++ b/server/models/video/video-comment.ts
@@ -1,36 +1,32 @@
1import { 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 AllowNull,
3 BeforeDestroy,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 ForeignKey,
9 Is,
10 Model,
11 Scopes,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
15import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects' 2import { ActivityTagObject } from '../../../shared/models/activitypub/objects/common-objects'
16import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' 3import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
17import { VideoComment } from '../../../shared/models/videos/video-comment.model' 4import { VideoComment } from '../../../shared/models/videos/video-comment.model'
18import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 5import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
19import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' 6import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
20import { sendDeleteVideoComment } from '../../lib/activitypub/send'
21import { AccountModel } from '../account/account' 7import { AccountModel } from '../account/account'
22import { ActorModel } from '../activitypub/actor' 8import { ActorModel } from '../activitypub/actor'
23import { AvatarModel } from '../avatar/avatar'
24import { ServerModel } from '../server/server'
25import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' 9import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
26import { VideoModel } from './video' 10import { VideoModel } from './video'
27import { VideoChannelModel } from './video-channel' 11import { VideoChannelModel } from './video-channel'
28import { getServerActor } from '../../helpers/utils' 12import { getServerActor } from '../../helpers/utils'
29import { UserModel } from '../account/user'
30import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor' 13import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
31import { regexpCapture } from '../../helpers/regexp' 14import { regexpCapture } from '../../helpers/regexp'
32import { uniq } from 'lodash' 15import { uniq } from 'lodash'
33import { FindOptions, literal, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' 16import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
17import * as Bluebird from 'bluebird'
18import {
19 MComment,
20 MCommentAP,
21 MCommentFormattable,
22 MCommentId,
23 MCommentOwner,
24 MCommentOwnerReplyVideoLight,
25 MCommentOwnerVideo,
26 MCommentOwnerVideoFeed,
27 MCommentOwnerVideoReply
28} from '../../typings/models/video'
29import { MUserAccountId } from '@server/typings/models'
34 30
35enum ScopeNames { 31enum ScopeNames {
36 WITH_ACCOUNT = 'WITH_ACCOUNT', 32 WITH_ACCOUNT = 'WITH_ACCOUNT',
@@ -68,22 +64,7 @@ enum ScopeNames {
68 [ScopeNames.WITH_ACCOUNT]: { 64 [ScopeNames.WITH_ACCOUNT]: {
69 include: [ 65 include: [
70 { 66 {
71 model: AccountModel, 67 model: AccountModel
72 include: [
73 {
74 model: ActorModel,
75 include: [
76 {
77 model: ServerModel,
78 required: false
79 },
80 {
81 model: AvatarModel,
82 required: false
83 }
84 ]
85 }
86 ]
87 } 68 }
88 ] 69 ]
89 }, 70 },
@@ -102,22 +83,12 @@ enum ScopeNames {
102 required: true, 83 required: true,
103 include: [ 84 include: [
104 { 85 {
105 model: VideoChannelModel.unscoped(), 86 model: VideoChannelModel,
106 required: true, 87 required: true,
107 include: [ 88 include: [
108 { 89 {
109 model: ActorModel,
110 required: true
111 },
112 {
113 model: AccountModel, 90 model: AccountModel,
114 required: true, 91 required: true
115 include: [
116 {
117 model: ActorModel,
118 required: true
119 }
120 ]
121 } 92 }
122 ] 93 ]
123 } 94 }
@@ -212,7 +183,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
212 }) 183 })
213 Account: AccountModel 184 Account: AccountModel
214 185
215 static loadById (id: number, t?: Transaction) { 186 static loadById (id: number, t?: Transaction): Bluebird<MComment> {
216 const query: FindOptions = { 187 const query: FindOptions = {
217 where: { 188 where: {
218 id 189 id
@@ -224,7 +195,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
224 return VideoCommentModel.findOne(query) 195 return VideoCommentModel.findOne(query)
225 } 196 }
226 197
227 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction) { 198 static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
228 const query: FindOptions = { 199 const query: FindOptions = {
229 where: { 200 where: {
230 id 201 id
@@ -238,7 +209,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
238 .findOne(query) 209 .findOne(query)
239 } 210 }
240 211
241 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction) { 212 static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
242 const query: FindOptions = { 213 const query: FindOptions = {
243 where: { 214 where: {
244 url 215 url
@@ -250,7 +221,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
250 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query) 221 return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
251 } 222 }
252 223
253 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction) { 224 static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
254 const query: FindOptions = { 225 const query: FindOptions = {
255 where: { 226 where: {
256 url 227 url
@@ -273,7 +244,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
273 start: number, 244 start: number,
274 count: number, 245 count: number,
275 sort: string, 246 sort: string,
276 user?: UserModel 247 user?: MUserAccountId
277 }) { 248 }) {
278 const { videoId, start, count, sort, user } = parameters 249 const { videoId, start, count, sort, user } = parameters
279 250
@@ -314,7 +285,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
314 static async listThreadCommentsForApi (parameters: { 285 static async listThreadCommentsForApi (parameters: {
315 videoId: number, 286 videoId: number,
316 threadId: number, 287 threadId: number,
317 user?: UserModel 288 user?: MUserAccountId
318 }) { 289 }) {
319 const { videoId, threadId, user } = parameters 290 const { videoId, threadId, user } = parameters
320 291
@@ -353,7 +324,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
353 }) 324 })
354 } 325 }
355 326
356 static listThreadParentComments (comment: VideoCommentModel, t: Transaction, order: 'ASC' | 'DESC' = 'ASC') { 327 static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
357 const query = { 328 const query = {
358 order: [ [ 'createdAt', order ] ] as Order, 329 order: [ [ 'createdAt', order ] ] as Order,
359 where: { 330 where: {
@@ -389,10 +360,10 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
389 transaction: t 360 transaction: t
390 } 361 }
391 362
392 return VideoCommentModel.findAndCountAll(query) 363 return VideoCommentModel.findAndCountAll<MComment>(query)
393 } 364 }
394 365
395 static listForFeed (start: number, count: number, videoId?: number) { 366 static listForFeed (start: number, count: number, videoId?: number): Bluebird<MCommentOwnerVideoFeed[]> {
396 const query = { 367 const query = {
397 order: [ [ 'createdAt', 'DESC' ] ] as Order, 368 order: [ [ 'createdAt', 'DESC' ] ] as Order,
398 offset: start, 369 offset: start,
@@ -506,7 +477,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
506 return uniq(result) 477 return uniq(result)
507 } 478 }
508 479
509 toFormattedJSON () { 480 toFormattedJSON (this: MCommentFormattable) {
510 return { 481 return {
511 id: this.id, 482 id: this.id,
512 url: this.url, 483 url: this.url,
@@ -521,7 +492,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
521 } as VideoComment 492 } as VideoComment
522 } 493 }
523 494
524 toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject { 495 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject {
525 let inReplyTo: string 496 let inReplyTo: string
526 // New thread, so in AS we reply to the video 497 // New thread, so in AS we reply to the video
527 if (this.inReplyToCommentId === null) { 498 if (this.inReplyToCommentId === null) {