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