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 | |
parent | 1735c825726edaa0af5035cb6cbb0cc0db502c6d (diff) | |
download | PeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.gz PeerTube-3acc50844047a37698f0618fa235c138e386a053.tar.zst PeerTube-3acc50844047a37698f0618fa235c138e386a053.zip |
Upgrade sequelize
Diffstat (limited to 'server/models/account')
-rw-r--r-- | server/models/account/account-blocklist.ts | 10 | ||||
-rw-r--r-- | server/models/account/account.ts | 10 | ||||
-rw-r--r-- | server/models/account/user-notification.ts | 44 | ||||
-rw-r--r-- | server/models/account/user.ts | 58 |
4 files changed, 59 insertions, 63 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index efd6ed59e..d5746ad76 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts | |||
@@ -8,22 +8,22 @@ enum ScopeNames { | |||
8 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | 8 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' |
9 | } | 9 | } |
10 | 10 | ||
11 | @Scopes({ | 11 | @Scopes(() => ({ |
12 | [ScopeNames.WITH_ACCOUNTS]: { | 12 | [ScopeNames.WITH_ACCOUNTS]: { |
13 | include: [ | 13 | include: [ |
14 | { | 14 | { |
15 | model: () => AccountModel, | 15 | model: AccountModel, |
16 | required: true, | 16 | required: true, |
17 | as: 'ByAccount' | 17 | as: 'ByAccount' |
18 | }, | 18 | }, |
19 | { | 19 | { |
20 | model: () => AccountModel, | 20 | model: AccountModel, |
21 | required: true, | 21 | required: true, |
22 | as: 'BlockedAccount' | 22 | as: 'BlockedAccount' |
23 | } | 23 | } |
24 | ] | 24 | ] |
25 | } | 25 | } |
26 | }) | 26 | })) |
27 | 27 | ||
28 | @Table({ | 28 | @Table({ |
29 | tableName: 'accountBlocklist', | 29 | tableName: 'accountBlocklist', |
@@ -83,7 +83,7 @@ export class AccountBlocklistModel extends Model<AccountBlocklistModel> { | |||
83 | attributes: [ 'accountId', 'id' ], | 83 | attributes: [ 'accountId', 'id' ], |
84 | where: { | 84 | where: { |
85 | accountId: { | 85 | accountId: { |
86 | [Op.any]: accountIds | 86 | [Op.in]: accountIds // FIXME: sequelize ANY seems broken |
87 | }, | 87 | }, |
88 | targetAccountId | 88 | targetAccountId |
89 | }, | 89 | }, |
diff --git a/server/models/account/account.ts b/server/models/account/account.ts index bf2ed0a61..c53312990 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts | |||
@@ -33,15 +33,15 @@ export enum ScopeNames { | |||
33 | SUMMARY = 'SUMMARY' | 33 | SUMMARY = 'SUMMARY' |
34 | } | 34 | } |
35 | 35 | ||
36 | @DefaultScope({ | 36 | @DefaultScope(() => ({ |
37 | include: [ | 37 | include: [ |
38 | { | 38 | { |
39 | model: () => ActorModel, // Default scope includes avatar and server | 39 | model: ActorModel, // Default scope includes avatar and server |
40 | required: true | 40 | required: true |
41 | } | 41 | } |
42 | ] | 42 | ] |
43 | }) | 43 | })) |
44 | @Scopes({ | 44 | @Scopes(() => ({ |
45 | [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { | 45 | [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { |
46 | return { | 46 | return { |
47 | attributes: [ 'id', 'name' ], | 47 | attributes: [ 'id', 'name' ], |
@@ -66,7 +66,7 @@ export enum ScopeNames { | |||
66 | ] | 66 | ] |
67 | } | 67 | } |
68 | } | 68 | } |
69 | }) | 69 | })) |
70 | @Table({ | 70 | @Table({ |
71 | tableName: 'account', | 71 | tableName: 'account', |
72 | indexes: [ | 72 | indexes: [ |
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 08388f268..a4f97037b 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts | |||
@@ -6,7 +6,7 @@ import { isUserNotificationTypeValid } from '../../helpers/custom-validators/use | |||
6 | import { UserModel } from './user' | 6 | import { UserModel } from './user' |
7 | import { VideoModel } from '../video/video' | 7 | import { VideoModel } from '../video/video' |
8 | import { VideoCommentModel } from '../video/video-comment' | 8 | import { VideoCommentModel } from '../video/video-comment' |
9 | import { FindOptions, Op } from 'sequelize' | 9 | import { FindOptions, ModelIndexesOptions, Op, WhereOptions } from 'sequelize' |
10 | import { VideoChannelModel } from '../video/video-channel' | 10 | import { VideoChannelModel } from '../video/video-channel' |
11 | import { AccountModel } from './account' | 11 | import { AccountModel } from './account' |
12 | import { VideoAbuseModel } from '../video/video-abuse' | 12 | import { VideoAbuseModel } from '../video/video-abuse' |
@@ -24,17 +24,17 @@ enum ScopeNames { | |||
24 | function buildActorWithAvatarInclude () { | 24 | function buildActorWithAvatarInclude () { |
25 | return { | 25 | return { |
26 | attributes: [ 'preferredUsername' ], | 26 | attributes: [ 'preferredUsername' ], |
27 | model: () => ActorModel.unscoped(), | 27 | model: ActorModel.unscoped(), |
28 | required: true, | 28 | required: true, |
29 | include: [ | 29 | include: [ |
30 | { | 30 | { |
31 | attributes: [ 'filename' ], | 31 | attributes: [ 'filename' ], |
32 | model: () => AvatarModel.unscoped(), | 32 | model: AvatarModel.unscoped(), |
33 | required: false | 33 | required: false |
34 | }, | 34 | }, |
35 | { | 35 | { |
36 | attributes: [ 'host' ], | 36 | attributes: [ 'host' ], |
37 | model: () => ServerModel.unscoped(), | 37 | model: ServerModel.unscoped(), |
38 | required: false | 38 | required: false |
39 | } | 39 | } |
40 | ] | 40 | ] |
@@ -44,7 +44,7 @@ function buildActorWithAvatarInclude () { | |||
44 | function buildVideoInclude (required: boolean) { | 44 | function buildVideoInclude (required: boolean) { |
45 | return { | 45 | return { |
46 | attributes: [ 'id', 'uuid', 'name' ], | 46 | attributes: [ 'id', 'uuid', 'name' ], |
47 | model: () => VideoModel.unscoped(), | 47 | model: VideoModel.unscoped(), |
48 | required | 48 | required |
49 | } | 49 | } |
50 | } | 50 | } |
@@ -53,7 +53,7 @@ function buildChannelInclude (required: boolean, withActor = false) { | |||
53 | return { | 53 | return { |
54 | required, | 54 | required, |
55 | attributes: [ 'id', 'name' ], | 55 | attributes: [ 'id', 'name' ], |
56 | model: () => VideoChannelModel.unscoped(), | 56 | model: VideoChannelModel.unscoped(), |
57 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | 57 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] |
58 | } | 58 | } |
59 | } | 59 | } |
@@ -62,12 +62,12 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
62 | return { | 62 | return { |
63 | required, | 63 | required, |
64 | attributes: [ 'id', 'name' ], | 64 | attributes: [ 'id', 'name' ], |
65 | model: () => AccountModel.unscoped(), | 65 | model: AccountModel.unscoped(), |
66 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | 66 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] |
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
70 | @Scopes({ | 70 | @Scopes(() => ({ |
71 | [ScopeNames.WITH_ALL]: { | 71 | [ScopeNames.WITH_ALL]: { |
72 | include: [ | 72 | include: [ |
73 | Object.assign(buildVideoInclude(false), { | 73 | Object.assign(buildVideoInclude(false), { |
@@ -76,7 +76,7 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
76 | 76 | ||
77 | { | 77 | { |
78 | attributes: [ 'id', 'originCommentId' ], | 78 | attributes: [ 'id', 'originCommentId' ], |
79 | model: () => VideoCommentModel.unscoped(), | 79 | model: VideoCommentModel.unscoped(), |
80 | required: false, | 80 | required: false, |
81 | include: [ | 81 | include: [ |
82 | buildAccountInclude(true, true), | 82 | buildAccountInclude(true, true), |
@@ -86,56 +86,56 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
86 | 86 | ||
87 | { | 87 | { |
88 | attributes: [ 'id' ], | 88 | attributes: [ 'id' ], |
89 | model: () => VideoAbuseModel.unscoped(), | 89 | model: VideoAbuseModel.unscoped(), |
90 | required: false, | 90 | required: false, |
91 | include: [ buildVideoInclude(true) ] | 91 | include: [ buildVideoInclude(true) ] |
92 | }, | 92 | }, |
93 | 93 | ||
94 | { | 94 | { |
95 | attributes: [ 'id' ], | 95 | attributes: [ 'id' ], |
96 | model: () => VideoBlacklistModel.unscoped(), | 96 | model: VideoBlacklistModel.unscoped(), |
97 | required: false, | 97 | required: false, |
98 | include: [ buildVideoInclude(true) ] | 98 | include: [ buildVideoInclude(true) ] |
99 | }, | 99 | }, |
100 | 100 | ||
101 | { | 101 | { |
102 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], | 102 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], |
103 | model: () => VideoImportModel.unscoped(), | 103 | model: VideoImportModel.unscoped(), |
104 | required: false, | 104 | required: false, |
105 | include: [ buildVideoInclude(false) ] | 105 | include: [ buildVideoInclude(false) ] |
106 | }, | 106 | }, |
107 | 107 | ||
108 | { | 108 | { |
109 | attributes: [ 'id', 'state' ], | 109 | attributes: [ 'id', 'state' ], |
110 | model: () => ActorFollowModel.unscoped(), | 110 | model: ActorFollowModel.unscoped(), |
111 | required: false, | 111 | required: false, |
112 | include: [ | 112 | include: [ |
113 | { | 113 | { |
114 | attributes: [ 'preferredUsername' ], | 114 | attributes: [ 'preferredUsername' ], |
115 | model: () => ActorModel.unscoped(), | 115 | model: ActorModel.unscoped(), |
116 | required: true, | 116 | required: true, |
117 | as: 'ActorFollower', | 117 | as: 'ActorFollower', |
118 | include: [ | 118 | include: [ |
119 | { | 119 | { |
120 | attributes: [ 'id', 'name' ], | 120 | attributes: [ 'id', 'name' ], |
121 | model: () => AccountModel.unscoped(), | 121 | model: AccountModel.unscoped(), |
122 | required: true | 122 | required: true |
123 | }, | 123 | }, |
124 | { | 124 | { |
125 | attributes: [ 'filename' ], | 125 | attributes: [ 'filename' ], |
126 | model: () => AvatarModel.unscoped(), | 126 | model: AvatarModel.unscoped(), |
127 | required: false | 127 | required: false |
128 | }, | 128 | }, |
129 | { | 129 | { |
130 | attributes: [ 'host' ], | 130 | attributes: [ 'host' ], |
131 | model: () => ServerModel.unscoped(), | 131 | model: ServerModel.unscoped(), |
132 | required: false | 132 | required: false |
133 | } | 133 | } |
134 | ] | 134 | ] |
135 | }, | 135 | }, |
136 | { | 136 | { |
137 | attributes: [ 'preferredUsername' ], | 137 | attributes: [ 'preferredUsername' ], |
138 | model: () => ActorModel.unscoped(), | 138 | model: ActorModel.unscoped(), |
139 | required: true, | 139 | required: true, |
140 | as: 'ActorFollowing', | 140 | as: 'ActorFollowing', |
141 | include: [ | 141 | include: [ |
@@ -147,9 +147,9 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
147 | }, | 147 | }, |
148 | 148 | ||
149 | buildAccountInclude(false, true) | 149 | buildAccountInclude(false, true) |
150 | ] as any // FIXME: sequelize typings | 150 | ] |
151 | } | 151 | } |
152 | }) | 152 | })) |
153 | @Table({ | 153 | @Table({ |
154 | tableName: 'userNotification', | 154 | tableName: 'userNotification', |
155 | indexes: [ | 155 | indexes: [ |
@@ -212,7 +212,7 @@ function buildAccountInclude (required: boolean, withActor = false) { | |||
212 | } | 212 | } |
213 | } | 213 | } |
214 | } | 214 | } |
215 | ] as any // FIXME: sequelize typings | 215 | ] as (ModelIndexesOptions & { where?: WhereOptions })[] |
216 | }) | 216 | }) |
217 | export class UserNotificationModel extends Model<UserNotificationModel> { | 217 | export class UserNotificationModel extends Model<UserNotificationModel> { |
218 | 218 | ||
@@ -357,7 +357,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
357 | where: { | 357 | where: { |
358 | userId, | 358 | userId, |
359 | id: { | 359 | id: { |
360 | [Op.any]: notificationIds | 360 | [Op.in]: notificationIds // FIXME: sequelize ANY seems broken |
361 | } | 361 | } |
362 | } | 362 | } |
363 | } | 363 | } |
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 | } |