aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/user.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-01-03 14:17:57 +0100
committerChocobozzz <me@florianbigard.com>2020-01-03 14:17:57 +0100
commitac0868bcc0259d4ff14265d9ae403e10869a13aa (patch)
tree7a86bc8f4d881360d5b613dc906be1a0d99aa0df /server/models/account/user.ts
parent35f28e94c763370616d25d5820f4b9ef70cedca9 (diff)
downloadPeerTube-ac0868bcc0259d4ff14265d9ae403e10869a13aa.tar.gz
PeerTube-ac0868bcc0259d4ff14265d9ae403e10869a13aa.tar.zst
PeerTube-ac0868bcc0259d4ff14265d9ae403e10869a13aa.zip
Improve SQL query for my special playlists
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r--server/models/account/user.ts72
1 files changed, 38 insertions, 34 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index 8bd41de22..27262af42 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -19,14 +19,15 @@ import {
19 Table, 19 Table,
20 UpdatedAt 20 UpdatedAt
21} from 'sequelize-typescript' 21} from 'sequelize-typescript'
22import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy, MyUser } from '../../../shared' 22import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared'
23import { User, UserRole } from '../../../shared/models/users' 23import { User, UserRole } from '../../../shared/models/users'
24import { 24import {
25 isNoInstanceConfigWarningModal, 25 isNoInstanceConfigWarningModal,
26 isNoWelcomeModal,
26 isUserAdminFlagsValid, 27 isUserAdminFlagsValid,
27 isUserAutoPlayVideoValid,
28 isUserAutoPlayNextVideoValid,
29 isUserAutoPlayNextVideoPlaylistValid, 28 isUserAutoPlayNextVideoPlaylistValid,
29 isUserAutoPlayNextVideoValid,
30 isUserAutoPlayVideoValid,
30 isUserBlockedReasonValid, 31 isUserBlockedReasonValid,
31 isUserBlockedValid, 32 isUserBlockedValid,
32 isUserEmailVerifiedValid, 33 isUserEmailVerifiedValid,
@@ -38,8 +39,7 @@ import {
38 isUserVideoQuotaDailyValid, 39 isUserVideoQuotaDailyValid,
39 isUserVideoQuotaValid, 40 isUserVideoQuotaValid,
40 isUserVideosHistoryEnabledValid, 41 isUserVideosHistoryEnabledValid,
41 isUserWebTorrentEnabledValid, 42 isUserWebTorrentEnabledValid
42 isNoWelcomeModal
43} from '../../helpers/custom-validators/users' 43} from '../../helpers/custom-validators/users'
44import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' 44import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'
45import { OAuthTokenModel } from '../oauth/oauth-token' 45import { OAuthTokenModel } from '../oauth/oauth-token'
@@ -61,16 +61,17 @@ import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
61import { getThemeOrDefault } from '../../lib/plugins/theme-utils' 61import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
62import * as Bluebird from 'bluebird' 62import * as Bluebird from 'bluebird'
63import { 63import {
64 MMyUserFormattable,
64 MUserDefault, 65 MUserDefault,
65 MUserFormattable, 66 MUserFormattable,
66 MUserId, 67 MUserId,
67 MUserNotifSettingChannelDefault, 68 MUserNotifSettingChannelDefault,
68 MUserWithNotificationSetting, MVideoFullLight 69 MUserWithNotificationSetting,
70 MVideoFullLight
69} from '@server/typings/models' 71} from '@server/typings/models'
70 72
71enum ScopeNames { 73enum ScopeNames {
72 WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL', 74 FOR_ME_API = 'FOR_ME_API'
73 WITH_SPECIAL_PLAYLISTS = 'WITH_SPECIAL_PLAYLISTS'
74} 75}
75 76
76@DefaultScope(() => ({ 77@DefaultScope(() => ({
@@ -86,28 +87,31 @@ enum ScopeNames {
86 ] 87 ]
87})) 88}))
88@Scopes(() => ({ 89@Scopes(() => ({
89 [ScopeNames.WITH_VIDEO_CHANNEL]: { 90 [ScopeNames.FOR_ME_API]: {
90 include: [ 91 include: [
91 { 92 {
92 model: AccountModel, 93 model: AccountModel,
93 required: true, 94 include: [
94 include: [ VideoChannelModel ] 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 ]
95 }, 109 },
96 { 110 {
97 model: UserNotificationSettingModel, 111 model: UserNotificationSettingModel,
98 required: true 112 required: true
99 } 113 }
100 ] 114 ]
101 },
102 [ScopeNames.WITH_SPECIAL_PLAYLISTS]: {
103 attributes: {
104 include: [
105 [
106 literal('(select array(select "id" from "videoPlaylist" where "ownerAccountId" in (select id from public.account where "userId" = "UserModel"."id") and name LIKE \'Watch later\'))'),
107 'specialPlaylists'
108 ]
109 ]
110 }
111 } 115 }
112})) 116}))
113@Table({ 117@Table({
@@ -436,17 +440,14 @@ export class UserModel extends Model<UserModel> {
436 return UserModel.findOne(query) 440 return UserModel.findOne(query)
437 } 441 }
438 442
439 static loadByUsernameAndPopulateChannels (username: string): Bluebird<MUserNotifSettingChannelDefault> { 443 static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> {
440 const query = { 444 const query = {
441 where: { 445 where: {
442 username: { [ Op.iLike ]: username } 446 username: { [ Op.iLike ]: username }
443 } 447 }
444 } 448 }
445 449
446 return UserModel.scope([ 450 return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query)
447 ScopeNames.WITH_VIDEO_CHANNEL,
448 ScopeNames.WITH_SPECIAL_PLAYLISTS
449 ]).findOne(query)
450 } 451 }
451 452
452 static loadByEmail (email: string): Bluebird<MUserDefault> { 453 static loadByEmail (email: string): Bluebird<MUserDefault> {
@@ -625,11 +626,11 @@ export class UserModel extends Model<UserModel> {
625 return comparePassword(password, this.password) 626 return comparePassword(password, this.password)
626 } 627 }
627 628
628 toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean, me?: boolean } = {}): User | MyUser { 629 toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User {
629 const videoQuotaUsed = this.get('videoQuotaUsed') 630 const videoQuotaUsed = this.get('videoQuotaUsed')
630 const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') 631 const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily')
631 632
632 const json: User | MyUser = { 633 const json: User = {
633 id: this.id, 634 id: this.id,
634 username: this.username, 635 username: this.username,
635 email: this.email, 636 email: this.email,
@@ -690,15 +691,18 @@ export class UserModel extends Model<UserModel> {
690 }) 691 })
691 } 692 }
692 693
693 if (parameters.me) {
694 Object.assign(json, {
695 specialPlaylists: (this.get('specialPlaylists') as Array<number>).map(p => ({ id: p }))
696 })
697 }
698
699 return json 694 return json
700 } 695 }
701 696
697 toMeFormattedJSON (this: MMyUserFormattable): MyUser {
698 const formatted = this.toFormattedJSON()
699
700 const specialPlaylists = this.Account.VideoPlaylists
701 .map(p => ({ id: p.id, name: p.name, type: p.type }))
702
703 return Object.assign(formatted, { specialPlaylists })
704 }
705
702 async isAbleToUploadVideo (videoFile: { size: number }) { 706 async isAbleToUploadVideo (videoFile: { size: number }) {
703 if (this.videoQuota === -1 && this.videoQuotaDaily === -1) return Promise.resolve(true) 707 if (this.videoQuota === -1 && this.videoQuotaDaily === -1) return Promise.resolve(true)
704 708