Check activities host
[oweals/peertube.git] / server / models / video / video-abuse.ts
1 import {
2   AfterCreate,
3   AllowNull,
4   BelongsTo,
5   Column,
6   CreatedAt,
7   DataType,
8   Default,
9   ForeignKey,
10   Is,
11   Model,
12   Table,
13   UpdatedAt
14 } from 'sequelize-typescript'
15 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
16 import { VideoAbuse } from '../../../shared/models/videos'
17 import {
18   isVideoAbuseModerationCommentValid,
19   isVideoAbuseReasonValid,
20   isVideoAbuseStateValid
21 } from '../../helpers/custom-validators/video-abuses'
22 import { Emailer } from '../../lib/emailer'
23 import { AccountModel } from '../account/account'
24 import { getSort, throwIfNotValid } from '../utils'
25 import { VideoModel } from './video'
26 import { VideoAbuseState } from '../../../shared'
27 import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers'
28
29 @Table({
30   tableName: 'videoAbuse',
31   indexes: [
32     {
33       fields: [ 'videoId' ]
34     },
35     {
36       fields: [ 'reporterAccountId' ]
37     }
38   ]
39 })
40 export class VideoAbuseModel extends Model<VideoAbuseModel> {
41
42   @AllowNull(false)
43   @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
44   @Column
45   reason: string
46
47   @AllowNull(false)
48   @Default(null)
49   @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
50   @Column
51   state: VideoAbuseState
52
53   @AllowNull(true)
54   @Default(null)
55   @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment'))
56   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
57   moderationComment: string
58
59   @CreatedAt
60   createdAt: Date
61
62   @UpdatedAt
63   updatedAt: Date
64
65   @ForeignKey(() => AccountModel)
66   @Column
67   reporterAccountId: number
68
69   @BelongsTo(() => AccountModel, {
70     foreignKey: {
71       allowNull: false
72     },
73     onDelete: 'cascade'
74   })
75   Account: AccountModel
76
77   @ForeignKey(() => VideoModel)
78   @Column
79   videoId: number
80
81   @BelongsTo(() => VideoModel, {
82     foreignKey: {
83       allowNull: false
84     },
85     onDelete: 'cascade'
86   })
87   Video: VideoModel
88
89   @AfterCreate
90   static sendEmailNotification (instance: VideoAbuseModel) {
91     return Emailer.Instance.addVideoAbuseReportJob(instance.videoId)
92   }
93
94   static loadByIdAndVideoId (id: number, videoId: number) {
95     const query = {
96       where: {
97         id,
98         videoId
99       }
100     }
101     return VideoAbuseModel.findOne(query)
102   }
103
104   static listForApi (start: number, count: number, sort: string) {
105     const query = {
106       offset: start,
107       limit: count,
108       order: getSort(sort),
109       include: [
110         {
111           model: AccountModel,
112           required: true
113         },
114         {
115           model: VideoModel,
116           required: true
117         }
118       ]
119     }
120
121     return VideoAbuseModel.findAndCountAll(query)
122       .then(({ rows, count }) => {
123         return { total: count, data: rows }
124       })
125   }
126
127   toFormattedJSON (): VideoAbuse {
128     return {
129       id: this.id,
130       reason: this.reason,
131       reporterAccount: this.Account.toFormattedJSON(),
132       state: {
133         id: this.state,
134         label: VideoAbuseModel.getStateLabel(this.state)
135       },
136       moderationComment: this.moderationComment,
137       video: {
138         id: this.Video.id,
139         uuid: this.Video.uuid,
140         name: this.Video.name
141       },
142       createdAt: this.createdAt
143     }
144   }
145
146   toActivityPubObject (): VideoAbuseObject {
147     return {
148       type: 'Flag' as 'Flag',
149       content: this.reason,
150       object: this.Video.url
151     }
152   }
153
154   private static getStateLabel (id: number) {
155     return VIDEO_ABUSE_STATES[id] || 'Unknown'
156   }
157 }