itizawa 6 лет назад
Родитель
Сommit
f451713b3e

+ 14 - 1
src/client/js/services/PersonalContainer.js

@@ -149,7 +149,20 @@ export default class PersonalContainer extends Container {
    * @memberOf PersonalContainer
    * @memberOf PersonalContainer
    */
    */
   async updateProfileImage() {
   async updateProfileImage() {
-    // TODO create apiV3
+    try {
+      const response = await this.appContainer.apiv3.put('/personal-setting/image-type', {
+        isGravatarEnabled: this.state.isGravatarEnabled,
+      });
+      const { userData } = response.data;
+      this.setState({
+        isGravatarEnabled: userData.isGravatarEnabled,
+      });
+    }
+    catch (err) {
+      this.setState({ retrieveError: err });
+      logger.error(err);
+      throw new Error('Failed to update profile image');
+    }
   }
   }
 
 
 }
 }

+ 0 - 1
src/server/form/index.js

@@ -7,7 +7,6 @@ module.exports = {
   me: {
   me: {
     user: require('./me/user'),
     user: require('./me/user'),
     password: require('./me/password'),
     password: require('./me/password'),
-    imagetype: require('./me/imagetype'),
     apiToken: require('./me/apiToken'),
     apiToken: require('./me/apiToken'),
   },
   },
   admin: {
   admin: {

+ 0 - 7
src/server/form/me/imagetype.js

@@ -1,7 +0,0 @@
-const form = require('express-form');
-
-const field = form.field;
-
-module.exports = form(
-  field('imagetypeForm.isGravatarEnabled').required(),
-);

+ 6 - 15
src/server/models/user.js

@@ -188,25 +188,16 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
-  userSchema.methods.updateIsGravatarEnabled = function(isGravatarEnabled, callback) {
+  userSchema.methods.updateIsGravatarEnabled = async function(isGravatarEnabled) {
     this.isGravatarEnabled = isGravatarEnabled;
     this.isGravatarEnabled = isGravatarEnabled;
-    this.save((err, userData) => {
-      return callback(err, userData);
-    });
-  };
-
-  userSchema.methods.updateIsEmailPublished = function(isEmailPublished, callback) {
-    this.isEmailPublished = isEmailPublished;
-    this.save((err, userData) => {
-      return callback(err, userData);
-    });
+    const userData = await this.save();
+    return userData;
   };
   };
 
 
-  userSchema.methods.updatePassword = function(password, callback) {
+  userSchema.methods.updatePassword = async function(password) {
     this.setPassword(password);
     this.setPassword(password);
-    this.save((err, userData) => {
-      return callback(err, userData);
-    });
+    const userData = await this.save();
+    return userData;
   };
   };
 
 
   userSchema.methods.canDeleteCompletely = function(creatorId) {
   userSchema.methods.canDeleteCompletely = function(creatorId) {

+ 14 - 0
src/server/routes/apiv3/personal-setting.js

@@ -144,6 +144,20 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
+  // TODO swagger validator
+  router.put('/image-type', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
+    const { isGravatarEnabled } = req.body;
+
+    try {
+      const userData = await req.user.updateIsGravatarEnabled(isGravatarEnabled);
+      return res.apiv3({ userData });
+    }
+    catch (err) {
+      logger.error(err);
+      return res.apiv3Err('update-personal-settings-failed');
+    }
+  });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *

+ 0 - 2
src/server/routes/index.js

@@ -134,8 +134,6 @@ module.exports = function(crowi, app) {
   app.post('/me/external-accounts/disassociate'           , loginRequiredStrictly , me.externalAccounts.disassociate);
   app.post('/me/external-accounts/disassociate'           , loginRequiredStrictly , me.externalAccounts.disassociate);
   app.post('/me/external-accounts/associateLdap'          , loginRequiredStrictly , form.login , me.externalAccounts.associateLdap);
   app.post('/me/external-accounts/associateLdap'          , loginRequiredStrictly , form.login , me.externalAccounts.associateLdap);
 
 
-  app.post('/me/imagetype'            , form.me.imagetype         , loginRequiredStrictly , me.imagetype);
-
   app.get('/:id([0-9a-z]{24})'       , loginRequired , page.redirector);
   app.get('/:id([0-9a-z]{24})'       , loginRequired , page.redirector);
   app.get('/_r/:id([0-9a-z]{24})'    , loginRequired , page.redirector); // alias
   app.get('/_r/:id([0-9a-z]{24})'    , loginRequired , page.redirector); // alias
   app.get('/attachment/:pageId/:fileName'  , loginRequired, attachment.api.obsoletedGetForMongoDB); // DEPRECATED: remains for backward compatibility for v3.3.x or below
   app.get('/attachment/:pageId/:fileName'  , loginRequired, attachment.api.obsoletedGetForMongoDB); // DEPRECATED: remains for backward compatibility for v3.3.x or below

+ 0 - 32
src/server/routes/me.js

@@ -106,38 +106,6 @@ module.exports = function(crowi, app) {
     return res.render('me/index');
     return res.render('me/index');
   };
   };
 
 
-  actions.imagetype = function(req, res) {
-    if (req.method !== 'POST') {
-      // do nothing
-      return;
-    }
-    if (!req.form.isValid) {
-      req.flash('errorMessage', req.form.errors.join('\n'));
-      return;
-    }
-
-    const imagetypeForm = req.body.imagetypeForm;
-    const userData = req.user;
-
-    const isGravatarEnabled = imagetypeForm.isGravatarEnabled;
-
-    userData.updateIsGravatarEnabled(isGravatarEnabled, (err, userData) => {
-      if (err) {
-        /* eslint-disable no-restricted-syntax, no-prototype-builtins */
-        for (const e in err.errors) {
-          if (err.errors.hasOwnProperty(e)) {
-            req.form.errors.push(err.errors[e].message);
-          }
-        }
-        /* eslint-enable no-restricted-syntax, no-prototype-builtins */
-        return res.render('me/index', {});
-      }
-
-      req.flash('successMessage', req.t('Updated'));
-      return res.redirect('/me');
-    });
-  };
-
   actions.externalAccounts = {};
   actions.externalAccounts = {};
   actions.externalAccounts.list = function(req, res) {
   actions.externalAccounts.list = function(req, res) {
     const userData = req.user;
     const userData = req.user;