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/user.ts | |
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/user.ts')
-rw-r--r-- | server/models/user/user.ts | 47 |
1 files changed, 21 insertions, 26 deletions
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 | } |