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