]>
Commit | Line | Data |
---|---|---|
3acc5084 | 1 | import { FindOptions, literal, Op, QueryTypes } from 'sequelize' |
3fd3ab2d | 2 | import { |
d175a6f7 | 3 | AfterDestroy, |
f201a749 | 4 | AfterUpdate, |
a73c582e C |
5 | AllowNull, |
6 | BeforeCreate, | |
7 | BeforeUpdate, | |
8 | Column, | |
9 | CreatedAt, | |
10 | DataType, | |
11 | Default, | |
12 | DefaultScope, | |
13 | HasMany, | |
14 | HasOne, | |
15 | Is, | |
16 | IsEmail, | |
17 | Model, | |
18 | Scopes, | |
19 | Table, | |
20 | UpdatedAt | |
3fd3ab2d | 21 | } from 'sequelize-typescript' |
e34c85e5 | 22 | import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' |
ba75d268 | 23 | import { User, UserRole } from '../../../shared/models/users' |
65fcc311 | 24 | import { |
1eddc9a7 | 25 | isUserAdminFlagsValid, |
a73c582e | 26 | isUserAutoPlayVideoValid, |
eacb25c4 | 27 | isUserBlockedReasonValid, |
e6921918 | 28 | isUserBlockedValid, |
d9eaee39 | 29 | isUserEmailVerifiedValid, |
5cf84858 | 30 | isUserNSFWPolicyValid, |
a73c582e C |
31 | isUserPasswordValid, |
32 | isUserRoleValid, | |
33 | isUserUsernameValid, | |
3caf77d3 | 34 | isUserVideoLanguages, |
5cf84858 | 35 | isUserVideoQuotaDailyValid, |
64cc5e85 | 36 | isUserVideoQuotaValid, |
cef534ed C |
37 | isUserVideosHistoryEnabledValid, |
38 | isUserWebTorrentEnabledValid | |
3fd3ab2d | 39 | } from '../../helpers/custom-validators/users' |
da854ddd | 40 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' |
3fd3ab2d C |
41 | import { OAuthTokenModel } from '../oauth/oauth-token' |
42 | import { getSort, throwIfNotValid } from '../utils' | |
43 | import { VideoChannelModel } from '../video/video-channel' | |
44 | import { AccountModel } from './account' | |
0883b324 C |
45 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' |
46 | import { values } from 'lodash' | |
ffb321be | 47 | import { DEFAULT_THEME_NAME, DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' |
f201a749 | 48 | import { clearCacheByUserId } from '../../lib/oauth-model' |
cef534ed C |
49 | import { UserNotificationSettingModel } from './user-notification-setting' |
50 | import { VideoModel } from '../video/video' | |
51 | import { ActorModel } from '../activitypub/actor' | |
52 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
dc133480 | 53 | import { VideoImportModel } from '../video/video-import' |
1eddc9a7 | 54 | import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' |
503c6f44 | 55 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' |
7cd4d2ba | 56 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' |
453e83ea C |
57 | import * as Bluebird from 'bluebird' |
58 | import { MUserChannel, MUserDefault, MUserId, MUserWithNotificationSetting } from '@server/typings/models' | |
3fd3ab2d | 59 | |
9c2e0dbf C |
60 | enum ScopeNames { |
61 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' | |
62 | } | |
63 | ||
3acc5084 | 64 | @DefaultScope(() => ({ |
d48ff09d C |
65 | include: [ |
66 | { | |
3acc5084 | 67 | model: AccountModel, |
d48ff09d | 68 | required: true |
cef534ed C |
69 | }, |
70 | { | |
3acc5084 | 71 | model: UserNotificationSettingModel, |
cef534ed | 72 | required: true |
d48ff09d C |
73 | } |
74 | ] | |
3acc5084 C |
75 | })) |
76 | @Scopes(() => ({ | |
9c2e0dbf | 77 | [ScopeNames.WITH_VIDEO_CHANNEL]: { |
d48ff09d C |
78 | include: [ |
79 | { | |
3acc5084 | 80 | model: AccountModel, |
d48ff09d | 81 | required: true, |
3acc5084 | 82 | include: [ VideoChannelModel ] |
cef534ed C |
83 | }, |
84 | { | |
3acc5084 | 85 | model: UserNotificationSettingModel, |
cef534ed | 86 | required: true |
d48ff09d | 87 | } |
3acc5084 | 88 | ] |
d48ff09d | 89 | } |
3acc5084 | 90 | })) |
3fd3ab2d C |
91 | @Table({ |
92 | tableName: 'user', | |
93 | indexes: [ | |
feb4bdfd | 94 | { |
3fd3ab2d C |
95 | fields: [ 'username' ], |
96 | unique: true | |
feb4bdfd C |
97 | }, |
98 | { | |
3fd3ab2d C |
99 | fields: [ 'email' ], |
100 | unique: true | |
feb4bdfd | 101 | } |
e02643f3 | 102 | ] |
3fd3ab2d C |
103 | }) |
104 | export class UserModel extends Model<UserModel> { | |
105 | ||
106 | @AllowNull(false) | |
107 | @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password')) | |
108 | @Column | |
109 | password: string | |
110 | ||
111 | @AllowNull(false) | |
112 | @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) | |
113 | @Column | |
114 | username: string | |
115 | ||
116 | @AllowNull(false) | |
117 | @IsEmail | |
118 | @Column(DataType.STRING(400)) | |
119 | email: string | |
120 | ||
d1ab89de C |
121 | @AllowNull(true) |
122 | @IsEmail | |
123 | @Column(DataType.STRING(400)) | |
124 | pendingEmail: string | |
125 | ||
d9eaee39 JM |
126 | @AllowNull(true) |
127 | @Default(null) | |
1735c825 | 128 | @Is('UserEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean', true)) |
d9eaee39 JM |
129 | @Column |
130 | emailVerified: boolean | |
131 | ||
3fd3ab2d | 132 | @AllowNull(false) |
0883b324 | 133 | @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) |
1735c825 | 134 | @Column(DataType.ENUM(...values(NSFW_POLICY_TYPES))) |
0883b324 | 135 | nsfwPolicy: NSFWPolicyType |
3fd3ab2d | 136 | |
64cc5e85 | 137 | @AllowNull(false) |
0229b014 | 138 | @Default(true) |
ed638e53 RK |
139 | @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) |
140 | @Column | |
141 | webTorrentEnabled: boolean | |
64cc5e85 | 142 | |
8b9a525a C |
143 | @AllowNull(false) |
144 | @Default(true) | |
145 | @Is('UserVideosHistoryEnabled', value => throwIfNotValid(value, isUserVideosHistoryEnabledValid, 'Videos history enabled')) | |
146 | @Column | |
147 | videosHistoryEnabled: boolean | |
148 | ||
7efe153b AL |
149 | @AllowNull(false) |
150 | @Default(true) | |
151 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | |
152 | @Column | |
153 | autoPlayVideo: boolean | |
154 | ||
3caf77d3 C |
155 | @AllowNull(true) |
156 | @Default(null) | |
157 | @Is('UserVideoLanguages', value => throwIfNotValid(value, isUserVideoLanguages, 'video languages')) | |
158 | @Column(DataType.ARRAY(DataType.STRING)) | |
159 | videoLanguages: string[] | |
160 | ||
1eddc9a7 C |
161 | @AllowNull(false) |
162 | @Default(UserAdminFlag.NONE) | |
163 | @Is('UserAdminFlags', value => throwIfNotValid(value, isUserAdminFlagsValid, 'user admin flags')) | |
164 | @Column | |
165 | adminFlags?: UserAdminFlag | |
166 | ||
e6921918 C |
167 | @AllowNull(false) |
168 | @Default(false) | |
169 | @Is('UserBlocked', value => throwIfNotValid(value, isUserBlockedValid, 'blocked boolean')) | |
170 | @Column | |
171 | blocked: boolean | |
172 | ||
eacb25c4 C |
173 | @AllowNull(true) |
174 | @Default(null) | |
1735c825 | 175 | @Is('UserBlockedReason', value => throwIfNotValid(value, isUserBlockedReasonValid, 'blocked reason', true)) |
eacb25c4 C |
176 | @Column |
177 | blockedReason: string | |
178 | ||
3fd3ab2d C |
179 | @AllowNull(false) |
180 | @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role')) | |
181 | @Column | |
182 | role: number | |
183 | ||
184 | @AllowNull(false) | |
185 | @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota')) | |
186 | @Column(DataType.BIGINT) | |
187 | videoQuota: number | |
188 | ||
bee0abff FA |
189 | @AllowNull(false) |
190 | @Is('UserVideoQuotaDaily', value => throwIfNotValid(value, isUserVideoQuotaDailyValid, 'video quota daily')) | |
191 | @Column(DataType.BIGINT) | |
192 | videoQuotaDaily: number | |
193 | ||
7cd4d2ba | 194 | @AllowNull(false) |
ffb321be | 195 | @Default(DEFAULT_THEME_NAME) |
503c6f44 | 196 | @Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme')) |
7cd4d2ba C |
197 | @Column |
198 | theme: string | |
199 | ||
3fd3ab2d C |
200 | @CreatedAt |
201 | createdAt: Date | |
202 | ||
203 | @UpdatedAt | |
204 | updatedAt: Date | |
205 | ||
206 | @HasOne(() => AccountModel, { | |
207 | foreignKey: 'userId', | |
f05a1c30 C |
208 | onDelete: 'cascade', |
209 | hooks: true | |
3fd3ab2d C |
210 | }) |
211 | Account: AccountModel | |
69b0a27c | 212 | |
cef534ed C |
213 | @HasOne(() => UserNotificationSettingModel, { |
214 | foreignKey: 'userId', | |
215 | onDelete: 'cascade', | |
216 | hooks: true | |
217 | }) | |
218 | NotificationSetting: UserNotificationSettingModel | |
219 | ||
dc133480 C |
220 | @HasMany(() => VideoImportModel, { |
221 | foreignKey: 'userId', | |
222 | onDelete: 'cascade' | |
223 | }) | |
224 | VideoImports: VideoImportModel[] | |
225 | ||
3fd3ab2d C |
226 | @HasMany(() => OAuthTokenModel, { |
227 | foreignKey: 'userId', | |
228 | onDelete: 'cascade' | |
229 | }) | |
230 | OAuthTokens: OAuthTokenModel[] | |
231 | ||
232 | @BeforeCreate | |
233 | @BeforeUpdate | |
234 | static cryptPasswordIfNeeded (instance: UserModel) { | |
235 | if (instance.changed('password')) { | |
236 | return cryptPassword(instance.password) | |
237 | .then(hash => { | |
238 | instance.password = hash | |
239 | return undefined | |
240 | }) | |
241 | } | |
59557c46 | 242 | } |
26d7d31b | 243 | |
f201a749 | 244 | @AfterUpdate |
d175a6f7 | 245 | @AfterDestroy |
f201a749 C |
246 | static removeTokenCache (instance: UserModel) { |
247 | return clearCacheByUserId(instance.id) | |
248 | } | |
249 | ||
3fd3ab2d C |
250 | static countTotal () { |
251 | return this.count() | |
252 | } | |
954605a8 | 253 | |
24b9417c C |
254 | static listForApi (start: number, count: number, sort: string, search?: string) { |
255 | let where = undefined | |
256 | if (search) { | |
257 | where = { | |
3acc5084 | 258 | [Op.or]: [ |
24b9417c C |
259 | { |
260 | email: { | |
3acc5084 | 261 | [Op.iLike]: '%' + search + '%' |
24b9417c C |
262 | } |
263 | }, | |
264 | { | |
265 | username: { | |
3acc5084 | 266 | [ Op.iLike ]: '%' + search + '%' |
24b9417c C |
267 | } |
268 | } | |
269 | ] | |
270 | } | |
271 | } | |
272 | ||
3acc5084 | 273 | const query: FindOptions = { |
a76138ff C |
274 | attributes: { |
275 | include: [ | |
276 | [ | |
3acc5084 | 277 | literal( |
a76138ff | 278 | '(' + |
8b604880 C |
279 | 'SELECT COALESCE(SUM("size"), 0) ' + |
280 | 'FROM (' + | |
a76138ff C |
281 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + |
282 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
283 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
284 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
285 | 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + | |
286 | ') t' + | |
287 | ')' | |
288 | ), | |
289 | 'videoQuotaUsed' | |
3acc5084 | 290 | ] |
a76138ff C |
291 | ] |
292 | }, | |
3fd3ab2d C |
293 | offset: start, |
294 | limit: count, | |
24b9417c C |
295 | order: getSort(sort), |
296 | where | |
3fd3ab2d | 297 | } |
72c7248b | 298 | |
3fd3ab2d C |
299 | return UserModel.findAndCountAll(query) |
300 | .then(({ rows, count }) => { | |
301 | return { | |
302 | data: rows, | |
303 | total: count | |
304 | } | |
72c7248b | 305 | }) |
72c7248b C |
306 | } |
307 | ||
453e83ea | 308 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { |
ba75d268 C |
309 | const roles = Object.keys(USER_ROLE_LABELS) |
310 | .map(k => parseInt(k, 10) as UserRole) | |
311 | .filter(role => hasUserRight(role, right)) | |
312 | ||
ba75d268 | 313 | const query = { |
ba75d268 C |
314 | where: { |
315 | role: { | |
3acc5084 | 316 | [Op.in]: roles |
ba75d268 C |
317 | } |
318 | } | |
319 | } | |
320 | ||
cef534ed C |
321 | return UserModel.findAll(query) |
322 | } | |
323 | ||
453e83ea | 324 | static listUserSubscribersOf (actorId: number): Bluebird<MUserWithNotificationSetting[]> { |
cef534ed C |
325 | const query = { |
326 | include: [ | |
327 | { | |
328 | model: UserNotificationSettingModel.unscoped(), | |
329 | required: true | |
330 | }, | |
331 | { | |
332 | attributes: [ 'userId' ], | |
333 | model: AccountModel.unscoped(), | |
334 | required: true, | |
335 | include: [ | |
336 | { | |
337 | attributes: [ ], | |
338 | model: ActorModel.unscoped(), | |
339 | required: true, | |
340 | where: { | |
341 | serverId: null | |
342 | }, | |
343 | include: [ | |
344 | { | |
345 | attributes: [ ], | |
346 | as: 'ActorFollowings', | |
347 | model: ActorFollowModel.unscoped(), | |
348 | required: true, | |
349 | where: { | |
350 | targetActorId: actorId | |
351 | } | |
352 | } | |
353 | ] | |
354 | } | |
355 | ] | |
356 | } | |
357 | ] | |
358 | } | |
359 | ||
360 | return UserModel.unscoped().findAll(query) | |
ba75d268 C |
361 | } |
362 | ||
453e83ea | 363 | static listByUsernames (usernames: string[]): Bluebird<MUserDefault[]> { |
f7cc67b4 C |
364 | const query = { |
365 | where: { | |
366 | username: usernames | |
367 | } | |
368 | } | |
369 | ||
370 | return UserModel.findAll(query) | |
371 | } | |
372 | ||
453e83ea | 373 | static loadById (id: number): Bluebird<MUserDefault> { |
9b39106d | 374 | return UserModel.findByPk(id) |
3fd3ab2d | 375 | } |
feb4bdfd | 376 | |
453e83ea | 377 | static loadByUsername (username: string): Bluebird<MUserDefault> { |
3fd3ab2d C |
378 | const query = { |
379 | where: { | |
50b4dcce | 380 | username: { [ Op.iLike ]: username } |
d48ff09d | 381 | } |
3fd3ab2d | 382 | } |
089ff2f2 | 383 | |
3fd3ab2d | 384 | return UserModel.findOne(query) |
feb4bdfd C |
385 | } |
386 | ||
453e83ea | 387 | static loadByUsernameAndPopulateChannels (username: string): Bluebird<MUserChannel> { |
3fd3ab2d C |
388 | const query = { |
389 | where: { | |
50b4dcce | 390 | username: { [ Op.iLike ]: username } |
d48ff09d | 391 | } |
3fd3ab2d | 392 | } |
9bd26629 | 393 | |
9c2e0dbf | 394 | return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) |
feb4bdfd C |
395 | } |
396 | ||
453e83ea | 397 | static loadByEmail (email: string): Bluebird<MUserDefault> { |
ecb4e35f C |
398 | const query = { |
399 | where: { | |
400 | ||
401 | } | |
402 | } | |
403 | ||
404 | return UserModel.findOne(query) | |
405 | } | |
406 | ||
453e83ea | 407 | static loadByUsernameOrEmail (username: string, email?: string): Bluebird<MUserDefault> { |
ba12e8b3 C |
408 | if (!email) email = username |
409 | ||
3fd3ab2d | 410 | const query = { |
3fd3ab2d | 411 | where: { |
50b4dcce | 412 | [ Op.or ]: [ { username: { [ Op.iLike ]: username } }, { email } ] |
3fd3ab2d | 413 | } |
6fcd19ba | 414 | } |
69b0a27c | 415 | |
d48ff09d | 416 | return UserModel.findOne(query) |
72c7248b C |
417 | } |
418 | ||
453e83ea | 419 | static loadByVideoId (videoId: number): Bluebird<MUserDefault> { |
cef534ed C |
420 | const query = { |
421 | include: [ | |
422 | { | |
423 | required: true, | |
424 | attributes: [ 'id' ], | |
425 | model: AccountModel.unscoped(), | |
426 | include: [ | |
427 | { | |
428 | required: true, | |
429 | attributes: [ 'id' ], | |
430 | model: VideoChannelModel.unscoped(), | |
431 | include: [ | |
432 | { | |
433 | required: true, | |
434 | attributes: [ 'id' ], | |
435 | model: VideoModel.unscoped(), | |
436 | where: { | |
437 | id: videoId | |
438 | } | |
439 | } | |
440 | ] | |
441 | } | |
442 | ] | |
443 | } | |
444 | ] | |
445 | } | |
446 | ||
447 | return UserModel.findOne(query) | |
448 | } | |
449 | ||
453e83ea | 450 | static loadByVideoImportId (videoImportId: number): Bluebird<MUserDefault> { |
dc133480 C |
451 | const query = { |
452 | include: [ | |
453 | { | |
454 | required: true, | |
455 | attributes: [ 'id' ], | |
456 | model: VideoImportModel.unscoped(), | |
457 | where: { | |
458 | id: videoImportId | |
459 | } | |
460 | } | |
461 | ] | |
462 | } | |
463 | ||
464 | return UserModel.findOne(query) | |
465 | } | |
466 | ||
453e83ea | 467 | static loadByChannelActorId (videoChannelActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
468 | const query = { |
469 | include: [ | |
470 | { | |
471 | required: true, | |
472 | attributes: [ 'id' ], | |
473 | model: AccountModel.unscoped(), | |
474 | include: [ | |
475 | { | |
476 | required: true, | |
477 | attributes: [ 'id' ], | |
478 | model: VideoChannelModel.unscoped(), | |
479 | where: { | |
480 | actorId: videoChannelActorId | |
481 | } | |
482 | } | |
483 | ] | |
484 | } | |
485 | ] | |
486 | } | |
487 | ||
488 | return UserModel.findOne(query) | |
489 | } | |
490 | ||
453e83ea | 491 | static loadByAccountActorId (accountActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
492 | const query = { |
493 | include: [ | |
494 | { | |
495 | required: true, | |
496 | attributes: [ 'id' ], | |
497 | model: AccountModel.unscoped(), | |
498 | where: { | |
499 | actorId: accountActorId | |
500 | } | |
501 | } | |
502 | ] | |
503 | } | |
504 | ||
505 | return UserModel.findOne(query) | |
506 | } | |
507 | ||
453e83ea | 508 | static getOriginalVideoFileTotalFromUser (user: MUserId) { |
3fd3ab2d | 509 | // Don't use sequelize because we need to use a sub query |
8b604880 | 510 | const query = UserModel.generateUserQuotaBaseSQL() |
bee0abff | 511 | |
8b604880 | 512 | return UserModel.getTotalRawQuery(query, user.id) |
bee0abff FA |
513 | } |
514 | ||
8b604880 | 515 | // Returns cumulative size of all video files uploaded in the last 24 hours. |
453e83ea | 516 | static getOriginalVideoFileTotalDailyFromUser (user: MUserId) { |
bee0abff | 517 | // Don't use sequelize because we need to use a sub query |
8b604880 | 518 | const query = UserModel.generateUserQuotaBaseSQL('"video"."createdAt" > now() - interval \'24 hours\'') |
68a3b9f2 | 519 | |
8b604880 | 520 | return UserModel.getTotalRawQuery(query, user.id) |
72c7248b C |
521 | } |
522 | ||
09cababd C |
523 | static async getStats () { |
524 | const totalUsers = await UserModel.count() | |
525 | ||
526 | return { | |
527 | totalUsers | |
528 | } | |
529 | } | |
530 | ||
5cf84858 C |
531 | static autoComplete (search: string) { |
532 | const query = { | |
533 | where: { | |
534 | username: { | |
3acc5084 | 535 | [ Op.like ]: `%${search}%` |
5cf84858 C |
536 | } |
537 | }, | |
538 | limit: 10 | |
539 | } | |
540 | ||
541 | return UserModel.findAll(query) | |
542 | .then(u => u.map(u => u.username)) | |
543 | } | |
544 | ||
3fd3ab2d C |
545 | hasRight (right: UserRight) { |
546 | return hasUserRight(this.role, right) | |
547 | } | |
72c7248b | 548 | |
1eddc9a7 C |
549 | hasAdminFlag (flag: UserAdminFlag) { |
550 | return this.adminFlags & flag | |
551 | } | |
552 | ||
3fd3ab2d C |
553 | isPasswordMatch (password: string) { |
554 | return comparePassword(password, this.password) | |
feb4bdfd C |
555 | } |
556 | ||
1eddc9a7 | 557 | toFormattedJSON (parameters: { withAdminFlags?: boolean } = {}): User { |
a76138ff | 558 | const videoQuotaUsed = this.get('videoQuotaUsed') |
bee0abff | 559 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') |
a76138ff | 560 | |
3fd3ab2d C |
561 | const json = { |
562 | id: this.id, | |
563 | username: this.username, | |
564 | email: this.email, | |
d1ab89de | 565 | pendingEmail: this.pendingEmail, |
d9eaee39 | 566 | emailVerified: this.emailVerified, |
0883b324 | 567 | nsfwPolicy: this.nsfwPolicy, |
ed638e53 | 568 | webTorrentEnabled: this.webTorrentEnabled, |
276d9652 | 569 | videosHistoryEnabled: this.videosHistoryEnabled, |
7efe153b | 570 | autoPlayVideo: this.autoPlayVideo, |
3caf77d3 | 571 | videoLanguages: this.videoLanguages, |
3fd3ab2d | 572 | role: this.role, |
ffb321be | 573 | theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), |
3fd3ab2d C |
574 | roleLabel: USER_ROLE_LABELS[ this.role ], |
575 | videoQuota: this.videoQuota, | |
bee0abff | 576 | videoQuotaDaily: this.videoQuotaDaily, |
3fd3ab2d | 577 | createdAt: this.createdAt, |
eacb25c4 C |
578 | blocked: this.blocked, |
579 | blockedReason: this.blockedReason, | |
c5911fd3 | 580 | account: this.Account.toFormattedJSON(), |
cef534ed | 581 | notificationSettings: this.NotificationSetting ? this.NotificationSetting.toFormattedJSON() : undefined, |
a76138ff | 582 | videoChannels: [], |
bee0abff | 583 | videoQuotaUsed: videoQuotaUsed !== undefined |
1735c825 | 584 | ? parseInt(videoQuotaUsed + '', 10) |
bee0abff FA |
585 | : undefined, |
586 | videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined | |
1735c825 | 587 | ? parseInt(videoQuotaUsedDaily + '', 10) |
bee0abff | 588 | : undefined |
3fd3ab2d C |
589 | } |
590 | ||
1eddc9a7 C |
591 | if (parameters.withAdminFlags) { |
592 | Object.assign(json, { adminFlags: this.adminFlags }) | |
593 | } | |
594 | ||
3fd3ab2d | 595 | if (Array.isArray(this.Account.VideoChannels) === true) { |
c5911fd3 | 596 | json.videoChannels = this.Account.VideoChannels |
3fd3ab2d C |
597 | .map(c => c.toFormattedJSON()) |
598 | .sort((v1, v2) => { | |
599 | if (v1.createdAt < v2.createdAt) return -1 | |
600 | if (v1.createdAt === v2.createdAt) return 0 | |
ad4a8a1c | 601 | |
3fd3ab2d C |
602 | return 1 |
603 | }) | |
ad4a8a1c | 604 | } |
3fd3ab2d C |
605 | |
606 | return json | |
ad4a8a1c C |
607 | } |
608 | ||
bee0abff FA |
609 | async isAbleToUploadVideo (videoFile: { size: number }) { |
610 | if (this.videoQuota === -1 && this.videoQuotaDaily === -1) return Promise.resolve(true) | |
b0f9f39e | 611 | |
bee0abff FA |
612 | const [ totalBytes, totalBytesDaily ] = await Promise.all([ |
613 | UserModel.getOriginalVideoFileTotalFromUser(this), | |
614 | UserModel.getOriginalVideoFileTotalDailyFromUser(this) | |
615 | ]) | |
616 | ||
617 | const uploadedTotal = videoFile.size + totalBytes | |
618 | const uploadedDaily = videoFile.size + totalBytesDaily | |
bee0abff | 619 | |
3acc5084 C |
620 | if (this.videoQuotaDaily === -1) return uploadedTotal < this.videoQuota |
621 | if (this.videoQuota === -1) return uploadedDaily < this.videoQuotaDaily | |
622 | ||
623 | return uploadedTotal < this.videoQuota && uploadedDaily < this.videoQuotaDaily | |
b0f9f39e | 624 | } |
8b604880 C |
625 | |
626 | private static generateUserQuotaBaseSQL (where?: string) { | |
627 | const andWhere = where ? 'AND ' + where : '' | |
628 | ||
629 | return 'SELECT SUM("size") AS "total" ' + | |
630 | 'FROM (' + | |
631 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | |
632 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
633 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
634 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
635 | 'WHERE "account"."userId" = $userId ' + andWhere + | |
636 | 'GROUP BY "video"."id"' + | |
637 | ') t' | |
638 | } | |
639 | ||
640 | private static getTotalRawQuery (query: string, userId: number) { | |
641 | const options = { | |
642 | bind: { userId }, | |
3acc5084 | 643 | type: QueryTypes.SELECT as QueryTypes.SELECT |
8b604880 C |
644 | } |
645 | ||
3acc5084 | 646 | return UserModel.sequelize.query<{ total: string }>(query, options) |
8b604880 C |
647 | .then(([ { total } ]) => { |
648 | if (total === null) return 0 | |
649 | ||
3acc5084 | 650 | return parseInt(total, 10) |
8b604880 C |
651 | }) |
652 | } | |
b0f9f39e | 653 | } |