]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/user.js
Server: make a basic "quick and dirty update" for videos
[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 },
feb4bdfd 42 role: {
67bf9b96
C
43 type: DataTypes.ENUM(values(constants.USER_ROLES)),
44 allowNull: false
feb4bdfd
C
45 }
46 },
47 {
319d072e
C
48 indexes: [
49 {
5d67f289
C
50 fields: [ 'username' ],
51 unique: true
ad4a8a1c
C
52 },
53 {
54 fields: [ 'email' ],
55 unique: true
319d072e
C
56 }
57 ],
feb4bdfd
C
58 classMethods: {
59 associate,
60
61 countTotal,
62 getByUsername,
63 list,
64 listForApi,
65 loadById,
ad4a8a1c
C
66 loadByUsername,
67 loadByUsernameOrEmail
feb4bdfd
C
68 },
69 instanceMethods: {
70 isPasswordMatch,
71 toFormatedJSON
72 },
73 hooks: {
74 beforeCreate: beforeCreateOrUpdate,
75 beforeUpdate: beforeCreateOrUpdate
76 }
77 }
78 )
79
80 return User
9bd26629 81}
69b0a27c 82
feb4bdfd
C
83function beforeCreateOrUpdate (user, options, next) {
84 peertubeCrypto.cryptPassword(user.password, function (err, hash) {
26d7d31b
C
85 if (err) return next(err)
86
87 user.password = hash
88
89 return next()
90 })
feb4bdfd 91}
69b0a27c 92
26d7d31b
C
93// ------------------------------ METHODS ------------------------------
94
95function isPasswordMatch (password, callback) {
96 return peertubeCrypto.comparePassword(password, this.password, callback)
97}
98
99function toFormatedJSON () {
100 return {
feb4bdfd 101 id: this.id,
26d7d31b 102 username: this.username,
ad4a8a1c 103 email: this.email,
d74a0680 104 role: this.role,
feb4bdfd 105 createdAt: this.createdAt
26d7d31b
C
106 }
107}
108// ------------------------------ STATICS ------------------------------
69b0a27c 109
feb4bdfd 110function associate (models) {
4712081f
C
111 this.hasOne(models.Author, {
112 foreignKey: 'userId',
113 onDelete: 'cascade'
114 })
115
feb4bdfd
C
116 this.hasMany(models.OAuthToken, {
117 foreignKey: 'userId',
118 onDelete: 'cascade'
119 })
120}
121
5c39adb7 122function countTotal (callback) {
feb4bdfd 123 return this.count().asCallback(callback)
089ff2f2
C
124}
125
26d7d31b 126function getByUsername (username) {
feb4bdfd
C
127 const query = {
128 where: {
129 username: username
130 }
131 }
132
133 return this.findOne(query)
9bd26629
C
134}
135
00d6b0dd 136function list (callback) {
feb4bdfd 137 return this.find().asCallback(callback)
00d6b0dd
C
138}
139
5c39adb7 140function listForApi (start, count, sort, callback) {
feb4bdfd
C
141 const query = {
142 offset: start,
143 limit: count,
144 order: [ modelUtils.getSort(sort) ]
145 }
146
147 return this.findAndCountAll(query).asCallback(function (err, result) {
148 if (err) return callback(err)
149
150 return callback(null, result.rows, result.count)
151 })
69b0a27c
C
152}
153
68a3b9f2 154function loadById (id, callback) {
feb4bdfd 155 return this.findById(id).asCallback(callback)
68a3b9f2
C
156}
157
9bd26629 158function loadByUsername (username, callback) {
feb4bdfd
C
159 const query = {
160 where: {
161 username: username
162 }
163 }
164
165 return this.findOne(query).asCallback(callback)
9bd26629 166}
ad4a8a1c
C
167
168function loadByUsernameOrEmail (username, email, callback) {
169 const query = {
170 where: {
171 $or: [ { username }, { email } ]
172 }
173 }
174
175 return this.findOne(query).asCallback(callback)
176}