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