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