]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
74889a71 | 3 | import { CONFIG } from '../../initializers' |
8e13fa7d | 4 | import { isVideoAbuseReasonValid } from '../../helpers' |
55fa55a9 | 5 | |
74889a71 | 6 | import { addMethodsToModel, getSort } from '../utils' |
e02643f3 | 7 | import { |
e02643f3 C |
8 | VideoAbuseInstance, |
9 | VideoAbuseAttributes, | |
10 | ||
11 | VideoAbuseMethods | |
12 | } from './video-abuse-interface' | |
21e0727a | 13 | import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object' |
e02643f3 C |
14 | |
15 | let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes> | |
0aef76c4 | 16 | let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON |
e02643f3 | 17 | let listForApi: VideoAbuseMethods.ListForApi |
21e0727a | 18 | let toActivityPubObject: VideoAbuseMethods.ToActivityPubObject |
e02643f3 | 19 | |
127944aa C |
20 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
21 | VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse', | |
55fa55a9 | 22 | { |
55fa55a9 C |
23 | reason: { |
24 | type: DataTypes.STRING, | |
25 | allowNull: false, | |
26 | validate: { | |
075f16ca | 27 | reasonValid: value => { |
65fcc311 | 28 | const res = isVideoAbuseReasonValid(value) |
55fa55a9 C |
29 | if (res === false) throw new Error('Video abuse reason is not valid.') |
30 | } | |
31 | } | |
32 | } | |
33 | }, | |
34 | { | |
35 | indexes: [ | |
36 | { | |
37 | fields: [ 'videoId' ] | |
38 | }, | |
39 | { | |
8e13fa7d | 40 | fields: [ 'reporterAccountId' ] |
55fa55a9 | 41 | } |
e02643f3 | 42 | ] |
55fa55a9 C |
43 | } |
44 | ) | |
45 | ||
e02643f3 C |
46 | const classMethods = [ |
47 | associate, | |
48 | ||
49 | listForApi | |
50 | ] | |
51 | const instanceMethods = [ | |
21e0727a C |
52 | toFormattedJSON, |
53 | toActivityPubObject | |
e02643f3 C |
54 | ] |
55 | addMethodsToModel(VideoAbuse, classMethods, instanceMethods) | |
56 | ||
55fa55a9 C |
57 | return VideoAbuse |
58 | } | |
59 | ||
e02643f3 C |
60 | // ------------------------------ METHODS ------------------------------ |
61 | ||
0aef76c4 | 62 | toFormattedJSON = function (this: VideoAbuseInstance) { |
60862425 | 63 | let reporterServerHost |
e02643f3 | 64 | |
8e13fa7d C |
65 | if (this.Account.Server) { |
66 | reporterServerHost = this.Account.Server.host | |
e02643f3 C |
67 | } else { |
68 | // It means it's our video | |
60862425 | 69 | reporterServerHost = CONFIG.WEBSERVER.HOST |
e02643f3 C |
70 | } |
71 | ||
72 | const json = { | |
73 | id: this.id, | |
e02643f3 | 74 | reason: this.reason, |
8e13fa7d C |
75 | reporterUsername: this.Account.name, |
76 | reporterServerHost, | |
77 | videoId: this.Video.id, | |
78 | videoUUID: this.Video.uuid, | |
79 | videoName: this.Video.name, | |
e02643f3 C |
80 | createdAt: this.createdAt |
81 | } | |
82 | ||
83 | return json | |
84 | } | |
21e0727a C |
85 | |
86 | toActivityPubObject = function (this: VideoAbuseInstance) { | |
87 | const videoAbuseObject: VideoAbuseObject = { | |
88 | type: 'Flag' as 'Flag', | |
89 | content: this.reason, | |
90 | object: this.Video.url | |
91 | } | |
92 | ||
93 | return videoAbuseObject | |
94 | } | |
e02643f3 C |
95 | |
96 | // ------------------------------ STATICS ------------------------------ | |
55fa55a9 C |
97 | |
98 | function associate (models) { | |
8e13fa7d | 99 | VideoAbuse.belongsTo(models.Account, { |
55fa55a9 | 100 | foreignKey: { |
8e13fa7d | 101 | name: 'reporterAccountId', |
74bb2cb8 | 102 | allowNull: false |
55fa55a9 | 103 | }, |
0a6658fd | 104 | onDelete: 'CASCADE' |
55fa55a9 C |
105 | }) |
106 | ||
e02643f3 | 107 | VideoAbuse.belongsTo(models.Video, { |
55fa55a9 C |
108 | foreignKey: { |
109 | name: 'videoId', | |
110 | allowNull: false | |
111 | }, | |
0a6658fd | 112 | onDelete: 'CASCADE' |
55fa55a9 C |
113 | }) |
114 | } | |
115 | ||
6fcd19ba | 116 | listForApi = function (start: number, count: number, sort: string) { |
55fa55a9 C |
117 | const query = { |
118 | offset: start, | |
119 | limit: count, | |
65fcc311 | 120 | order: [ getSort(sort) ], |
55fa55a9 C |
121 | include: [ |
122 | { | |
8e13fa7d C |
123 | model: VideoAbuse['sequelize'].models.Account, |
124 | required: true, | |
125 | include: [ | |
126 | { | |
127 | model: VideoAbuse['sequelize'].models.Server, | |
128 | required: false | |
129 | } | |
130 | ] | |
131 | }, | |
132 | { | |
133 | model: VideoAbuse['sequelize'].models.Video, | |
134 | required: true | |
55fa55a9 C |
135 | } |
136 | ] | |
137 | } | |
138 | ||
6fcd19ba C |
139 | return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => { |
140 | return { total: count, data: rows } | |
55fa55a9 C |
141 | }) |
142 | } |