diff options
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r-- | server/models/account/user.ts | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 4c2c5e278..fb4c15aef 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { FindOptions, literal, Op, QueryTypes, where, fn, col } from 'sequelize' | 1 | import { FindOptions, literal, Op, QueryTypes, where, fn, col, WhereOptions } from 'sequelize' |
2 | import { | 2 | import { |
3 | AfterDestroy, | 3 | AfterDestroy, |
4 | AfterUpdate, | 4 | AfterUpdate, |
@@ -101,7 +101,7 @@ enum ScopeNames { | |||
101 | required: true, | 101 | required: true, |
102 | where: { | 102 | where: { |
103 | type: { | 103 | type: { |
104 | [ Op.ne ]: VideoPlaylistType.REGULAR | 104 | [Op.ne]: VideoPlaylistType.REGULAR |
105 | } | 105 | } |
106 | } | 106 | } |
107 | } | 107 | } |
@@ -186,7 +186,10 @@ export class UserModel extends Model<UserModel> { | |||
186 | 186 | ||
187 | @AllowNull(false) | 187 | @AllowNull(false) |
188 | @Default(true) | 188 | @Default(true) |
189 | @Is('UserAutoPlayNextVideoPlaylist', value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean')) | 189 | @Is( |
190 | 'UserAutoPlayNextVideoPlaylist', | ||
191 | value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean') | ||
192 | ) | ||
190 | @Column | 193 | @Column |
191 | autoPlayNextVideoPlaylist: boolean | 194 | autoPlayNextVideoPlaylist: boolean |
192 | 195 | ||
@@ -308,7 +311,8 @@ export class UserModel extends Model<UserModel> { | |||
308 | } | 311 | } |
309 | 312 | ||
310 | static listForApi (start: number, count: number, sort: string, search?: string) { | 313 | static listForApi (start: number, count: number, sort: string, search?: string) { |
311 | let where = undefined | 314 | let where: WhereOptions |
315 | |||
312 | if (search) { | 316 | if (search) { |
313 | where = { | 317 | where = { |
314 | [Op.or]: [ | 318 | [Op.or]: [ |
@@ -319,7 +323,7 @@ export class UserModel extends Model<UserModel> { | |||
319 | }, | 323 | }, |
320 | { | 324 | { |
321 | username: { | 325 | username: { |
322 | [ Op.iLike ]: '%' + search + '%' | 326 | [Op.iLike]: '%' + search + '%' |
323 | } | 327 | } |
324 | } | 328 | } |
325 | ] | 329 | ] |
@@ -332,14 +336,14 @@ export class UserModel extends Model<UserModel> { | |||
332 | [ | 336 | [ |
333 | literal( | 337 | literal( |
334 | '(' + | 338 | '(' + |
335 | 'SELECT COALESCE(SUM("size"), 0) ' + | 339 | 'SELECT COALESCE(SUM("size"), 0) ' + |
336 | 'FROM (' + | 340 | 'FROM (' + |
337 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | 341 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + |
338 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | 342 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + |
339 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | 343 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + |
340 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | 344 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + |
341 | 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + | 345 | 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + |
342 | ') t' + | 346 | ') t' + |
343 | ')' | 347 | ')' |
344 | ), | 348 | ), |
345 | 'videoQuotaUsed' | 349 | 'videoQuotaUsed' |
@@ -353,18 +357,18 @@ export class UserModel extends Model<UserModel> { | |||
353 | } | 357 | } |
354 | 358 | ||
355 | return UserModel.findAndCountAll(query) | 359 | return UserModel.findAndCountAll(query) |
356 | .then(({ rows, count }) => { | 360 | .then(({ rows, count }) => { |
357 | return { | 361 | return { |
358 | data: rows, | 362 | data: rows, |
359 | total: count | 363 | total: count |
360 | } | 364 | } |
361 | }) | 365 | }) |
362 | } | 366 | } |
363 | 367 | ||
364 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { | 368 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { |
365 | const roles = Object.keys(USER_ROLE_LABELS) | 369 | const roles = Object.keys(USER_ROLE_LABELS) |
366 | .map(k => parseInt(k, 10) as UserRole) | 370 | .map(k => parseInt(k, 10) as UserRole) |
367 | .filter(role => hasUserRight(role, right)) | 371 | .filter(role => hasUserRight(role, right)) |
368 | 372 | ||
369 | const query = { | 373 | const query = { |
370 | where: { | 374 | where: { |
@@ -390,7 +394,7 @@ export class UserModel extends Model<UserModel> { | |||
390 | required: true, | 394 | required: true, |
391 | include: [ | 395 | include: [ |
392 | { | 396 | { |
393 | attributes: [ ], | 397 | attributes: [], |
394 | model: ActorModel.unscoped(), | 398 | model: ActorModel.unscoped(), |
395 | required: true, | 399 | required: true, |
396 | where: { | 400 | where: { |
@@ -398,7 +402,7 @@ export class UserModel extends Model<UserModel> { | |||
398 | }, | 402 | }, |
399 | include: [ | 403 | include: [ |
400 | { | 404 | { |
401 | attributes: [ ], | 405 | attributes: [], |
402 | as: 'ActorFollowings', | 406 | as: 'ActorFollowings', |
403 | model: ActorFollowModel.unscoped(), | 407 | model: ActorFollowModel.unscoped(), |
404 | required: true, | 408 | required: true, |
@@ -433,7 +437,7 @@ export class UserModel extends Model<UserModel> { | |||
433 | static loadByUsername (username: string): Bluebird<MUserDefault> { | 437 | static loadByUsername (username: string): Bluebird<MUserDefault> { |
434 | const query = { | 438 | const query = { |
435 | where: { | 439 | where: { |
436 | username: { [ Op.iLike ]: username } | 440 | username: { [Op.iLike]: username } |
437 | } | 441 | } |
438 | } | 442 | } |
439 | 443 | ||
@@ -443,7 +447,7 @@ export class UserModel extends Model<UserModel> { | |||
443 | static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> { | 447 | static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> { |
444 | const query = { | 448 | const query = { |
445 | where: { | 449 | where: { |
446 | username: { [ Op.iLike ]: username } | 450 | username: { [Op.iLike]: username } |
447 | } | 451 | } |
448 | } | 452 | } |
449 | 453 | ||
@@ -465,7 +469,7 @@ export class UserModel extends Model<UserModel> { | |||
465 | 469 | ||
466 | const query = { | 470 | const query = { |
467 | where: { | 471 | where: { |
468 | [ Op.or ]: [ | 472 | [Op.or]: [ |
469 | where(fn('lower', col('username')), fn('lower', username)), | 473 | where(fn('lower', col('username')), fn('lower', username)), |
470 | 474 | ||
471 | { email } | 475 | { email } |
@@ -592,7 +596,7 @@ export class UserModel extends Model<UserModel> { | |||
592 | const query = { | 596 | const query = { |
593 | where: { | 597 | where: { |
594 | username: { | 598 | username: { |
595 | [ Op.like ]: `%${search}%` | 599 | [Op.like]: `%${search}%` |
596 | } | 600 | } |
597 | }, | 601 | }, |
598 | limit: 10 | 602 | limit: 10 |
@@ -652,7 +656,7 @@ export class UserModel extends Model<UserModel> { | |||
652 | videoLanguages: this.videoLanguages, | 656 | videoLanguages: this.videoLanguages, |
653 | 657 | ||
654 | role: this.role, | 658 | role: this.role, |
655 | roleLabel: USER_ROLE_LABELS[ this.role ], | 659 | roleLabel: USER_ROLE_LABELS[this.role], |
656 | 660 | ||
657 | videoQuota: this.videoQuota, | 661 | videoQuota: this.videoQuota, |
658 | videoQuotaDaily: this.videoQuotaDaily, | 662 | videoQuotaDaily: this.videoQuotaDaily, |
@@ -686,13 +690,13 @@ export class UserModel extends Model<UserModel> { | |||
686 | 690 | ||
687 | if (Array.isArray(this.Account.VideoChannels) === true) { | 691 | if (Array.isArray(this.Account.VideoChannels) === true) { |
688 | json.videoChannels = this.Account.VideoChannels | 692 | json.videoChannels = this.Account.VideoChannels |
689 | .map(c => c.toFormattedJSON()) | 693 | .map(c => c.toFormattedJSON()) |
690 | .sort((v1, v2) => { | 694 | .sort((v1, v2) => { |
691 | if (v1.createdAt < v2.createdAt) return -1 | 695 | if (v1.createdAt < v2.createdAt) return -1 |
692 | if (v1.createdAt === v2.createdAt) return 0 | 696 | if (v1.createdAt === v2.createdAt) return 0 |
693 | 697 | ||
694 | return 1 | 698 | return 1 |
695 | }) | 699 | }) |
696 | } | 700 | } |
697 | 701 | ||
698 | return json | 702 | return json |
@@ -702,7 +706,7 @@ export class UserModel extends Model<UserModel> { | |||
702 | const formatted = this.toFormattedJSON() | 706 | const formatted = this.toFormattedJSON() |
703 | 707 | ||
704 | const specialPlaylists = this.Account.VideoPlaylists | 708 | const specialPlaylists = this.Account.VideoPlaylists |
705 | .map(p => ({ id: p.id, name: p.name, type: p.type })) | 709 | .map(p => ({ id: p.id, name: p.name, type: p.type })) |
706 | 710 | ||
707 | return Object.assign(formatted, { specialPlaylists }) | 711 | return Object.assign(formatted, { specialPlaylists }) |
708 | } | 712 | } |
@@ -729,12 +733,12 @@ export class UserModel extends Model<UserModel> { | |||
729 | 733 | ||
730 | return 'SELECT SUM("size") AS "total" ' + | 734 | return 'SELECT SUM("size") AS "total" ' + |
731 | 'FROM (' + | 735 | 'FROM (' + |
732 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | 736 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + |
733 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | 737 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + |
734 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | 738 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + |
735 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | 739 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + |
736 | 'WHERE "account"."userId" = $userId ' + andWhere + | 740 | 'WHERE "account"."userId" = $userId ' + andWhere + |
737 | 'GROUP BY "video"."id"' + | 741 | 'GROUP BY "video"."id"' + |
738 | ') t' | 742 | ') t' |
739 | } | 743 | } |
740 | 744 | ||