aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user/user.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-07-05 13:26:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-07-05 14:14:16 +0200
commit6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch)
tree3365a96d82bc7f00ae504a568725c8e914150cf8 /server/models/user/user.ts
parent5fe7e898316e18369c3e1aba307b55077adc7bfb (diff)
downloadPeerTube-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.ts47
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
14import { addMethodsToModel } from '../utils' 14import { addMethodsToModel } from '../utils'
15import { 15import {
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
120function beforeCreateOrUpdate (user: UserInstance) { 119function 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
134isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) { 128isPasswordMatch = function (this: UserInstance, password: string) {
135 return comparePassword(password, this.password, callback) 129 return comparePassword(password, this.password)
136} 130}
137 131
138toFormatedJSON = function (this: UserInstance) { 132toFormatedJSON = function (this: UserInstance) {
@@ -164,8 +158,8 @@ function associate (models) {
164 }) 158 })
165} 159}
166 160
167countTotal = function (callback: UserMethods.CountTotalCallback) { 161countTotal = function () {
168 return this.count().asCallback(callback) 162 return this.count()
169} 163}
170 164
171getByUsername = function (username: string) { 165getByUsername = 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
181list = function (callback: UserMethods.ListCallback) { 175list = function () {
182 return User.find().asCallback(callback) 176 return User.findAll()
183} 177}
184 178
185listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) { 179listForApi = 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
199loadById = function (id: number, callback: UserMethods.LoadByIdCallback) { 194loadById = function (id: number) {
200 return User.findById(id).asCallback(callback) 195 return User.findById(id)
201} 196}
202 197
203loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) { 198loadByUsername = 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
213loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) { 208loadByUsernameOrEmail = 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}