From 6fcd19ba737f1f5614a56c6925adb882dea43b8d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Jul 2017 13:26:25 +0200 Subject: Move to promises Closes https://github.com/Chocobozzz/PeerTube/issues/74 --- server/models/user/user-interface.ts | 26 ++++++-------- server/models/user/user-video-rate-interface.ts | 4 +-- server/models/user/user-video-rate.ts | 5 ++- server/models/user/user.ts | 47 +++++++++++-------------- 4 files changed, 35 insertions(+), 47 deletions(-) (limited to 'server/models/user') 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 @@ import * as Sequelize from 'sequelize' -import * as Bluebird from 'bluebird' +import * as Promise from 'bluebird' // Don't use barrel, import just what we need import { UserRole, User as FormatedUser } from '../../../shared/models/user.model' +import { ResultList } from '../../../shared/models/result-list.model' export namespace UserMethods { - export type IsPasswordMatchCallback = (err: Error, same: boolean) => void - export type IsPasswordMatch = (this: UserInstance, password: string, callback: IsPasswordMatchCallback) => void + export type IsPasswordMatch = (this: UserInstance, password: string) => Promise export type ToFormatedJSON = (this: UserInstance) => FormatedUser export type IsAdmin = (this: UserInstance) => boolean - export type CountTotalCallback = (err: Error, total: number) => void - export type CountTotal = (callback: CountTotalCallback) => void + export type CountTotal = () => Promise - export type GetByUsername = (username: string) => Bluebird + export type GetByUsername = (username: string) => Promise - export type ListCallback = (err: Error, userInstances: UserInstance[]) => void - export type List = (callback: ListCallback) => void + export type List = () => Promise - export type ListForApiCallback = (err: Error, userInstances?: UserInstance[], total?: number) => void - export type ListForApi = (start: number, count: number, sort: string, callback: ListForApiCallback) => void + export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList > - export type LoadByIdCallback = (err: Error, userInstance: UserInstance) => void - export type LoadById = (id: number, callback: LoadByIdCallback) => void + export type LoadById = (id: number) => Promise - export type LoadByUsernameCallback = (err: Error, userInstance: UserInstance) => void - export type LoadByUsername = (username: string, callback: LoadByUsernameCallback) => void + export type LoadByUsername = (username: string) => Promise - export type LoadByUsernameOrEmailCallback = (err: Error, userInstance: UserInstance) => void - export type LoadByUsernameOrEmail = (username: string, email: string, callback: LoadByUsernameOrEmailCallback) => void + export type LoadByUsernameOrEmail = (username: string, email: string) => Promise } 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 @@ import * as Sequelize from 'sequelize' +import * as Promise from 'bluebird' import { VideoRateType } from '../../../shared/models/user-video-rate.model' export namespace UserVideoRateMethods { - export type LoadCallback = (err: Error, userVideoRateInstance: UserVideoRateInstance) => void - export type Load = (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: LoadCallback) => void + export type Load = (userId: number, videoId: string, transaction: Sequelize.Transaction) => Promise } 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' import { addMethodsToModel } from '../utils' import { - UserVideoRateClass, UserVideoRateInstance, UserVideoRateAttributes, @@ -66,7 +65,7 @@ function associate (models) { }) } -load = function (userId: number, videoId: string, transaction: Sequelize.Transaction, callback: UserVideoRateMethods.LoadCallback) { +load = function (userId: number, videoId: string, transaction: Sequelize.Transaction) { const options: Sequelize.FindOptions = { where: { userId, @@ -75,5 +74,5 @@ load = function (userId: number, videoId: string, transaction: Sequelize.Transac } if (transaction) options.transaction = transaction - return UserVideoRate.findOne(options).asCallback(callback) + return UserVideoRate.findOne(options) } 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 { import { addMethodsToModel } from '../utils' import { - UserClass, UserInstance, UserAttributes, @@ -118,21 +117,16 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da } function beforeCreateOrUpdate (user: UserInstance) { - return new Promise(function (resolve, reject) { - cryptPassword(user.password, function (err, hash) { - if (err) return reject(err) - - user.password = hash - - return resolve() - }) + return cryptPassword(user.password).then(hash => { + user.password = hash + return undefined }) } // ------------------------------ METHODS ------------------------------ -isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) { - return comparePassword(password, this.password, callback) +isPasswordMatch = function (this: UserInstance, password: string) { + return comparePassword(password, this.password) } toFormatedJSON = function (this: UserInstance) { @@ -164,8 +158,8 @@ function associate (models) { }) } -countTotal = function (callback: UserMethods.CountTotalCallback) { - return this.count().asCallback(callback) +countTotal = function () { + return this.count() } getByUsername = function (username: string) { @@ -178,44 +172,45 @@ getByUsername = function (username: string) { return User.findOne(query) } -list = function (callback: UserMethods.ListCallback) { - return User.find().asCallback(callback) +list = function () { + return User.findAll() } -listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) { +listForApi = function (start: number, count: number, sort: string) { const query = { offset: start, limit: count, order: [ getSort(sort) ] } - return User.findAndCountAll(query).asCallback(function (err, result) { - if (err) return callback(err) - - return callback(null, result.rows, result.count) + return User.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } }) } -loadById = function (id: number, callback: UserMethods.LoadByIdCallback) { - return User.findById(id).asCallback(callback) +loadById = function (id: number) { + return User.findById(id) } -loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) { +loadByUsername = function (username: string) { const query = { where: { username: username } } - return User.findOne(query).asCallback(callback) + return User.findOne(query) } -loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) { +loadByUsernameOrEmail = function (username: string, email: string) { const query = { where: { $or: [ { username }, { email } ] } } - return User.findOne(query).asCallback(callback) + return User.findOne(query) } -- cgit v1.2.3