]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-abuse.ts
Stronger model typings
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
CommitLineData
1506307f 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3fd3ab2d 2import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
19a3b914 3import { VideoAbuse } from '../../../shared/models/videos'
268eebed
C
4import {
5 isVideoAbuseModerationCommentValid,
6 isVideoAbuseReasonValid,
7 isVideoAbuseStateValid
8} from '../../helpers/custom-validators/video-abuses'
3fd3ab2d 9import { AccountModel } from '../account/account'
3fd3ab2d
C
10import { getSort, throwIfNotValid } from '../utils'
11import { VideoModel } from './video'
268eebed 12import { VideoAbuseState } from '../../../shared'
74dc3bca 13import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
453e83ea
C
14import { MVideoAbuse, MVideoAbuseAccountVideo, MVideoAbuseVideo } from '../../typings/models'
15import * as Bluebird from 'bluebird'
3fd3ab2d
C
16
17@Table({
18 tableName: 'videoAbuse',
19 indexes: [
55fa55a9 20 {
3fd3ab2d 21 fields: [ 'videoId' ]
55fa55a9
C
22 },
23 {
3fd3ab2d 24 fields: [ 'reporterAccountId' ]
55fa55a9 25 }
e02643f3 26 ]
3fd3ab2d
C
27})
28export class VideoAbuseModel extends Model<VideoAbuseModel> {
e02643f3 29
3fd3ab2d 30 @AllowNull(false)
1506307f 31 @Default(null)
3fd3ab2d 32 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
1506307f 33 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
3fd3ab2d 34 reason: string
21e0727a 35
268eebed
C
36 @AllowNull(false)
37 @Default(null)
38 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
39 @Column
40 state: VideoAbuseState
41
42 @AllowNull(true)
43 @Default(null)
1735c825 44 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
268eebed
C
45 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
46 moderationComment: string
47
3fd3ab2d
C
48 @CreatedAt
49 createdAt: Date
21e0727a 50
3fd3ab2d
C
51 @UpdatedAt
52 updatedAt: Date
e02643f3 53
3fd3ab2d
C
54 @ForeignKey(() => AccountModel)
55 @Column
56 reporterAccountId: number
55fa55a9 57
3fd3ab2d 58 @BelongsTo(() => AccountModel, {
55fa55a9 59 foreignKey: {
74bb2cb8 60 allowNull: false
55fa55a9 61 },
3fd3ab2d 62 onDelete: 'cascade'
55fa55a9 63 })
3fd3ab2d
C
64 Account: AccountModel
65
66 @ForeignKey(() => VideoModel)
67 @Column
68 videoId: number
55fa55a9 69
3fd3ab2d 70 @BelongsTo(() => VideoModel, {
55fa55a9 71 foreignKey: {
55fa55a9
C
72 allowNull: false
73 },
3fd3ab2d 74 onDelete: 'cascade'
55fa55a9 75 })
3fd3ab2d
C
76 Video: VideoModel
77
453e83ea 78 static loadByIdAndVideoId (id: number, videoId: number): Bluebird<MVideoAbuse> {
268eebed
C
79 const query = {
80 where: {
81 id,
82 videoId
83 }
84 }
85 return VideoAbuseModel.findOne(query)
86 }
87
3fd3ab2d
C
88 static listForApi (start: number, count: number, sort: string) {
89 const query = {
90 offset: start,
91 limit: count,
3bb6c526 92 order: getSort(sort),
3fd3ab2d
C
93 include: [
94 {
95 model: AccountModel,
50d6de9c 96 required: true
3fd3ab2d
C
97 },
98 {
99 model: VideoModel,
100 required: true
101 }
102 ]
103 }
55fa55a9 104
3fd3ab2d
C
105 return VideoAbuseModel.findAndCountAll(query)
106 .then(({ rows, count }) => {
107 return { total: count, data: rows }
108 })
55fa55a9
C
109 }
110
453e83ea 111 toFormattedJSON (this: MVideoAbuseAccountVideo): VideoAbuse {
3fd3ab2d
C
112 return {
113 id: this.id,
114 reason: this.reason,
19a3b914 115 reporterAccount: this.Account.toFormattedJSON(),
268eebed
C
116 state: {
117 id: this.state,
118 label: VideoAbuseModel.getStateLabel(this.state)
119 },
120 moderationComment: this.moderationComment,
19a3b914
C
121 video: {
122 id: this.Video.id,
123 uuid: this.Video.uuid,
19a3b914
C
124 name: this.Video.name
125 },
3fd3ab2d
C
126 createdAt: this.createdAt
127 }
128 }
129
453e83ea 130 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
3fd3ab2d
C
131 return {
132 type: 'Flag' as 'Flag',
133 content: this.reason,
134 object: this.Video.url
135 }
136 }
268eebed
C
137
138 private static getStateLabel (id: number) {
139 return VIDEO_ABUSE_STATES[id] || 'Unknown'
140 }
55fa55a9 141}