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