]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/user.js
Server: Add NSFW in user profile
[github/Chocobozzz/PeerTube.git] / server / models / user.js
index 36ed723ccd1292423599e7ab069d3ffd4dac1810..e64bab8abc54061778968453e6a7ff0caff71e84 100644 (file)
@@ -32,6 +32,24 @@ module.exports = function (sequelize, DataTypes) {
           }
         }
       },
+      email: {
+        type: DataTypes.STRING(400),
+        allowNull: false,
+        validate: {
+          isEmail: true
+        }
+      },
+      displayNSFW: {
+        type: DataTypes.BOOLEAN,
+        allowNull: false,
+        defaultValue: false,
+        validate: {
+          nsfwValid: function (value) {
+            const res = customUsersValidators.isUserDisplayNSFWValid(value)
+            if (res === false) throw new Error('Display NSFW is not valid.')
+          }
+        }
+      },
       role: {
         type: DataTypes.ENUM(values(constants.USER_ROLES)),
         allowNull: false
@@ -40,7 +58,12 @@ module.exports = function (sequelize, DataTypes) {
     {
       indexes: [
         {
-          fields: [ 'username' ]
+          fields: [ 'username' ],
+          unique: true
+        },
+        {
+          fields: [ 'email' ],
+          unique: true
         }
       ],
       classMethods: {
@@ -51,7 +74,8 @@ module.exports = function (sequelize, DataTypes) {
         list,
         listForApi,
         loadById,
-        loadByUsername
+        loadByUsername,
+        loadByUsernameOrEmail
       },
       instanceMethods: {
         isPasswordMatch,
@@ -87,6 +111,8 @@ function toFormatedJSON () {
   return {
     id: this.id,
     username: this.username,
+    email: this.email,
+    displayNSFW: this.displayNSFW,
     role: this.role,
     createdAt: this.createdAt
   }
@@ -150,3 +176,13 @@ function loadByUsername (username, callback) {
 
   return this.findOne(query).asCallback(callback)
 }
+
+function loadByUsernameOrEmail (username, email, callback) {
+  const query = {
+    where: {
+      $or: [ { username }, { email } ]
+    }
+  }
+
+  return this.findOne(query).asCallback(callback)
+}