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