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