aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-16 09:45:46 +0200
commit74889a71fe687dda74f2a687653122327807af36 (patch)
treee938e8b6401b74fbec80513a877d9967f2c0dbcd /server/models/user.ts
parent15a302943d84bc0978b84fe33110c4daa451d311 (diff)
downloadPeerTube-74889a71fe687dda74f2a687653122327807af36.tar.gz
PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.zst
PeerTube-74889a71fe687dda74f2a687653122327807af36.zip
Reorganize model files
Diffstat (limited to 'server/models/user.ts')
-rw-r--r--server/models/user.ts221
1 files changed, 0 insertions, 221 deletions
diff --git a/server/models/user.ts b/server/models/user.ts
deleted file mode 100644
index 1a56a6dc3..000000000
--- a/server/models/user.ts
+++ /dev/null
@@ -1,221 +0,0 @@
1import { values } from 'lodash'
2import * as Sequelize from 'sequelize'
3
4import { getSort } from './utils'
5import { USER_ROLES } from '../initializers'
6import {
7 cryptPassword,
8 comparePassword,
9 isUserPasswordValid,
10 isUserUsernameValid,
11 isUserDisplayNSFWValid
12} from '../helpers'
13
14import { addMethodsToModel } from './utils'
15import {
16 UserClass,
17 UserInstance,
18 UserAttributes,
19
20 UserMethods
21} from './user-interface'
22
23let User: Sequelize.Model<UserInstance, UserAttributes>
24let isPasswordMatch: UserMethods.IsPasswordMatch
25let toFormatedJSON: UserMethods.ToFormatedJSON
26let isAdmin: UserMethods.IsAdmin
27let countTotal: UserMethods.CountTotal
28let getByUsername: UserMethods.GetByUsername
29let list: UserMethods.List
30let listForApi: UserMethods.ListForApi
31let loadById: UserMethods.LoadById
32let loadByUsername: UserMethods.LoadByUsername
33let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
34
35export 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
120function 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
134isPasswordMatch = function (password: string, callback: UserMethods.IsPasswordMatchCallback) {
135 return comparePassword(password, this.password, callback)
136}
137
138toFormatedJSON = 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
149isAdmin = function () {
150 return this.role === USER_ROLES.ADMIN
151}
152
153// ------------------------------ STATICS ------------------------------
154
155function 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
167countTotal = function (callback: UserMethods.CountTotalCallback) {
168 return this.count().asCallback(callback)
169}
170
171getByUsername = function (username: string) {
172 const query = {
173 where: {
174 username: username
175 }
176 }
177
178 return User.findOne(query)
179}
180
181list = function (callback: UserMethods.ListCallback) {
182 return User.find().asCallback(callback)
183}
184
185listForApi = 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
199loadById = function (id: number, callback: UserMethods.LoadByIdCallback) {
200 return User.findById(id).asCallback(callback)
201}
202
203loadByUsername = 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
213loadByUsernameOrEmail = 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}