]>
Commit | Line | Data |
---|---|---|
268eebed C |
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' | |
3fd3ab2d | 15 | import { VideoAbuseObject } from '../../../shared/models/activitypub/objects' |
19a3b914 | 16 | import { VideoAbuse } from '../../../shared/models/videos' |
268eebed C |
17 | import { |
18 | isVideoAbuseModerationCommentValid, | |
19 | isVideoAbuseReasonValid, | |
20 | isVideoAbuseStateValid | |
21 | } from '../../helpers/custom-validators/video-abuses' | |
ba75d268 | 22 | import { Emailer } from '../../lib/emailer' |
3fd3ab2d | 23 | import { AccountModel } from '../account/account' |
3fd3ab2d C |
24 | import { getSort, throwIfNotValid } from '../utils' |
25 | import { VideoModel } from './video' | |
268eebed C |
26 | import { VideoAbuseState } from '../../../shared' |
27 | import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers' | |
3fd3ab2d C |
28 | |
29 | @Table({ | |
30 | tableName: 'videoAbuse', | |
31 | indexes: [ | |
55fa55a9 | 32 | { |
3fd3ab2d | 33 | fields: [ 'videoId' ] |
55fa55a9 C |
34 | }, |
35 | { | |
3fd3ab2d | 36 | fields: [ 'reporterAccountId' ] |
55fa55a9 | 37 | } |
e02643f3 | 38 | ] |
3fd3ab2d C |
39 | }) |
40 | export class VideoAbuseModel extends Model<VideoAbuseModel> { | |
e02643f3 | 41 | |
3fd3ab2d C |
42 | @AllowNull(false) |
43 | @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) | |
44 | @Column | |
45 | reason: string | |
21e0727a | 46 | |
268eebed C |
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 | ||
3fd3ab2d C |
59 | @CreatedAt |
60 | createdAt: Date | |
21e0727a | 61 | |
3fd3ab2d C |
62 | @UpdatedAt |
63 | updatedAt: Date | |
e02643f3 | 64 | |
3fd3ab2d C |
65 | @ForeignKey(() => AccountModel) |
66 | @Column | |
67 | reporterAccountId: number | |
55fa55a9 | 68 | |
3fd3ab2d | 69 | @BelongsTo(() => AccountModel, { |
55fa55a9 | 70 | foreignKey: { |
74bb2cb8 | 71 | allowNull: false |
55fa55a9 | 72 | }, |
3fd3ab2d | 73 | onDelete: 'cascade' |
55fa55a9 | 74 | }) |
3fd3ab2d C |
75 | Account: AccountModel |
76 | ||
77 | @ForeignKey(() => VideoModel) | |
78 | @Column | |
79 | videoId: number | |
55fa55a9 | 80 | |
3fd3ab2d | 81 | @BelongsTo(() => VideoModel, { |
55fa55a9 | 82 | foreignKey: { |
55fa55a9 C |
83 | allowNull: false |
84 | }, | |
3fd3ab2d | 85 | onDelete: 'cascade' |
55fa55a9 | 86 | }) |
3fd3ab2d C |
87 | Video: VideoModel |
88 | ||
268eebed C |
89 | static loadByIdAndVideoId (id: number, videoId: number) { |
90 | const query = { | |
91 | where: { | |
92 | id, | |
93 | videoId | |
94 | } | |
95 | } | |
96 | return VideoAbuseModel.findOne(query) | |
97 | } | |
98 | ||
3fd3ab2d C |
99 | static listForApi (start: number, count: number, sort: string) { |
100 | const query = { | |
101 | offset: start, | |
102 | limit: count, | |
3bb6c526 | 103 | order: getSort(sort), |
3fd3ab2d C |
104 | include: [ |
105 | { | |
106 | model: AccountModel, | |
50d6de9c | 107 | required: true |
3fd3ab2d C |
108 | }, |
109 | { | |
110 | model: VideoModel, | |
111 | required: true | |
112 | } | |
113 | ] | |
114 | } | |
55fa55a9 | 115 | |
3fd3ab2d C |
116 | return VideoAbuseModel.findAndCountAll(query) |
117 | .then(({ rows, count }) => { | |
118 | return { total: count, data: rows } | |
119 | }) | |
55fa55a9 C |
120 | } |
121 | ||
19a3b914 | 122 | toFormattedJSON (): VideoAbuse { |
3fd3ab2d C |
123 | return { |
124 | id: this.id, | |
125 | reason: this.reason, | |
19a3b914 | 126 | reporterAccount: this.Account.toFormattedJSON(), |
268eebed C |
127 | state: { |
128 | id: this.state, | |
129 | label: VideoAbuseModel.getStateLabel(this.state) | |
130 | }, | |
131 | moderationComment: this.moderationComment, | |
19a3b914 C |
132 | video: { |
133 | id: this.Video.id, | |
134 | uuid: this.Video.uuid, | |
19a3b914 C |
135 | name: this.Video.name |
136 | }, | |
3fd3ab2d C |
137 | createdAt: this.createdAt |
138 | } | |
139 | } | |
140 | ||
141 | toActivityPubObject (): VideoAbuseObject { | |
142 | return { | |
143 | type: 'Flag' as 'Flag', | |
144 | content: this.reason, | |
145 | object: this.Video.url | |
146 | } | |
147 | } | |
268eebed C |
148 | |
149 | private static getStateLabel (id: number) { | |
150 | return VIDEO_ABUSE_STATES[id] || 'Unknown' | |
151 | } | |
55fa55a9 | 152 | } |