diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 13:26:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 14:14:16 +0200 |
commit | 6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch) | |
tree | 3365a96d82bc7f00ae504a568725c8e914150cf8 /server/models/user | |
parent | 5fe7e898316e18369c3e1aba307b55077adc7bfb (diff) | |
download | PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip |
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/models/user')
-rw-r--r-- | server/models/user/user-interface.ts | 26 | ||||
-rw-r--r-- | server/models/user/user-video-rate-interface.ts | 4 | ||||
-rw-r--r-- | server/models/user/user-video-rate.ts | 5 | ||||
-rw-r--r-- | server/models/user/user.ts | 47 |
4 files changed, 35 insertions, 47 deletions
diff --git a/server/models/user/user-interface.ts b/server/models/user/user-interface.ts index 48c67678b..f743945f8 100644 --- a/server/models/user/user-interface.ts +++ b/server/models/user/user-interface.ts | |||
@@ -1,35 +1,29 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Promise from 'bluebird' |
3 | 3 | ||
4 | // Don't use barrel, import just what we need | 4 | // Don't use barrel, import just what we need |
5 | import { UserRole, User as FormatedUser } from '../../../shared/models/user.model' | 5 | import { UserRole, User as FormatedUser } from '../../../shared/models/user.model' |
6 | import { ResultList } from '../../../shared/models/result-list.model' | ||
6 | 7 | ||
7 | export namespace UserMethods { | 8 | export namespace UserMethods { |
8 | export type IsPasswordMatchCallback = (err: Error, same: boolean) => void | 9 | export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean> |
9 | export type IsPasswordMatch = (this: UserInstance, password: string, callback: IsPasswordMatchCallback) => void | ||
10 | 10 | ||
11 | export type ToFormatedJSON = (this: UserInstance) => FormatedUser | 11 | export type ToFormatedJSON = (this: UserInstance) => FormatedUser |
12 | export type IsAdmin = (this: UserInstance) => boolean | 12 | export type IsAdmin = (this: UserInstance) => boolean |
13 | 13 | ||
14 | export type CountTotalCallback = (err: Error, total: number) => void | 14 | export type CountTotal = () => Promise<number> |
15 | export type CountTotal = (callback: CountTotalCallback) => void | ||
16 | 15 | ||
17 | export type GetByUsername = (username: string) => Bluebird<UserInstance> | 16 | export type GetByUsername = (username: string) => Promise<UserInstance> |
18 | 17 | ||
19 | export type ListCallback = (err: Error, userInstances: UserInstance[]) => void | 18 | export type List = () => Promise<UserInstance[]> |
20 | export type List = (callback: ListCallback) => void | ||
21 | 19 | ||
22 | export type ListForApiCallback = (err: Error, userInstances?: UserInstance[], total?: number) => void | 20 | export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<UserInstance> > |
23 | export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void | ||
24 | 21 | ||
25 | export type LoadByIdCallback = (err: Error, userInstance: UserInstance) => void | 22 | export type LoadById = (id: number) => Promise<UserInstance> |
26 | export type LoadById = (id: number, callback: LoadByIdCallback) => void | ||
27 | 23 | ||
28 | export type LoadByUsernameCallback = (err: Error, userInstance: UserInstance) => void | 24 | export type LoadByUsername = (username: string) => Promise<UserInstance> |
29 | export type LoadByUsername = (username: string, callback: LoadByUsernameCallback) => void | ||
30 | 25 | ||
31 | export type LoadByUsernameOrEmailCallback = (err: Error, userInstance: UserInstance) => void | 26 | export type LoadByUsernameOrEmail = (username: string, email: string) => Promise<UserInstance> |
32 | export type LoadByUsernameOrEmail = (username: string, email: string, callback: LoadByUsernameOrEmailCallback) => void | ||
33 | } | 27 | } |
34 | 28 | ||
35 | export interface UserClass { | 29 | export interface UserClass { |
diff --git a/server/models/user/user-video-rate-interface.ts b/server/models/user/user-video-rate-interface.ts index a726639b1..e0b65a13d 100644 --- a/server/models/user/user-video-rate-interface.ts +++ b/server/models/user/user-video-rate-interface.ts | |||
@@ -1,10 +1,10 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import * as Promise from 'bluebird' | ||
2 | 3 | ||
3 | import { VideoRateType } from '../../../shared/models/user-video-rate.model' | 4 | import { VideoRateType } from '../../../shared/models/user-video-rate.model' |
4 | 5 | ||
5 | export namespace UserVideoRateMethods { | 6 | export namespace UserVideoRateMethods { |
6 | export type LoadCallback = (err: Error, userVideoRateInstance: UserVideoRateInstance) => void | 7 | export type Load = (userId: number, videoId: string, transaction: Sequelize.Transaction) => Promise<UserVideoRateInstance> |
7 | export type Load = (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: LoadCallback) => void | ||
8 | } | 8 | } |
9 | 9 | ||
10 | export interface UserVideoRateClass { | 10 | export interface UserVideoRateClass { |
diff --git a/server/models/user/user-video-rate.ts b/server/models/user/user-video-rate.ts index 4bdd35bc9..37d0222cf 100644 --- a/server/models/user/user-video-rate.ts +++ b/server/models/user/user-video-rate.ts | |||
@@ -8,7 +8,6 @@ import { VIDEO_RATE_TYPES } from '../../initializers' | |||
8 | 8 | ||
9 | import { addMethodsToModel } from '../utils' | 9 | import { addMethodsToModel } from '../utils' |
10 | import { | 10 | import { |
11 | UserVideoRateClass, | ||
12 | UserVideoRateInstance, | 11 | UserVideoRateInstance, |
13 | UserVideoRateAttributes, | 12 | UserVideoRateAttributes, |
14 | 13 | ||
@@ -66,7 +65,7 @@ function associate (models) { | |||
66 | }) | 65 | }) |
67 | } | 66 | } |
68 | 67 | ||
69 | load = function (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: UserVideoRateMethods.LoadCallback) { | 68 | load = function (userId: number, videoId: string, transaction: Sequelize.Transaction) { |
70 | const options: Sequelize.FindOptions = { | 69 | const options: Sequelize.FindOptions = { |
71 | where: { | 70 | where: { |
72 | userId, | 71 | userId, |
@@ -75,5 +74,5 @@ load = function (userId: number, videoId: string, transaction: Sequelize.Transac | |||
75 | } | 74 | } |
76 | if (transaction) options.transaction = transaction | 75 | if (transaction) options.transaction = transaction |
77 | 76 | ||
78 | return UserVideoRate.findOne(options).asCallback(callback) | 77 | return UserVideoRate.findOne(options) |
79 | } | 78 | } |
diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 6b2410259..5ff81e741 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts | |||
@@ -13,7 +13,6 @@ import { | |||
13 | 13 | ||
14 | import { addMethodsToModel } from '../utils' | 14 | import { addMethodsToModel } from '../utils' |
15 | import { | 15 | import { |
16 | UserClass, | ||
17 | UserInstance, | 16 | UserInstance, |
18 | UserAttributes, | 17 | UserAttributes, |
19 | 18 | ||
@@ -118,21 +117,16 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
118 | } | 117 | } |
119 | 118 | ||
120 | function beforeCreateOrUpdate (user: UserInstance) { | 119 | function beforeCreateOrUpdate (user: UserInstance) { |
121 | return new Promise(function (resolve, reject) { | 120 | return cryptPassword(user.password).then(hash => { |
122 | cryptPassword(user.password, function (err, hash) { | 121 | user.password = hash |
123 | if (err) return reject(err) | 122 | return undefined |
124 | |||
125 | user.password = hash | ||
126 | |||
127 | return resolve() | ||
128 | }) | ||
129 | }) | 123 | }) |
130 | } | 124 | } |
131 | 125 | ||
132 | // ------------------------------ METHODS ------------------------------ | 126 | // ------------------------------ METHODS ------------------------------ |
133 | 127 | ||
134 | isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) { | 128 | isPasswordMatch = function (this: UserInstance, password: string) { |
135 | return comparePassword(password, this.password, callback) | 129 | return comparePassword(password, this.password) |
136 | } | 130 | } |
137 | 131 | ||
138 | toFormatedJSON = function (this: UserInstance) { | 132 | toFormatedJSON = function (this: UserInstance) { |
@@ -164,8 +158,8 @@ function associate (models) { | |||
164 | }) | 158 | }) |
165 | } | 159 | } |
166 | 160 | ||
167 | countTotal = function (callback: UserMethods.CountTotalCallback) { | 161 | countTotal = function () { |
168 | return this.count().asCallback(callback) | 162 | return this.count() |
169 | } | 163 | } |
170 | 164 | ||
171 | getByUsername = function (username: string) { | 165 | getByUsername = function (username: string) { |
@@ -178,44 +172,45 @@ getByUsername = function (username: string) { | |||
178 | return User.findOne(query) | 172 | return User.findOne(query) |
179 | } | 173 | } |
180 | 174 | ||
181 | list = function (callback: UserMethods.ListCallback) { | 175 | list = function () { |
182 | return User.find().asCallback(callback) | 176 | return User.findAll() |
183 | } | 177 | } |
184 | 178 | ||
185 | listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) { | 179 | listForApi = function (start: number, count: number, sort: string) { |
186 | const query = { | 180 | const query = { |
187 | offset: start, | 181 | offset: start, |
188 | limit: count, | 182 | limit: count, |
189 | order: [ getSort(sort) ] | 183 | order: [ getSort(sort) ] |
190 | } | 184 | } |
191 | 185 | ||
192 | return User.findAndCountAll(query).asCallback(function (err, result) { | 186 | return User.findAndCountAll(query).then(({ rows, count }) => { |
193 | if (err) return callback(err) | 187 | return { |
194 | 188 | data: rows, | |
195 | return callback(null, result.rows, result.count) | 189 | total: count |
190 | } | ||
196 | }) | 191 | }) |
197 | } | 192 | } |
198 | 193 | ||
199 | loadById = function (id: number, callback: UserMethods.LoadByIdCallback) { | 194 | loadById = function (id: number) { |
200 | return User.findById(id).asCallback(callback) | 195 | return User.findById(id) |
201 | } | 196 | } |
202 | 197 | ||
203 | loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) { | 198 | loadByUsername = function (username: string) { |
204 | const query = { | 199 | const query = { |
205 | where: { | 200 | where: { |
206 | username: username | 201 | username: username |
207 | } | 202 | } |
208 | } | 203 | } |
209 | 204 | ||
210 | return User.findOne(query).asCallback(callback) | 205 | return User.findOne(query) |
211 | } | 206 | } |
212 | 207 | ||
213 | loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) { | 208 | loadByUsernameOrEmail = function (username: string, email: string) { |
214 | const query = { | 209 | const query = { |
215 | where: { | 210 | where: { |
216 | $or: [ { username }, { email } ] | 211 | $or: [ { username }, { email } ] |
217 | } | 212 | } |
218 | } | 213 | } |
219 | 214 | ||
220 | return User.findOne(query).asCallback(callback) | 215 | return User.findOne(query) |
221 | } | 216 | } |