aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-20 10:13:13 +0200
committerChocobozzz <me@florianbigard.com>2018-09-20 11:45:59 +0200
commit91411dba928678c15a5e99d9795ae061909e397d (patch)
tree7ba6e340cc9eb6f993051fcac74eefd652cb0ffd /server/models/oauth
parentfcc7c060374c3a547257d96af847352c14d6144b (diff)
downloadPeerTube-91411dba928678c15a5e99d9795ae061909e397d.tar.gz
PeerTube-91411dba928678c15a5e99d9795ae061909e397d.tar.zst
PeerTube-91411dba928678c15a5e99d9795ae061909e397d.zip
Limit associations fetch when loading token
Diffstat (limited to 'server/models/oauth')
-rw-r--r--server/models/oauth/oauth-token.ts26
1 files changed, 18 insertions, 8 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts
index 4c53848dc..1dd5e0289 100644
--- a/server/models/oauth/oauth-token.ts
+++ b/server/models/oauth/oauth-token.ts
@@ -1,9 +1,10 @@
1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' 1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2import { logger } from '../../helpers/logger' 2import { logger } from '../../helpers/logger'
3import { AccountModel } from '../account/account'
4import { UserModel } from '../account/user' 3import { UserModel } from '../account/user'
5import { OAuthClientModel } from './oauth-client' 4import { OAuthClientModel } from './oauth-client'
6import { Transaction } from 'sequelize' 5import { Transaction } from 'sequelize'
6import { AccountModel } from '../account/account'
7import { ActorModel } from '../activitypub/actor'
7 8
8export type OAuthTokenInfo = { 9export type OAuthTokenInfo = {
9 refreshToken: string 10 refreshToken: string
@@ -17,18 +18,27 @@ export type OAuthTokenInfo = {
17} 18}
18 19
19enum ScopeNames { 20enum ScopeNames {
20 WITH_ACCOUNT = 'WITH_ACCOUNT' 21 WITH_USER = 'WITH_USER'
21} 22}
22 23
23@Scopes({ 24@Scopes({
24 [ScopeNames.WITH_ACCOUNT]: { 25 [ScopeNames.WITH_USER]: {
25 include: [ 26 include: [
26 { 27 {
27 model: () => UserModel, 28 model: () => UserModel.unscoped(),
29 required: true,
28 include: [ 30 include: [
29 { 31 {
30 model: () => AccountModel, 32 attributes: [ 'id' ],
31 required: true 33 model: () => AccountModel.unscoped(),
34 required: true,
35 include: [
36 {
37 attributes: [ 'id' ],
38 model: () => ActorModel.unscoped(),
39 required: true
40 }
41 ]
32 } 42 }
33 ] 43 ]
34 } 44 }
@@ -138,7 +148,7 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
138 } 148 }
139 } 149 }
140 150
141 return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query).then(token => { 151 return OAuthTokenModel.scope(ScopeNames.WITH_USER).findOne(query).then(token => {
142 if (token) token['user'] = token.User 152 if (token) token['user'] = token.User
143 153
144 return token 154 return token
@@ -152,7 +162,7 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
152 } 162 }
153 } 163 }
154 164
155 return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT) 165 return OAuthTokenModel.scope(ScopeNames.WITH_USER)
156 .findOne(query) 166 .findOne(query)
157 .then(token => { 167 .then(token => {
158 if (token) { 168 if (token) {