]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-abuse.ts
Add video abuse to activity pub
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
CommitLineData
e02643f3
C
1import * as Sequelize from 'sequelize'
2
74889a71 3import { CONFIG } from '../../initializers'
8e13fa7d 4import { isVideoAbuseReasonValid } from '../../helpers'
55fa55a9 5
74889a71 6import { addMethodsToModel, getSort } from '../utils'
e02643f3 7import {
e02643f3
C
8 VideoAbuseInstance,
9 VideoAbuseAttributes,
10
11 VideoAbuseMethods
12} from './video-abuse-interface'
13
14let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
0aef76c4 15let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
e02643f3
C
16let listForApi: VideoAbuseMethods.ListForApi
17
127944aa
C
18export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
19 VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
55fa55a9 20 {
55fa55a9
C
21 reason: {
22 type: DataTypes.STRING,
23 allowNull: false,
24 validate: {
075f16ca 25 reasonValid: value => {
65fcc311 26 const res = isVideoAbuseReasonValid(value)
55fa55a9
C
27 if (res === false) throw new Error('Video abuse reason is not valid.')
28 }
29 }
30 }
31 },
32 {
33 indexes: [
34 {
35 fields: [ 'videoId' ]
36 },
37 {
8e13fa7d 38 fields: [ 'reporterAccountId' ]
55fa55a9 39 }
e02643f3 40 ]
55fa55a9
C
41 }
42 )
43
e02643f3
C
44 const classMethods = [
45 associate,
46
47 listForApi
48 ]
49 const instanceMethods = [
0aef76c4 50 toFormattedJSON
e02643f3
C
51 ]
52 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
53
55fa55a9
C
54 return VideoAbuse
55}
56
e02643f3
C
57// ------------------------------ METHODS ------------------------------
58
0aef76c4 59toFormattedJSON = function (this: VideoAbuseInstance) {
60862425 60 let reporterServerHost
e02643f3 61
8e13fa7d
C
62 if (this.Account.Server) {
63 reporterServerHost = this.Account.Server.host
e02643f3
C
64 } else {
65 // It means it's our video
60862425 66 reporterServerHost = CONFIG.WEBSERVER.HOST
e02643f3
C
67 }
68
69 const json = {
70 id: this.id,
e02643f3 71 reason: this.reason,
8e13fa7d
C
72 reporterUsername: this.Account.name,
73 reporterServerHost,
74 videoId: this.Video.id,
75 videoUUID: this.Video.uuid,
76 videoName: this.Video.name,
e02643f3
C
77 createdAt: this.createdAt
78 }
79
80 return json
81}
82
83// ------------------------------ STATICS ------------------------------
55fa55a9
C
84
85function associate (models) {
8e13fa7d 86 VideoAbuse.belongsTo(models.Account, {
55fa55a9 87 foreignKey: {
8e13fa7d 88 name: 'reporterAccountId',
55fa55a9
C
89 allowNull: true
90 },
0a6658fd 91 onDelete: 'CASCADE'
55fa55a9
C
92 })
93
e02643f3 94 VideoAbuse.belongsTo(models.Video, {
55fa55a9
C
95 foreignKey: {
96 name: 'videoId',
97 allowNull: false
98 },
0a6658fd 99 onDelete: 'CASCADE'
55fa55a9
C
100 })
101}
102
6fcd19ba 103listForApi = function (start: number, count: number, sort: string) {
55fa55a9
C
104 const query = {
105 offset: start,
106 limit: count,
65fcc311 107 order: [ getSort(sort) ],
55fa55a9
C
108 include: [
109 {
8e13fa7d
C
110 model: VideoAbuse['sequelize'].models.Account,
111 required: true,
112 include: [
113 {
114 model: VideoAbuse['sequelize'].models.Server,
115 required: false
116 }
117 ]
118 },
119 {
120 model: VideoAbuse['sequelize'].models.Video,
121 required: true
55fa55a9
C
122 }
123 ]
124 }
125
6fcd19ba
C
126 return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
127 return { total: count, data: rows }
55fa55a9
C
128 })
129}