]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/user.js
Update README features
[github/Chocobozzz/PeerTube.git] / server / models / user.js
CommitLineData
67bf9b96
C
1'use strict'
2
3const values = require('lodash/values')
4
5c39adb7 5const modelUtils = require('./utils')
67bf9b96 6const constants = require('../initializers/constants')
26d7d31b 7const peertubeCrypto = require('../helpers/peertube-crypto')
67bf9b96 8const customUsersValidators = require('../helpers/custom-validators').users
9bd26629 9
69b0a27c
C
10// ---------------------------------------------------------------------------
11
feb4bdfd
C
12module.exports = function (sequelize, DataTypes) {
13 const User = sequelize.define('User',
14 {
15 password: {
67bf9b96
C
16 type: DataTypes.STRING,
17 allowNull: false,
18 validate: {
19 passwordValid: function (value) {
20 const res = customUsersValidators.isUserPasswordValid(value)
21 if (res === false) throw new Error('Password not valid.')
22 }
23 }
feb4bdfd
C
24 },
25 username: {
67bf9b96
C
26 type: DataTypes.STRING,
27 allowNull: false,
28 validate: {
29 usernameValid: function (value) {
30 const res = customUsersValidators.isUserUsernameValid(value)
31 if (res === false) throw new Error('Username not valid.')
32 }
33 }
feb4bdfd 34 },
ad4a8a1c 35 email: {
5804c0db 36 type: DataTypes.STRING(400),
ad4a8a1c
C
37 allowNull: false,
38 validate: {
39 isEmail: true
40 }
41 },
1d49e1e2
C
42 displayNSFW: {
43 type: DataTypes.BOOLEAN,
44 allowNull: false,
45 defaultValue: false,
46 validate: {
47 nsfwValid: function (value) {
48 const res = customUsersValidators.isUserDisplayNSFWValid(value)
49 if (res === false) throw new Error('Display NSFW is not valid.')
50 }
51 }
52 },
feb4bdfd 53 role: {
67bf9b96
C
54 type: DataTypes.ENUM(values(constants.USER_ROLES)),
55 allowNull: false
feb4bdfd
C
56 }
57 },
58 {
319d072e
C
59 indexes: [
60 {
5d67f289
C
61 fields: [ 'username' ],
62 unique: true
ad4a8a1c
C
63 },
64 {
65 fields: [ 'email' ],
66 unique: true
319d072e
C
67 }
68 ],
feb4bdfd
C
69 classMethods: {
70 associate,
71
72 countTotal,
73 getByUsername,
74 list,
75 listForApi,
76 loadById,
ad4a8a1c
C
77 loadByUsername,
78 loadByUsernameOrEmail
feb4bdfd
C
79 },
80 instanceMethods: {
81 isPasswordMatch,
82 toFormatedJSON
83 },
84 hooks: {
85 beforeCreate: beforeCreateOrUpdate,
86 beforeUpdate: beforeCreateOrUpdate
87 }
88 }
89 )
90
91 return User
9bd26629 92}
69b0a27c 93
feb4bdfd
C
94function beforeCreateOrUpdate (user, options, next) {
95 peertubeCrypto.cryptPassword(user.password, function (err, hash) {
26d7d31b
C
96 if (err) return next(err)
97
98 user.password = hash
99
100 return next()
101 })
feb4bdfd 102}
69b0a27c 103
26d7d31b
C
104// ------------------------------ METHODS ------------------------------
105
106function isPasswordMatch (password, callback) {
107 return peertubeCrypto.comparePassword(password, this.password, callback)
108}
109
110function toFormatedJSON () {
111 return {
feb4bdfd 112 id: this.id,
26d7d31b 113 username: this.username,
ad4a8a1c 114 email: this.email,
1d49e1e2 115 displayNSFW: this.displayNSFW,
d74a0680 116 role: this.role,
feb4bdfd 117 createdAt: this.createdAt
26d7d31b
C
118 }
119}
120// ------------------------------ STATICS ------------------------------
69b0a27c 121
feb4bdfd 122function associate (models) {
4712081f
C
123 this.hasOne(models.Author, {
124 foreignKey: 'userId',
125 onDelete: 'cascade'
126 })
127
feb4bdfd
C
128 this.hasMany(models.OAuthToken, {
129 foreignKey: 'userId',
130 onDelete: 'cascade'
131 })
132}
133
5c39adb7 134function countTotal (callback) {
feb4bdfd 135 return this.count().asCallback(callback)
089ff2f2
C
136}
137
26d7d31b 138function getByUsername (username) {
feb4bdfd
C
139 const query = {
140 where: {
141 username: username
142 }
143 }
144
145 return this.findOne(query)
9bd26629
C
146}
147
00d6b0dd 148function list (callback) {
feb4bdfd 149 return this.find().asCallback(callback)
00d6b0dd
C
150}
151
5c39adb7 152function listForApi (start, count, sort, callback) {
feb4bdfd
C
153 const query = {
154 offset: start,
155 limit: count,
156 order: [ modelUtils.getSort(sort) ]
157 }
158
159 return this.findAndCountAll(query).asCallback(function (err, result) {
160 if (err) return callback(err)
161
162 return callback(null, result.rows, result.count)
163 })
69b0a27c
C
164}
165
68a3b9f2 166function loadById (id, callback) {
feb4bdfd 167 return this.findById(id).asCallback(callback)
68a3b9f2
C
168}
169
9bd26629 170function loadByUsername (username, callback) {
feb4bdfd
C
171 const query = {
172 where: {
173 username: username
174 }
175 }
176
177 return this.findOne(query).asCallback(callback)
9bd26629 178}
ad4a8a1c
C
179
180function loadByUsernameOrEmail (username, email, callback) {
181 const query = {
182 where: {
183 $or: [ { username }, { email } ]
184 }
185 }
186
187 return this.findOne(query).asCallback(callback)
188}