aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-22 20:58:25 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-25 17:32:16 +0200
commite02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch)
treeb7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/user.ts
parent65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff)
downloadPeerTube-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.ts110
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 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as Sequelize from 'sequelize'
2 3
3import { getSort } from './utils' 4import { getSort } from './utils'
4import { USER_ROLES } from '../initializers' 5import { USER_ROLES } from '../initializers'
@@ -10,10 +11,29 @@ import {
10 isUserDisplayNSFWValid 11 isUserDisplayNSFWValid
11} from '../helpers' 12} from '../helpers'
12 13
13// --------------------------------------------------------------------------- 14import { addMethodsToModel } from './utils'
14 15import {
15module.exports = function (sequelize, DataTypes) { 16 UserClass,
16 const User = sequelize.define('User', 17 UserInstance,
18 UserAttributes,
19
20 UserMethods
21} from './user-interface'
22
23let User: Sequelize.Model<UserInstance, UserAttributes>
24let isPasswordMatch: UserMethods.IsPasswordMatch
25let toFormatedJSON: UserMethods.ToFormatedJSON
26let isAdmin: UserMethods.IsAdmin
27let countTotal: UserMethods.CountTotal
28let getByUsername: UserMethods.GetByUsername
29let list: UserMethods.List
30let listForApi: UserMethods.ListForApi
31let loadById: UserMethods.LoadById
32let loadByUsername: UserMethods.LoadByUsername
33let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
34
35export 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
98function beforeCreateOrUpdate (user, options, next) { 120function 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
110function isPasswordMatch (password, callback) { 134isPasswordMatch = function (password, callback) {
111 return comparePassword(password, this.password, callback) 135 return comparePassword(password, this.password, callback)
112} 136}
113 137
114function toFormatedJSON () { 138toFormatedJSON = 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
125function isAdmin () { 149isAdmin = 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
131function associate (models) { 155function 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
143function countTotal (callback) { 167countTotal = function (callback) {
144 return this.count().asCallback(callback) 168 return this.count().asCallback(callback)
145} 169}
146 170
147function getByUsername (username) { 171getByUsername = 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
157function list (callback) { 181list = function (callback) {
158 return this.find().asCallback(callback) 182 return User.find().asCallback(callback)
159} 183}
160 184
161function listForApi (start, count, sort, callback) { 185listForApi = 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
175function loadById (id, callback) { 199loadById = function (id, callback) {
176 return this.findById(id).asCallback(callback) 200 return User.findById(id).asCallback(callback)
177} 201}
178 202
179function loadByUsername (username, callback) { 203loadByUsername = 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
189function loadByUsernameOrEmail (username, email, callback) { 213loadByUsernameOrEmail = 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}