1 import * as Sequelize from 'sequelize'
3 AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table,
5 } from 'sequelize-typescript'
6 import { VideoComment } from '../../../shared/models/videos/video-comment.model'
7 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
8 import { CONSTRAINTS_FIELDS } from '../../initializers'
9 import { ActorModel } from '../activitypub/actor'
10 import { getSort, throwIfNotValid } from '../utils'
11 import { VideoModel } from './video'
14 WITH_ACTOR = 'WITH_ACTOR'
18 [ScopeNames.WITH_ACTOR]: {
25 tableName: 'videoComment',
31 fields: [ 'videoId', 'originCommentId' ]
35 export class VideoCommentModel extends Model<VideoCommentModel> {
43 @Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
44 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
48 @Column(DataType.TEXT)
51 @ForeignKey(() => VideoCommentModel)
53 originCommentId: number
55 @BelongsTo(() => VideoCommentModel, {
61 OriginVideoComment: VideoCommentModel
63 @ForeignKey(() => VideoCommentModel)
65 inReplyToCommentId: number
67 @BelongsTo(() => VideoCommentModel, {
73 InReplyToVideoComment: VideoCommentModel
75 @ForeignKey(() => VideoModel)
79 @BelongsTo(() => VideoModel, {
87 @ForeignKey(() => ActorModel)
91 @BelongsTo(() => ActorModel, {
100 static sendDeleteIfOwned (instance: VideoCommentModel) {
105 static loadById (id: number, t?: Sequelize.Transaction) {
106 const query: IFindOptions<VideoCommentModel> = {
112 if (t !== undefined) query.transaction = t
114 return VideoCommentModel.findOne(query)
117 static loadByUrl (url: string, t?: Sequelize.Transaction) {
118 const query: IFindOptions<VideoCommentModel> = {
124 if (t !== undefined) query.transaction = t
126 return VideoCommentModel.findOne(query)
129 static listThreadsForApi (videoId: number, start: number, count: number, sort: string) {
133 order: [ getSort(sort) ],
139 return VideoCommentModel
140 .scope([ ScopeNames.WITH_ACTOR ])
141 .findAndCountAll(query)
142 .then(({ rows, count }) => {
143 return { total: count, data: rows }
147 static listThreadCommentsForApi (videoId: number, threadId: number) {
149 order: [ 'id', 'ASC' ],
152 [ Sequelize.Op.or ]: [
154 { originCommentId: threadId }
159 return VideoCommentModel
160 .scope([ ScopeNames.WITH_ACTOR ])
161 .findAndCountAll(query)
162 .then(({ rows, count }) => {
163 return { total: count, data: rows }
172 threadId: this.originCommentId || this.id,
173 inReplyToCommentId: this.inReplyToCommentId,
174 videoId: this.videoId,
175 createdAt: this.createdAt,
176 updatedAt: this.updatedAt