diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:43:11 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:43:11 +0200 |
commit | 5c5e587307a27e173333789b5b5167d35f468b01 (patch) | |
tree | 94e3721caf2e11d38fd5f4112c0fc98da89ac535 /server/models/account/user.ts | |
parent | 1b42d73f44811eec1b7ddd72dd0d640a57c3376c (diff) | |
parent | b5fecbf44192144d1ca27c23a0b53922de288c10 (diff) | |
download | PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.tar.gz PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.tar.zst PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.zip |
Merge branch 'feature/strong-model-types' into develop
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r-- | server/models/account/user.ts | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 0041bf577..616dd603c 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -54,6 +54,14 @@ import { VideoImportModel } from '../video/video-import' | |||
54 | import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' | 54 | import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' |
55 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' | 55 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' |
56 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' | 56 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' |
57 | import * as Bluebird from 'bluebird' | ||
58 | import { | ||
59 | MUserDefault, | ||
60 | MUserFormattable, | ||
61 | MUserId, | ||
62 | MUserNotifSettingChannelDefault, | ||
63 | MUserWithNotificationSetting | ||
64 | } from '@server/typings/models' | ||
57 | 65 | ||
58 | enum ScopeNames { | 66 | enum ScopeNames { |
59 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' | 67 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' |
@@ -303,7 +311,7 @@ export class UserModel extends Model<UserModel> { | |||
303 | }) | 311 | }) |
304 | } | 312 | } |
305 | 313 | ||
306 | static listWithRight (right: UserRight) { | 314 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { |
307 | const roles = Object.keys(USER_ROLE_LABELS) | 315 | const roles = Object.keys(USER_ROLE_LABELS) |
308 | .map(k => parseInt(k, 10) as UserRole) | 316 | .map(k => parseInt(k, 10) as UserRole) |
309 | .filter(role => hasUserRight(role, right)) | 317 | .filter(role => hasUserRight(role, right)) |
@@ -319,7 +327,7 @@ export class UserModel extends Model<UserModel> { | |||
319 | return UserModel.findAll(query) | 327 | return UserModel.findAll(query) |
320 | } | 328 | } |
321 | 329 | ||
322 | static listUserSubscribersOf (actorId: number) { | 330 | static listUserSubscribersOf (actorId: number): Bluebird<MUserWithNotificationSetting[]> { |
323 | const query = { | 331 | const query = { |
324 | include: [ | 332 | include: [ |
325 | { | 333 | { |
@@ -358,7 +366,7 @@ export class UserModel extends Model<UserModel> { | |||
358 | return UserModel.unscoped().findAll(query) | 366 | return UserModel.unscoped().findAll(query) |
359 | } | 367 | } |
360 | 368 | ||
361 | static listByUsernames (usernames: string[]) { | 369 | static listByUsernames (usernames: string[]): Bluebird<MUserDefault[]> { |
362 | const query = { | 370 | const query = { |
363 | where: { | 371 | where: { |
364 | username: usernames | 372 | username: usernames |
@@ -368,11 +376,11 @@ export class UserModel extends Model<UserModel> { | |||
368 | return UserModel.findAll(query) | 376 | return UserModel.findAll(query) |
369 | } | 377 | } |
370 | 378 | ||
371 | static loadById (id: number) { | 379 | static loadById (id: number): Bluebird<MUserDefault> { |
372 | return UserModel.findByPk(id) | 380 | return UserModel.findByPk(id) |
373 | } | 381 | } |
374 | 382 | ||
375 | static loadByUsername (username: string) { | 383 | static loadByUsername (username: string): Bluebird<MUserDefault> { |
376 | const query = { | 384 | const query = { |
377 | where: { | 385 | where: { |
378 | username: { [ Op.iLike ]: username } | 386 | username: { [ Op.iLike ]: username } |
@@ -382,7 +390,7 @@ export class UserModel extends Model<UserModel> { | |||
382 | return UserModel.findOne(query) | 390 | return UserModel.findOne(query) |
383 | } | 391 | } |
384 | 392 | ||
385 | static loadByUsernameAndPopulateChannels (username: string) { | 393 | static loadByUsernameAndPopulateChannels (username: string): Bluebird<MUserNotifSettingChannelDefault> { |
386 | const query = { | 394 | const query = { |
387 | where: { | 395 | where: { |
388 | username: { [ Op.iLike ]: username } | 396 | username: { [ Op.iLike ]: username } |
@@ -392,7 +400,7 @@ export class UserModel extends Model<UserModel> { | |||
392 | return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) | 400 | return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) |
393 | } | 401 | } |
394 | 402 | ||
395 | static loadByEmail (email: string) { | 403 | static loadByEmail (email: string): Bluebird<MUserDefault> { |
396 | const query = { | 404 | const query = { |
397 | where: { | 405 | where: { |
398 | 406 | ||
@@ -402,7 +410,7 @@ export class UserModel extends Model<UserModel> { | |||
402 | return UserModel.findOne(query) | 410 | return UserModel.findOne(query) |
403 | } | 411 | } |
404 | 412 | ||
405 | static loadByUsernameOrEmail (username: string, email?: string) { | 413 | static loadByUsernameOrEmail (username: string, email?: string): Bluebird<MUserDefault> { |
406 | if (!email) email = username | 414 | if (!email) email = username |
407 | 415 | ||
408 | const query = { | 416 | const query = { |
@@ -414,7 +422,7 @@ export class UserModel extends Model<UserModel> { | |||
414 | return UserModel.findOne(query) | 422 | return UserModel.findOne(query) |
415 | } | 423 | } |
416 | 424 | ||
417 | static loadByVideoId (videoId: number) { | 425 | static loadByVideoId (videoId: number): Bluebird<MUserDefault> { |
418 | const query = { | 426 | const query = { |
419 | include: [ | 427 | include: [ |
420 | { | 428 | { |
@@ -445,7 +453,7 @@ export class UserModel extends Model<UserModel> { | |||
445 | return UserModel.findOne(query) | 453 | return UserModel.findOne(query) |
446 | } | 454 | } |
447 | 455 | ||
448 | static loadByVideoImportId (videoImportId: number) { | 456 | static loadByVideoImportId (videoImportId: number): Bluebird<MUserDefault> { |
449 | const query = { | 457 | const query = { |
450 | include: [ | 458 | include: [ |
451 | { | 459 | { |
@@ -462,7 +470,7 @@ export class UserModel extends Model<UserModel> { | |||
462 | return UserModel.findOne(query) | 470 | return UserModel.findOne(query) |
463 | } | 471 | } |
464 | 472 | ||
465 | static loadByChannelActorId (videoChannelActorId: number) { | 473 | static loadByChannelActorId (videoChannelActorId: number): Bluebird<MUserDefault> { |
466 | const query = { | 474 | const query = { |
467 | include: [ | 475 | include: [ |
468 | { | 476 | { |
@@ -486,7 +494,7 @@ export class UserModel extends Model<UserModel> { | |||
486 | return UserModel.findOne(query) | 494 | return UserModel.findOne(query) |
487 | } | 495 | } |
488 | 496 | ||
489 | static loadByAccountActorId (accountActorId: number) { | 497 | static loadByAccountActorId (accountActorId: number): Bluebird<MUserDefault> { |
490 | const query = { | 498 | const query = { |
491 | include: [ | 499 | include: [ |
492 | { | 500 | { |
@@ -503,7 +511,7 @@ export class UserModel extends Model<UserModel> { | |||
503 | return UserModel.findOne(query) | 511 | return UserModel.findOne(query) |
504 | } | 512 | } |
505 | 513 | ||
506 | static getOriginalVideoFileTotalFromUser (user: UserModel) { | 514 | static getOriginalVideoFileTotalFromUser (user: MUserId) { |
507 | // Don't use sequelize because we need to use a sub query | 515 | // Don't use sequelize because we need to use a sub query |
508 | const query = UserModel.generateUserQuotaBaseSQL() | 516 | const query = UserModel.generateUserQuotaBaseSQL() |
509 | 517 | ||
@@ -511,7 +519,7 @@ export class UserModel extends Model<UserModel> { | |||
511 | } | 519 | } |
512 | 520 | ||
513 | // Returns cumulative size of all video files uploaded in the last 24 hours. | 521 | // Returns cumulative size of all video files uploaded in the last 24 hours. |
514 | static getOriginalVideoFileTotalDailyFromUser (user: UserModel) { | 522 | static getOriginalVideoFileTotalDailyFromUser (user: MUserId) { |
515 | // Don't use sequelize because we need to use a sub query | 523 | // Don't use sequelize because we need to use a sub query |
516 | const query = UserModel.generateUserQuotaBaseSQL('"video"."createdAt" > now() - interval \'24 hours\'') | 524 | const query = UserModel.generateUserQuotaBaseSQL('"video"."createdAt" > now() - interval \'24 hours\'') |
517 | 525 | ||
@@ -552,7 +560,9 @@ export class UserModel extends Model<UserModel> { | |||
552 | return comparePassword(password, this.password) | 560 | return comparePassword(password, this.password) |
553 | } | 561 | } |
554 | 562 | ||
555 | toFormattedJSON (parameters: { withAdminFlags?: boolean } = {}): User { | 563 | toSummaryJSON |
564 | |||
565 | toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { | ||
556 | const videoQuotaUsed = this.get('videoQuotaUsed') | 566 | const videoQuotaUsed = this.get('videoQuotaUsed') |
557 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') | 567 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') |
558 | 568 | ||