diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-22 20:58:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-25 17:32:16 +0200 |
commit | e02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch) | |
tree | b7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/user.ts | |
parent | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff) | |
download | PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip |
Type models
Diffstat (limited to 'server/models/user.ts')
-rw-r--r-- | server/models/user.ts | 110 |
1 files changed, 67 insertions, 43 deletions
diff --git a/server/models/user.ts b/server/models/user.ts index d63a50cc4..12ddaaeb7 100644 --- a/server/models/user.ts +++ b/server/models/user.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | ||
2 | 3 | ||
3 | import { getSort } from './utils' | 4 | import { getSort } from './utils' |
4 | import { USER_ROLES } from '../initializers' | 5 | import { USER_ROLES } from '../initializers' |
@@ -10,10 +11,29 @@ import { | |||
10 | isUserDisplayNSFWValid | 11 | isUserDisplayNSFWValid |
11 | } from '../helpers' | 12 | } from '../helpers' |
12 | 13 | ||
13 | // --------------------------------------------------------------------------- | 14 | import { addMethodsToModel } from './utils' |
14 | 15 | import { | |
15 | module.exports = function (sequelize, DataTypes) { | 16 | UserClass, |
16 | const User = sequelize.define('User', | 17 | UserInstance, |
18 | UserAttributes, | ||
19 | |||
20 | UserMethods | ||
21 | } from './user-interface' | ||
22 | |||
23 | let User: Sequelize.Model<UserInstance, UserAttributes> | ||
24 | let isPasswordMatch: UserMethods.IsPasswordMatch | ||
25 | let toFormatedJSON: UserMethods.ToFormatedJSON | ||
26 | let isAdmin: UserMethods.IsAdmin | ||
27 | let countTotal: UserMethods.CountTotal | ||
28 | let getByUsername: UserMethods.GetByUsername | ||
29 | let list: UserMethods.List | ||
30 | let listForApi: UserMethods.ListForApi | ||
31 | let loadById: UserMethods.LoadById | ||
32 | let loadByUsername: UserMethods.LoadByUsername | ||
33 | let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail | ||
34 | |||
35 | export default function (sequelize, DataTypes) { | ||
36 | User = sequelize.define('User', | ||
17 | { | 37 | { |
18 | password: { | 38 | password: { |
19 | type: DataTypes.STRING, | 39 | type: DataTypes.STRING, |
@@ -69,22 +89,6 @@ module.exports = function (sequelize, DataTypes) { | |||
69 | unique: true | 89 | unique: true |
70 | } | 90 | } |
71 | ], | 91 | ], |
72 | classMethods: { | ||
73 | associate, | ||
74 | |||
75 | countTotal, | ||
76 | getByUsername, | ||
77 | list, | ||
78 | listForApi, | ||
79 | loadById, | ||
80 | loadByUsername, | ||
81 | loadByUsernameOrEmail | ||
82 | }, | ||
83 | instanceMethods: { | ||
84 | isPasswordMatch, | ||
85 | toFormatedJSON, | ||
86 | isAdmin | ||
87 | }, | ||
88 | hooks: { | 92 | hooks: { |
89 | beforeCreate: beforeCreateOrUpdate, | 93 | beforeCreate: beforeCreateOrUpdate, |
90 | beforeUpdate: beforeCreateOrUpdate | 94 | beforeUpdate: beforeCreateOrUpdate |
@@ -92,26 +96,46 @@ module.exports = function (sequelize, DataTypes) { | |||
92 | } | 96 | } |
93 | ) | 97 | ) |
94 | 98 | ||
99 | const classMethods = [ | ||
100 | associate, | ||
101 | |||
102 | countTotal, | ||
103 | getByUsername, | ||
104 | list, | ||
105 | listForApi, | ||
106 | loadById, | ||
107 | loadByUsername, | ||
108 | loadByUsernameOrEmail | ||
109 | ] | ||
110 | const instanceMethods = [ | ||
111 | isPasswordMatch, | ||
112 | toFormatedJSON, | ||
113 | isAdmin | ||
114 | ] | ||
115 | addMethodsToModel(User, classMethods, instanceMethods) | ||
116 | |||
95 | return User | 117 | return User |
96 | } | 118 | } |
97 | 119 | ||
98 | function beforeCreateOrUpdate (user, options, next) { | 120 | function beforeCreateOrUpdate (user, options) { |
99 | cryptPassword(user.password, function (err, hash) { | 121 | return new Promise(function (resolve, reject) { |
100 | if (err) return next(err) | 122 | cryptPassword(user.password, function (err, hash) { |
123 | if (err) return reject(err) | ||
101 | 124 | ||
102 | user.password = hash | 125 | user.password = hash |
103 | 126 | ||
104 | return next() | 127 | return resolve() |
128 | }) | ||
105 | }) | 129 | }) |
106 | } | 130 | } |
107 | 131 | ||
108 | // ------------------------------ METHODS ------------------------------ | 132 | // ------------------------------ METHODS ------------------------------ |
109 | 133 | ||
110 | function isPasswordMatch (password, callback) { | 134 | isPasswordMatch = function (password, callback) { |
111 | return comparePassword(password, this.password, callback) | 135 | return comparePassword(password, this.password, callback) |
112 | } | 136 | } |
113 | 137 | ||
114 | function toFormatedJSON () { | 138 | toFormatedJSON = function () { |
115 | return { | 139 | return { |
116 | id: this.id, | 140 | id: this.id, |
117 | username: this.username, | 141 | username: this.username, |
@@ -122,76 +146,76 @@ function toFormatedJSON () { | |||
122 | } | 146 | } |
123 | } | 147 | } |
124 | 148 | ||
125 | function isAdmin () { | 149 | isAdmin = function () { |
126 | return this.role === USER_ROLES.ADMIN | 150 | return this.role === USER_ROLES.ADMIN |
127 | } | 151 | } |
128 | 152 | ||
129 | // ------------------------------ STATICS ------------------------------ | 153 | // ------------------------------ STATICS ------------------------------ |
130 | 154 | ||
131 | function associate (models) { | 155 | function associate (models) { |
132 | this.hasOne(models.Author, { | 156 | User.hasOne(models.Author, { |
133 | foreignKey: 'userId', | 157 | foreignKey: 'userId', |
134 | onDelete: 'cascade' | 158 | onDelete: 'cascade' |
135 | }) | 159 | }) |
136 | 160 | ||
137 | this.hasMany(models.OAuthToken, { | 161 | User.hasMany(models.OAuthToken, { |
138 | foreignKey: 'userId', | 162 | foreignKey: 'userId', |
139 | onDelete: 'cascade' | 163 | onDelete: 'cascade' |
140 | }) | 164 | }) |
141 | } | 165 | } |
142 | 166 | ||
143 | function countTotal (callback) { | 167 | countTotal = function (callback) { |
144 | return this.count().asCallback(callback) | 168 | return this.count().asCallback(callback) |
145 | } | 169 | } |
146 | 170 | ||
147 | function getByUsername (username) { | 171 | getByUsername = function (username) { |
148 | const query = { | 172 | const query = { |
149 | where: { | 173 | where: { |
150 | username: username | 174 | username: username |
151 | } | 175 | } |
152 | } | 176 | } |
153 | 177 | ||
154 | return this.findOne(query) | 178 | return User.findOne(query) |
155 | } | 179 | } |
156 | 180 | ||
157 | function list (callback) { | 181 | list = function (callback) { |
158 | return this.find().asCallback(callback) | 182 | return User.find().asCallback(callback) |
159 | } | 183 | } |
160 | 184 | ||
161 | function listForApi (start, count, sort, callback) { | 185 | listForApi = function (start, count, sort, callback) { |
162 | const query = { | 186 | const query = { |
163 | offset: start, | 187 | offset: start, |
164 | limit: count, | 188 | limit: count, |
165 | order: [ getSort(sort) ] | 189 | order: [ getSort(sort) ] |
166 | } | 190 | } |
167 | 191 | ||
168 | return this.findAndCountAll(query).asCallback(function (err, result) { | 192 | return User.findAndCountAll(query).asCallback(function (err, result) { |
169 | if (err) return callback(err) | 193 | if (err) return callback(err) |
170 | 194 | ||
171 | return callback(null, result.rows, result.count) | 195 | return callback(null, result.rows, result.count) |
172 | }) | 196 | }) |
173 | } | 197 | } |
174 | 198 | ||
175 | function loadById (id, callback) { | 199 | loadById = function (id, callback) { |
176 | return this.findById(id).asCallback(callback) | 200 | return User.findById(id).asCallback(callback) |
177 | } | 201 | } |
178 | 202 | ||
179 | function loadByUsername (username, callback) { | 203 | loadByUsername = function (username, callback) { |
180 | const query = { | 204 | const query = { |
181 | where: { | 205 | where: { |
182 | username: username | 206 | username: username |
183 | } | 207 | } |
184 | } | 208 | } |
185 | 209 | ||
186 | return this.findOne(query).asCallback(callback) | 210 | return User.findOne(query).asCallback(callback) |
187 | } | 211 | } |
188 | 212 | ||
189 | function loadByUsernameOrEmail (username, email, callback) { | 213 | loadByUsernameOrEmail = function (username, email, callback) { |
190 | const query = { | 214 | const query = { |
191 | where: { | 215 | where: { |
192 | $or: [ { username }, { email } ] | 216 | $or: [ { username }, { email } ] |
193 | } | 217 | } |
194 | } | 218 | } |
195 | 219 | ||
196 | return this.findOne(query).asCallback(callback) | 220 | return User.findOne(query).asCallback(callback) |
197 | } | 221 | } |