]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-abuse.ts
d0ee969fb10c89f13fdba06c1cd880c9ce60507c
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
1 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
3 import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos'
4 import { CONFIG } from '../../initializers'
5 import { AccountModel } from '../account/account'
6 import { ServerModel } from '../server/server'
7 import { getSort, throwIfNotValid } from '../utils'
8 import { VideoModel } from './video'
9
10 @Table({
11 tableName: 'videoAbuse',
12 indexes: [
13 {
14 fields: [ 'videoId' ]
15 },
16 {
17 fields: [ 'reporterAccountId' ]
18 }
19 ]
20 })
21 export class VideoAbuseModel extends Model<VideoAbuseModel> {
22
23 @AllowNull(false)
24 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
25 @Column
26 reason: string
27
28 @CreatedAt
29 createdAt: Date
30
31 @UpdatedAt
32 updatedAt: Date
33
34 @ForeignKey(() => AccountModel)
35 @Column
36 reporterAccountId: number
37
38 @BelongsTo(() => AccountModel, {
39 foreignKey: {
40 allowNull: false
41 },
42 onDelete: 'cascade'
43 })
44 Account: AccountModel
45
46 @ForeignKey(() => VideoModel)
47 @Column
48 videoId: number
49
50 @BelongsTo(() => VideoModel, {
51 foreignKey: {
52 allowNull: false
53 },
54 onDelete: 'cascade'
55 })
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 }
80
81 return VideoAbuseModel.findAndCountAll(query)
82 .then(({ rows, count }) => {
83 return { total: count, data: rows }
84 })
85 }
86
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 }
116 }