]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/user.js
Server: add user list sort/pagination
[github/Chocobozzz/PeerTube.git] / server / models / user.js
index 14ffecbff1ca89ae5139a26946d1b54344fc5e12..c9c35b3e2dffc0c06cbe6d35f379135998aae583 100644 (file)
@@ -1,28 +1,65 @@
 const mongoose = require('mongoose')
 
+const customUsersValidators = require('../helpers/custom-validators').users
+const modelUtils = require('./utils')
+
 // ---------------------------------------------------------------------------
 
 const UserSchema = mongoose.Schema({
+  createdDate: {
+    type: Date,
+    default: Date.now
+  },
   password: String,
-  username: String
+  username: String,
+  role: String
 })
 
-UserSchema.path('password').required(true)
-UserSchema.path('username').required(true)
+UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
+UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
+UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
+
+UserSchema.methods = {
+  toFormatedJSON: toFormatedJSON
+}
 
 UserSchema.statics = {
+  countTotal: countTotal,
   getByUsernameAndPassword: getByUsernameAndPassword,
-  list: list
+  listForApi: listForApi,
+  loadById: loadById,
+  loadByUsername: loadByUsername
 }
 
 mongoose.model('User', UserSchema)
 
 // ---------------------------------------------------------------------------
 
-function list (callback) {
-  return this.find(callback)
+function countTotal (callback) {
+  return this.count(callback)
 }
 
 function getByUsernameAndPassword (username, password) {
   return this.findOne({ username: username, password: password })
 }
+
+function listForApi (start, count, sort, callback) {
+  const query = {}
+  return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
+}
+
+function loadById (id, callback) {
+  return this.findById(id, callback)
+}
+
+function loadByUsername (username, callback) {
+  return this.findOne({ username: username }, callback)
+}
+
+function toFormatedJSON () {
+  return {
+    id: this._id,
+    username: this.username,
+    role: this.role
+  }
+}