]> 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 944986a44dfb9ffbe7ec0c4eb83d4730b7921b51..e64bab8abc54061778968453e6a7ff0caff71e84 100644 (file)
@@ -32,12 +32,40 @@ 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
       }
     },
     {
+      indexes: [
+        {
+          fields: [ 'username' ],
+          unique: true
+        },
+        {
+          fields: [ 'email' ],
+          unique: true
+        }
+      ],
       classMethods: {
         associate,
 
@@ -46,7 +74,8 @@ module.exports = function (sequelize, DataTypes) {
         list,
         listForApi,
         loadById,
-        loadByUsername
+        loadByUsername,
+        loadByUsernameOrEmail
       },
       instanceMethods: {
         isPasswordMatch,
@@ -82,6 +111,8 @@ function toFormatedJSON () {
   return {
     id: this.id,
     username: this.username,
+    email: this.email,
+    displayNSFW: this.displayNSFW,
     role: this.role,
     createdAt: this.createdAt
   }
@@ -89,6 +120,11 @@ function toFormatedJSON () {
 // ------------------------------ STATICS ------------------------------
 
 function associate (models) {
+  this.hasOne(models.Author, {
+    foreignKey: 'userId',
+    onDelete: 'cascade'
+  })
+
   this.hasMany(models.OAuthToken, {
     foreignKey: 'userId',
     onDelete: 'cascade'
@@ -140,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)
+}