]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user.ts
Move to promises
[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 UserInstance,
17 UserAttributes,
18
19 UserMethods
20 } from './user-interface'
21
22 let User: Sequelize.Model<UserInstance, UserAttributes>
23 let isPasswordMatch: UserMethods.IsPasswordMatch
24 let toFormatedJSON: UserMethods.ToFormatedJSON
25 let isAdmin: UserMethods.IsAdmin
26 let countTotal: UserMethods.CountTotal
27 let getByUsername: UserMethods.GetByUsername
28 let list: UserMethods.List
29 let listForApi: UserMethods.ListForApi
30 let loadById: UserMethods.LoadById
31 let loadByUsername: UserMethods.LoadByUsername
32 let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
33
34 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
35 User = sequelize.define<UserInstance, UserAttributes>('User',
36 {
37 password: {
38 type: DataTypes.STRING,
39 allowNull: false,
40 validate: {
41 passwordValid: function (value) {
42 const res = isUserPasswordValid(value)
43 if (res === false) throw new Error('Password not valid.')
44 }
45 }
46 },
47 username: {
48 type: DataTypes.STRING,
49 allowNull: false,
50 validate: {
51 usernameValid: function (value) {
52 const res = isUserUsernameValid(value)
53 if (res === false) throw new Error('Username not valid.')
54 }
55 }
56 },
57 email: {
58 type: DataTypes.STRING(400),
59 allowNull: false,
60 validate: {
61 isEmail: true
62 }
63 },
64 displayNSFW: {
65 type: DataTypes.BOOLEAN,
66 allowNull: false,
67 defaultValue: false,
68 validate: {
69 nsfwValid: function (value) {
70 const res = isUserDisplayNSFWValid(value)
71 if (res === false) throw new Error('Display NSFW is not valid.')
72 }
73 }
74 },
75 role: {
76 type: DataTypes.ENUM(values(USER_ROLES)),
77 allowNull: false
78 }
79 },
80 {
81 indexes: [
82 {
83 fields: [ 'username' ],
84 unique: true
85 },
86 {
87 fields: [ 'email' ],
88 unique: true
89 }
90 ],
91 hooks: {
92 beforeCreate: beforeCreateOrUpdate,
93 beforeUpdate: beforeCreateOrUpdate
94 }
95 }
96 )
97
98 const classMethods = [
99 associate,
100
101 countTotal,
102 getByUsername,
103 list,
104 listForApi,
105 loadById,
106 loadByUsername,
107 loadByUsernameOrEmail
108 ]
109 const instanceMethods = [
110 isPasswordMatch,
111 toFormatedJSON,
112 isAdmin
113 ]
114 addMethodsToModel(User, classMethods, instanceMethods)
115
116 return User
117 }
118
119 function beforeCreateOrUpdate (user: UserInstance) {
120 return cryptPassword(user.password).then(hash => {
121 user.password = hash
122 return undefined
123 })
124 }
125
126 // ------------------------------ METHODS ------------------------------
127
128 isPasswordMatch = function (this: UserInstance, password: string) {
129 return comparePassword(password, this.password)
130 }
131
132 toFormatedJSON = function (this: UserInstance) {
133 return {
134 id: this.id,
135 username: this.username,
136 email: this.email,
137 displayNSFW: this.displayNSFW,
138 role: this.role,
139 createdAt: this.createdAt
140 }
141 }
142
143 isAdmin = function (this: UserInstance) {
144 return this.role === USER_ROLES.ADMIN
145 }
146
147 // ------------------------------ STATICS ------------------------------
148
149 function associate (models) {
150 User.hasOne(models.Author, {
151 foreignKey: 'userId',
152 onDelete: 'cascade'
153 })
154
155 User.hasMany(models.OAuthToken, {
156 foreignKey: 'userId',
157 onDelete: 'cascade'
158 })
159 }
160
161 countTotal = function () {
162 return this.count()
163 }
164
165 getByUsername = function (username: string) {
166 const query = {
167 where: {
168 username: username
169 }
170 }
171
172 return User.findOne(query)
173 }
174
175 list = function () {
176 return User.findAll()
177 }
178
179 listForApi = function (start: number, count: number, sort: string) {
180 const query = {
181 offset: start,
182 limit: count,
183 order: [ getSort(sort) ]
184 }
185
186 return User.findAndCountAll(query).then(({ rows, count }) => {
187 return {
188 data: rows,
189 total: count
190 }
191 })
192 }
193
194 loadById = function (id: number) {
195 return User.findById(id)
196 }
197
198 loadByUsername = function (username: string) {
199 const query = {
200 where: {
201 username: username
202 }
203 }
204
205 return User.findOne(query)
206 }
207
208 loadByUsernameOrEmail = function (username: string, email: string) {
209 const query = {
210 where: {
211 $or: [ { username }, { email } ]
212 }
213 }
214
215 return User.findOne(query)
216 }