]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user.ts
d78f5f845040876ebe643c483a22d70bf043bff3
[github/Chocobozzz/PeerTube.git] / server / models / user / user.ts
1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
3
4 import { getSort } from '../utils'
5 import { USER_ROLES } from '../../initializers'
6 import {
7 cryptPassword,
8 comparePassword,
9 isUserPasswordValid,
10 isUserUsernameValid,
11 isUserDisplayNSFWValid
12 } from '../../helpers'
13
14 import { addMethodsToModel } from '../utils'
15 import {
16 UserClass,
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: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
36 User = sequelize.define<UserInstance, UserAttributes>('User',
37 {
38 password: {
39 type: DataTypes.STRING,
40 allowNull: false,
41 validate: {
42 passwordValid: function (value) {
43 const res = isUserPasswordValid(value)
44 if (res === false) throw new Error('Password not valid.')
45 }
46 }
47 },
48 username: {
49 type: DataTypes.STRING,
50 allowNull: false,
51 validate: {
52 usernameValid: function (value) {
53 const res = isUserUsernameValid(value)
54 if (res === false) throw new Error('Username not valid.')
55 }
56 }
57 },
58 email: {
59 type: DataTypes.STRING(400),
60 allowNull: false,
61 validate: {
62 isEmail: true
63 }
64 },
65 displayNSFW: {
66 type: DataTypes.BOOLEAN,
67 allowNull: false,
68 defaultValue: false,
69 validate: {
70 nsfwValid: function (value) {
71 const res = isUserDisplayNSFWValid(value)
72 if (res === false) throw new Error('Display NSFW is not valid.')
73 }
74 }
75 },
76 role: {
77 type: DataTypes.ENUM(values(USER_ROLES)),
78 allowNull: false
79 }
80 },
81 {
82 indexes: [
83 {
84 fields: [ 'username' ],
85 unique: true
86 },
87 {
88 fields: [ 'email' ],
89 unique: true
90 }
91 ],
92 hooks: {
93 beforeCreate: beforeCreateOrUpdate,
94 beforeUpdate: beforeCreateOrUpdate
95 }
96 }
97 )
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
117 return User
118 }
119
120 function beforeCreateOrUpdate (user: UserInstance) {
121 return new Promise(function (resolve, reject) {
122 cryptPassword(user.password, function (err, hash) {
123 if (err) return reject(err)
124
125 user.password = hash
126
127 return resolve()
128 })
129 })
130 }
131
132 // ------------------------------ METHODS ------------------------------
133
134 isPasswordMatch = function (password: string, callback: UserMethods.IsPasswordMatchCallback) {
135 return comparePassword(password, this.password, callback)
136 }
137
138 toFormatedJSON = function (this: UserInstance) {
139 return {
140 id: this.id,
141 username: this.username,
142 email: this.email,
143 displayNSFW: this.displayNSFW,
144 role: this.role,
145 createdAt: this.createdAt
146 }
147 }
148
149 isAdmin = function () {
150 return this.role === USER_ROLES.ADMIN
151 }
152
153 // ------------------------------ STATICS ------------------------------
154
155 function associate (models) {
156 User.hasOne(models.Author, {
157 foreignKey: 'userId',
158 onDelete: 'cascade'
159 })
160
161 User.hasMany(models.OAuthToken, {
162 foreignKey: 'userId',
163 onDelete: 'cascade'
164 })
165 }
166
167 countTotal = function (callback: UserMethods.CountTotalCallback) {
168 return this.count().asCallback(callback)
169 }
170
171 getByUsername = function (username: string) {
172 const query = {
173 where: {
174 username: username
175 }
176 }
177
178 return User.findOne(query)
179 }
180
181 list = function (callback: UserMethods.ListCallback) {
182 return User.find().asCallback(callback)
183 }
184
185 listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) {
186 const query = {
187 offset: start,
188 limit: count,
189 order: [ getSort(sort) ]
190 }
191
192 return User.findAndCountAll(query).asCallback(function (err, result) {
193 if (err) return callback(err)
194
195 return callback(null, result.rows, result.count)
196 })
197 }
198
199 loadById = function (id: number, callback: UserMethods.LoadByIdCallback) {
200 return User.findById(id).asCallback(callback)
201 }
202
203 loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) {
204 const query = {
205 where: {
206 username: username
207 }
208 }
209
210 return User.findOne(query).asCallback(callback)
211 }
212
213 loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) {
214 const query = {
215 where: {
216 $or: [ { username }, { email } ]
217 }
218 }
219
220 return User.findOne(query).asCallback(callback)
221 }