]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-abuse.ts
Change video abuse API response
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
1 import { AfterCreate, AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
3 import { VideoAbuse } from '../../../shared/models/videos'
4 import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos'
5 import { Emailer } from '../../lib/emailer'
6 import { AccountModel } from '../account/account'
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 @AfterCreate
59 static sendEmailNotification (instance: VideoAbuseModel) {
60 return Emailer.Instance.addVideoAbuseReport(instance.videoId)
61 }
62
63 static listForApi (start: number, count: number, sort: string) {
64 const query = {
65 offset: start,
66 limit: count,
67 order: getSort(sort),
68 include: [
69 {
70 model: AccountModel,
71 required: true
72 },
73 {
74 model: VideoModel,
75 required: true
76 }
77 ]
78 }
79
80 return VideoAbuseModel.findAndCountAll(query)
81 .then(({ rows, count }) => {
82 return { total: count, data: rows }
83 })
84 }
85
86 toFormattedJSON (): VideoAbuse {
87 return {
88 id: this.id,
89 reason: this.reason,
90 reporterAccount: this.Account.toFormattedJSON(),
91 video: {
92 id: this.Video.id,
93 uuid: this.Video.uuid,
94 url: this.Video.url,
95 name: this.Video.name
96 },
97 createdAt: this.createdAt
98 }
99 }
100
101 toActivityPubObject (): VideoAbuseObject {
102 return {
103 type: 'Flag' as 'Flag',
104 content: this.reason,
105 object: this.Video.url
106 }
107 }
108 }