]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
e02643f3 | 2 | import * as Sequelize from 'sequelize' |
b0f9f39e | 3 | import * as Promise from 'bluebird' |
65fcc311 | 4 | |
74889a71 C |
5 | import { getSort } from '../utils' |
6 | import { USER_ROLES } from '../../initializers' | |
65fcc311 C |
7 | import { |
8 | cryptPassword, | |
9 | comparePassword, | |
10 | isUserPasswordValid, | |
11 | isUserUsernameValid, | |
b0f9f39e C |
12 | isUserDisplayNSFWValid, |
13 | isUserVideoQuotaValid | |
74889a71 | 14 | } from '../../helpers' |
9bd26629 | 15 | |
74889a71 | 16 | import { addMethodsToModel } from '../utils' |
e02643f3 | 17 | import { |
e02643f3 C |
18 | UserInstance, |
19 | UserAttributes, | |
20 | ||
21 | UserMethods | |
22 | } from './user-interface' | |
23 | ||
24 | let User: Sequelize.Model<UserInstance, UserAttributes> | |
25 | let isPasswordMatch: UserMethods.IsPasswordMatch | |
0aef76c4 | 26 | let toFormattedJSON: UserMethods.ToFormattedJSON |
e02643f3 C |
27 | let isAdmin: UserMethods.IsAdmin |
28 | let countTotal: UserMethods.CountTotal | |
29 | let getByUsername: UserMethods.GetByUsername | |
e02643f3 C |
30 | let listForApi: UserMethods.ListForApi |
31 | let loadById: UserMethods.LoadById | |
32 | let loadByUsername: UserMethods.LoadByUsername | |
72c7248b | 33 | let loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels |
e02643f3 | 34 | let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail |
b0f9f39e | 35 | let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo |
e02643f3 | 36 | |
127944aa C |
37 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
38 | User = sequelize.define<UserInstance, UserAttributes>('User', | |
feb4bdfd C |
39 | { |
40 | password: { | |
67bf9b96 C |
41 | type: DataTypes.STRING, |
42 | allowNull: false, | |
43 | validate: { | |
075f16ca | 44 | passwordValid: value => { |
65fcc311 | 45 | const res = isUserPasswordValid(value) |
67bf9b96 C |
46 | if (res === false) throw new Error('Password not valid.') |
47 | } | |
48 | } | |
feb4bdfd C |
49 | }, |
50 | username: { | |
67bf9b96 C |
51 | type: DataTypes.STRING, |
52 | allowNull: false, | |
53 | validate: { | |
075f16ca | 54 | usernameValid: value => { |
65fcc311 | 55 | const res = isUserUsernameValid(value) |
67bf9b96 C |
56 | if (res === false) throw new Error('Username not valid.') |
57 | } | |
58 | } | |
feb4bdfd | 59 | }, |
ad4a8a1c | 60 | email: { |
5804c0db | 61 | type: DataTypes.STRING(400), |
ad4a8a1c C |
62 | allowNull: false, |
63 | validate: { | |
64 | isEmail: true | |
65 | } | |
66 | }, | |
1d49e1e2 C |
67 | displayNSFW: { |
68 | type: DataTypes.BOOLEAN, | |
69 | allowNull: false, | |
70 | defaultValue: false, | |
71 | validate: { | |
075f16ca | 72 | nsfwValid: value => { |
65fcc311 | 73 | const res = isUserDisplayNSFWValid(value) |
1d49e1e2 C |
74 | if (res === false) throw new Error('Display NSFW is not valid.') |
75 | } | |
76 | } | |
77 | }, | |
feb4bdfd | 78 | role: { |
65fcc311 | 79 | type: DataTypes.ENUM(values(USER_ROLES)), |
67bf9b96 | 80 | allowNull: false |
b0f9f39e C |
81 | }, |
82 | videoQuota: { | |
83 | type: DataTypes.BIGINT, | |
84 | allowNull: false, | |
85 | validate: { | |
86 | videoQuotaValid: value => { | |
87 | const res = isUserVideoQuotaValid(value) | |
88 | if (res === false) throw new Error('Video quota is not valid.') | |
89 | } | |
90 | } | |
feb4bdfd C |
91 | } |
92 | }, | |
93 | { | |
319d072e C |
94 | indexes: [ |
95 | { | |
5d67f289 C |
96 | fields: [ 'username' ], |
97 | unique: true | |
ad4a8a1c C |
98 | }, |
99 | { | |
100 | fields: [ 'email' ], | |
101 | unique: true | |
319d072e C |
102 | } |
103 | ], | |
feb4bdfd C |
104 | hooks: { |
105 | beforeCreate: beforeCreateOrUpdate, | |
106 | beforeUpdate: beforeCreateOrUpdate | |
107 | } | |
108 | } | |
109 | ) | |
110 | ||
e02643f3 C |
111 | const classMethods = [ |
112 | associate, | |
113 | ||
114 | countTotal, | |
115 | getByUsername, | |
e02643f3 C |
116 | listForApi, |
117 | loadById, | |
118 | loadByUsername, | |
72c7248b | 119 | loadByUsernameAndPopulateChannels, |
e02643f3 C |
120 | loadByUsernameOrEmail |
121 | ] | |
122 | const instanceMethods = [ | |
123 | isPasswordMatch, | |
0aef76c4 | 124 | toFormattedJSON, |
b0f9f39e C |
125 | isAdmin, |
126 | isAbleToUploadVideo | |
e02643f3 C |
127 | ] |
128 | addMethodsToModel(User, classMethods, instanceMethods) | |
129 | ||
feb4bdfd | 130 | return User |
9bd26629 | 131 | } |
69b0a27c | 132 | |
69818c93 | 133 | function beforeCreateOrUpdate (user: UserInstance) { |
6fcd19ba C |
134 | return cryptPassword(user.password).then(hash => { |
135 | user.password = hash | |
136 | return undefined | |
26d7d31b | 137 | }) |
feb4bdfd | 138 | } |
69b0a27c | 139 | |
26d7d31b C |
140 | // ------------------------------ METHODS ------------------------------ |
141 | ||
6fcd19ba C |
142 | isPasswordMatch = function (this: UserInstance, password: string) { |
143 | return comparePassword(password, this.password) | |
26d7d31b C |
144 | } |
145 | ||
0aef76c4 | 146 | toFormattedJSON = function (this: UserInstance) { |
72c7248b | 147 | const json = { |
feb4bdfd | 148 | id: this.id, |
26d7d31b | 149 | username: this.username, |
ad4a8a1c | 150 | email: this.email, |
1d49e1e2 | 151 | displayNSFW: this.displayNSFW, |
d74a0680 | 152 | role: this.role, |
b0f9f39e | 153 | videoQuota: this.videoQuota, |
72c7248b C |
154 | createdAt: this.createdAt, |
155 | author: { | |
156 | id: this.Author.id, | |
157 | uuid: this.Author.uuid | |
158 | } | |
26d7d31b | 159 | } |
72c7248b C |
160 | |
161 | if (Array.isArray(this.Author.VideoChannels) === true) { | |
162 | const videoChannels = this.Author.VideoChannels | |
163 | .map(c => c.toFormattedJSON()) | |
164 | .sort((v1, v2) => { | |
165 | if (v1.createdAt < v2.createdAt) return -1 | |
166 | if (v1.createdAt === v2.createdAt) return 0 | |
167 | ||
168 | return 1 | |
169 | }) | |
170 | ||
171 | json['videoChannels'] = videoChannels | |
172 | } | |
173 | ||
174 | return json | |
26d7d31b | 175 | } |
198b205c | 176 | |
70c065d6 | 177 | isAdmin = function (this: UserInstance) { |
65fcc311 | 178 | return this.role === USER_ROLES.ADMIN |
198b205c GS |
179 | } |
180 | ||
b0f9f39e C |
181 | isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) { |
182 | if (this.videoQuota === -1) return Promise.resolve(true) | |
183 | ||
184 | return getOriginalVideoFileTotalFromUser(this).then(totalBytes => { | |
185 | return (videoFile.size + totalBytes) < this.videoQuota | |
186 | }) | |
187 | } | |
188 | ||
26d7d31b | 189 | // ------------------------------ STATICS ------------------------------ |
69b0a27c | 190 | |
feb4bdfd | 191 | function associate (models) { |
e02643f3 | 192 | User.hasOne(models.Author, { |
4712081f C |
193 | foreignKey: 'userId', |
194 | onDelete: 'cascade' | |
195 | }) | |
196 | ||
e02643f3 | 197 | User.hasMany(models.OAuthToken, { |
feb4bdfd C |
198 | foreignKey: 'userId', |
199 | onDelete: 'cascade' | |
200 | }) | |
201 | } | |
202 | ||
6fcd19ba C |
203 | countTotal = function () { |
204 | return this.count() | |
089ff2f2 C |
205 | } |
206 | ||
69818c93 | 207 | getByUsername = function (username: string) { |
feb4bdfd C |
208 | const query = { |
209 | where: { | |
210 | username: username | |
72c7248b C |
211 | }, |
212 | include: [ { model: User['sequelize'].models.Author, required: true } ] | |
feb4bdfd C |
213 | } |
214 | ||
e02643f3 | 215 | return User.findOne(query) |
9bd26629 C |
216 | } |
217 | ||
6fcd19ba | 218 | listForApi = function (start: number, count: number, sort: string) { |
feb4bdfd C |
219 | const query = { |
220 | offset: start, | |
221 | limit: count, | |
72c7248b C |
222 | order: [ getSort(sort) ], |
223 | include: [ { model: User['sequelize'].models.Author, required: true } ] | |
feb4bdfd C |
224 | } |
225 | ||
6fcd19ba C |
226 | return User.findAndCountAll(query).then(({ rows, count }) => { |
227 | return { | |
228 | data: rows, | |
229 | total: count | |
230 | } | |
feb4bdfd | 231 | }) |
69b0a27c C |
232 | } |
233 | ||
6fcd19ba | 234 | loadById = function (id: number) { |
72c7248b C |
235 | const options = { |
236 | include: [ { model: User['sequelize'].models.Author, required: true } ] | |
237 | } | |
238 | ||
239 | return User.findById(id, options) | |
68a3b9f2 C |
240 | } |
241 | ||
6fcd19ba | 242 | loadByUsername = function (username: string) { |
feb4bdfd C |
243 | const query = { |
244 | where: { | |
556ddc31 | 245 | username |
72c7248b C |
246 | }, |
247 | include: [ { model: User['sequelize'].models.Author, required: true } ] | |
248 | } | |
249 | ||
250 | return User.findOne(query) | |
251 | } | |
252 | ||
253 | loadByUsernameAndPopulateChannels = function (username: string) { | |
254 | const query = { | |
255 | where: { | |
256 | username | |
257 | }, | |
258 | include: [ | |
259 | { | |
260 | model: User['sequelize'].models.Author, | |
261 | required: true, | |
262 | include: [ User['sequelize'].models.VideoChannel ] | |
263 | } | |
264 | ] | |
feb4bdfd C |
265 | } |
266 | ||
6fcd19ba | 267 | return User.findOne(query) |
9bd26629 | 268 | } |
ad4a8a1c | 269 | |
6fcd19ba | 270 | loadByUsernameOrEmail = function (username: string, email: string) { |
ad4a8a1c | 271 | const query = { |
72c7248b | 272 | include: [ { model: User['sequelize'].models.Author, required: true } ], |
ad4a8a1c C |
273 | where: { |
274 | $or: [ { username }, { email } ] | |
275 | } | |
276 | } | |
277 | ||
556ddc31 C |
278 | // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387 |
279 | return (User as any).findOne(query) | |
ad4a8a1c | 280 | } |
b0f9f39e C |
281 | |
282 | // --------------------------------------------------------------------------- | |
283 | ||
284 | function getOriginalVideoFileTotalFromUser (user: UserInstance) { | |
72c7248b | 285 | // Don't use sequelize because we need to use a sub query |
14d3270f C |
286 | const query = 'SELECT SUM("size") AS "total" FROM ' + |
287 | '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + | |
288 | 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + | |
72c7248b C |
289 | 'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' + |
290 | 'INNER JOIN "Authors" ON "VideoChannels"."authorId" = "Authors"."id" ' + | |
14d3270f C |
291 | 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' + |
292 | 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' | |
293 | ||
294 | const options = { | |
295 | bind: { userId: user.id }, | |
296 | type: Sequelize.QueryTypes.SELECT | |
b0f9f39e | 297 | } |
14d3270f C |
298 | return User['sequelize'].query(query, options).then(([ { total } ]) => { |
299 | if (total === null) return 0 | |
b0f9f39e | 300 | |
14d3270f C |
301 | return parseInt(total, 10) |
302 | }) | |
b0f9f39e | 303 | } |