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