]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-abuse.ts
Save
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
CommitLineData
3fd3ab2d
C
1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
3import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos'
74889a71 4import { CONFIG } from '../../initializers'
3fd3ab2d
C
5import { AccountModel } from '../account/account'
6import { ServerModel } from '../server/server'
7import { getSort, throwIfNotValid } from '../utils'
8import { VideoModel } from './video'
9
10@Table({
11 tableName: 'videoAbuse',
12 indexes: [
55fa55a9 13 {
3fd3ab2d 14 fields: [ 'videoId' ]
55fa55a9
C
15 },
16 {
3fd3ab2d 17 fields: [ 'reporterAccountId' ]
55fa55a9 18 }
e02643f3 19 ]
3fd3ab2d
C
20})
21export class VideoAbuseModel extends Model<VideoAbuseModel> {
e02643f3 22
3fd3ab2d
C
23 @AllowNull(false)
24 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
25 @Column
26 reason: string
21e0727a 27
3fd3ab2d
C
28 @CreatedAt
29 createdAt: Date
21e0727a 30
3fd3ab2d
C
31 @UpdatedAt
32 updatedAt: Date
e02643f3 33
3fd3ab2d
C
34 @ForeignKey(() => AccountModel)
35 @Column
36 reporterAccountId: number
55fa55a9 37
3fd3ab2d 38 @BelongsTo(() => AccountModel, {
55fa55a9 39 foreignKey: {
74bb2cb8 40 allowNull: false
55fa55a9 41 },
3fd3ab2d 42 onDelete: 'cascade'
55fa55a9 43 })
3fd3ab2d
C
44 Account: AccountModel
45
46 @ForeignKey(() => VideoModel)
47 @Column
48 videoId: number
55fa55a9 49
3fd3ab2d 50 @BelongsTo(() => VideoModel, {
55fa55a9 51 foreignKey: {
55fa55a9
C
52 allowNull: false
53 },
3fd3ab2d 54 onDelete: 'cascade'
55fa55a9 55 })
3fd3ab2d
C
56 Video: VideoModel
57
58 static listForApi (start: number, count: number, sort: string) {
59 const query = {
60 offset: start,
61 limit: count,
62 order: [ getSort(sort) ],
63 include: [
64 {
65 model: AccountModel,
66 required: true,
67 include: [
68 {
69 model: ServerModel,
70 required: false
71 }
72 ]
73 },
74 {
75 model: VideoModel,
76 required: true
77 }
78 ]
79 }
55fa55a9 80
3fd3ab2d
C
81 return VideoAbuseModel.findAndCountAll(query)
82 .then(({ rows, count }) => {
83 return { total: count, data: rows }
84 })
55fa55a9
C
85 }
86
3fd3ab2d
C
87 toFormattedJSON () {
88 let reporterServerHost
89
90 if (this.Account.Server) {
91 reporterServerHost = this.Account.Server.host
92 } else {
93 // It means it's our video
94 reporterServerHost = CONFIG.WEBSERVER.HOST
95 }
96
97 return {
98 id: this.id,
99 reason: this.reason,
100 reporterUsername: this.Account.name,
101 reporterServerHost,
102 videoId: this.Video.id,
103 videoUUID: this.Video.uuid,
104 videoName: this.Video.name,
105 createdAt: this.createdAt
106 }
107 }
108
109 toActivityPubObject (): VideoAbuseObject {
110 return {
111 type: 'Flag' as 'Flag',
112 content: this.reason,
113 object: this.Video.url
114 }
115 }
55fa55a9 116}