diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-23 09:50:57 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-24 16:26:21 +0200 |
commit | 3acc50844047a37698f0618fa235c138e386a053 (patch) | |
tree | e33243bf7fadbcf2df616fc41814245094fd881a /server/models/account/user.ts | |
parent | 1735c825726edaa0af5035cb6cbb0cc0db502c6d (diff) | |
download | PeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.gz PeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.zst PeerTube-3acc50844047a37698f0618fa235c138e386a053.zip |
Upgrade sequelize
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r-- | server/models/account/user.ts | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 8bd0397dd..4a9acd703 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import { FindOptions, literal, Op, QueryTypes } from 'sequelize' |
2 | import { | 2 | import { |
3 | AfterDestroy, | 3 | AfterDestroy, |
4 | AfterUpdate, | 4 | AfterUpdate, |
@@ -56,33 +56,33 @@ enum ScopeNames { | |||
56 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' | 56 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' |
57 | } | 57 | } |
58 | 58 | ||
59 | @DefaultScope({ | 59 | @DefaultScope(() => ({ |
60 | include: [ | 60 | include: [ |
61 | { | 61 | { |
62 | model: () => AccountModel, | 62 | model: AccountModel, |
63 | required: true | 63 | required: true |
64 | }, | 64 | }, |
65 | { | 65 | { |
66 | model: () => UserNotificationSettingModel, | 66 | model: UserNotificationSettingModel, |
67 | required: true | 67 | required: true |
68 | } | 68 | } |
69 | ] | 69 | ] |
70 | }) | 70 | })) |
71 | @Scopes({ | 71 | @Scopes(() => ({ |
72 | [ScopeNames.WITH_VIDEO_CHANNEL]: { | 72 | [ScopeNames.WITH_VIDEO_CHANNEL]: { |
73 | include: [ | 73 | include: [ |
74 | { | 74 | { |
75 | model: () => AccountModel, | 75 | model: AccountModel, |
76 | required: true, | 76 | required: true, |
77 | include: [ () => VideoChannelModel ] | 77 | include: [ VideoChannelModel ] |
78 | }, | 78 | }, |
79 | { | 79 | { |
80 | model: () => UserNotificationSettingModel, | 80 | model: UserNotificationSettingModel, |
81 | required: true | 81 | required: true |
82 | } | 82 | } |
83 | ] as any // FIXME: sequelize typings | 83 | ] |
84 | } | 84 | } |
85 | }) | 85 | })) |
86 | @Table({ | 86 | @Table({ |
87 | tableName: 'user', | 87 | tableName: 'user', |
88 | indexes: [ | 88 | indexes: [ |
@@ -233,26 +233,26 @@ export class UserModel extends Model<UserModel> { | |||
233 | let where = undefined | 233 | let where = undefined |
234 | if (search) { | 234 | if (search) { |
235 | where = { | 235 | where = { |
236 | [Sequelize.Op.or]: [ | 236 | [Op.or]: [ |
237 | { | 237 | { |
238 | email: { | 238 | email: { |
239 | [Sequelize.Op.iLike]: '%' + search + '%' | 239 | [Op.iLike]: '%' + search + '%' |
240 | } | 240 | } |
241 | }, | 241 | }, |
242 | { | 242 | { |
243 | username: { | 243 | username: { |
244 | [ Sequelize.Op.iLike ]: '%' + search + '%' | 244 | [ Op.iLike ]: '%' + search + '%' |
245 | } | 245 | } |
246 | } | 246 | } |
247 | ] | 247 | ] |
248 | } | 248 | } |
249 | } | 249 | } |
250 | 250 | ||
251 | const query = { | 251 | const query: FindOptions = { |
252 | attributes: { | 252 | attributes: { |
253 | include: [ | 253 | include: [ |
254 | [ | 254 | [ |
255 | Sequelize.literal( | 255 | literal( |
256 | '(' + | 256 | '(' + |
257 | 'SELECT COALESCE(SUM("size"), 0) ' + | 257 | 'SELECT COALESCE(SUM("size"), 0) ' + |
258 | 'FROM (' + | 258 | 'FROM (' + |
@@ -265,7 +265,7 @@ export class UserModel extends Model<UserModel> { | |||
265 | ')' | 265 | ')' |
266 | ), | 266 | ), |
267 | 'videoQuotaUsed' | 267 | 'videoQuotaUsed' |
268 | ] as any // FIXME: typings | 268 | ] |
269 | ] | 269 | ] |
270 | }, | 270 | }, |
271 | offset: start, | 271 | offset: start, |
@@ -291,7 +291,7 @@ export class UserModel extends Model<UserModel> { | |||
291 | const query = { | 291 | const query = { |
292 | where: { | 292 | where: { |
293 | role: { | 293 | role: { |
294 | [Sequelize.Op.in]: roles | 294 | [Op.in]: roles |
295 | } | 295 | } |
296 | } | 296 | } |
297 | } | 297 | } |
@@ -387,7 +387,7 @@ export class UserModel extends Model<UserModel> { | |||
387 | 387 | ||
388 | const query = { | 388 | const query = { |
389 | where: { | 389 | where: { |
390 | [ Sequelize.Op.or ]: [ { username }, { email } ] | 390 | [ Op.or ]: [ { username }, { email } ] |
391 | } | 391 | } |
392 | } | 392 | } |
393 | 393 | ||
@@ -510,7 +510,7 @@ export class UserModel extends Model<UserModel> { | |||
510 | const query = { | 510 | const query = { |
511 | where: { | 511 | where: { |
512 | username: { | 512 | username: { |
513 | [ Sequelize.Op.like ]: `%${search}%` | 513 | [ Op.like ]: `%${search}%` |
514 | } | 514 | } |
515 | }, | 515 | }, |
516 | limit: 10 | 516 | limit: 10 |
@@ -591,15 +591,11 @@ export class UserModel extends Model<UserModel> { | |||
591 | 591 | ||
592 | const uploadedTotal = videoFile.size + totalBytes | 592 | const uploadedTotal = videoFile.size + totalBytes |
593 | const uploadedDaily = videoFile.size + totalBytesDaily | 593 | const uploadedDaily = videoFile.size + totalBytesDaily |
594 | if (this.videoQuotaDaily === -1) { | ||
595 | return uploadedTotal < this.videoQuota | ||
596 | } | ||
597 | if (this.videoQuota === -1) { | ||
598 | return uploadedDaily < this.videoQuotaDaily | ||
599 | } | ||
600 | 594 | ||
601 | return (uploadedTotal < this.videoQuota) && | 595 | if (this.videoQuotaDaily === -1) return uploadedTotal < this.videoQuota |
602 | (uploadedDaily < this.videoQuotaDaily) | 596 | if (this.videoQuota === -1) return uploadedDaily < this.videoQuotaDaily |
597 | |||
598 | return uploadedTotal < this.videoQuota && uploadedDaily < this.videoQuotaDaily | ||
603 | } | 599 | } |
604 | 600 | ||
605 | private static generateUserQuotaBaseSQL (where?: string) { | 601 | private static generateUserQuotaBaseSQL (where?: string) { |
@@ -619,14 +615,14 @@ export class UserModel extends Model<UserModel> { | |||
619 | private static getTotalRawQuery (query: string, userId: number) { | 615 | private static getTotalRawQuery (query: string, userId: number) { |
620 | const options = { | 616 | const options = { |
621 | bind: { userId }, | 617 | bind: { userId }, |
622 | type: Sequelize.QueryTypes.SELECT as Sequelize.QueryTypes.SELECT | 618 | type: QueryTypes.SELECT as QueryTypes.SELECT |
623 | } | 619 | } |
624 | 620 | ||
625 | return UserModel.sequelize.query<{ total: number }>(query, options) | 621 | return UserModel.sequelize.query<{ total: string }>(query, options) |
626 | .then(([ { total } ]) => { | 622 | .then(([ { total } ]) => { |
627 | if (total === null) return 0 | 623 | if (total === null) return 0 |
628 | 624 | ||
629 | return parseInt(total + '', 10) | 625 | return parseInt(total, 10) |
630 | }) | 626 | }) |
631 | } | 627 | } |
632 | } | 628 | } |