Browse Source

Merge pull request #1948 from weseek/add-profile-img-cache-to-user

Add imageAttachmentPath to user
yusuketk 6 years ago
parent
commit
aaaa580f55
2 changed files with 24 additions and 2 deletions
  1. 22 1
      src/server/models/user.js
  2. 2 1
      src/server/routes/index.js

+ 22 - 1
src/server/models/user.js

@@ -6,6 +6,7 @@ const mongoose = require('mongoose');
 const mongoosePaginate = require('mongoose-paginate-v2');
 const mongoosePaginate = require('mongoose-paginate-v2');
 const path = require('path');
 const path = require('path');
 const uniqueValidator = require('mongoose-unique-validator');
 const uniqueValidator = require('mongoose-unique-validator');
+const md5 = require('md5');
 
 
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const crypto = require('crypto');
 const crypto = require('crypto');
@@ -16,7 +17,8 @@ module.exports = function(crowi) {
   const STATUS_SUSPENDED = 3;
   const STATUS_SUSPENDED = 3;
   const STATUS_DELETED = 4;
   const STATUS_DELETED = 4;
   const STATUS_INVITED = 5;
   const STATUS_INVITED = 5;
-  const USER_PUBLIC_FIELDS = '_id image isEmailPublished isGravatarEnabled googleId name username email introduction status lang createdAt lastLoginAt admin';
+  const USER_PUBLIC_FIELDS = '_id image isEmailPublished isGravatarEnabled googleId name username email introduction'
+  + 'status lang createdAt lastLoginAt admin imageUrlCached';
   const IMAGE_POPULATION = { path: 'imageAttachment', select: 'filePathProxied' };
   const IMAGE_POPULATION = { path: 'imageAttachment', select: 'filePathProxied' };
 
 
   const LANG_EN = 'en';
   const LANG_EN = 'en';
@@ -38,6 +40,7 @@ module.exports = function(crowi) {
     userId: String,
     userId: String,
     image: String,
     image: String,
     imageAttachment: { type: ObjectId, ref: 'Attachment' },
     imageAttachment: { type: ObjectId, ref: 'Attachment' },
+    imageUrlCached: String,
     isGravatarEnabled: { type: Boolean, default: false },
     isGravatarEnabled: { type: Boolean, default: false },
     isEmailPublished: { type: Boolean, default: true },
     isEmailPublished: { type: Boolean, default: true },
     googleId: String,
     googleId: String,
@@ -190,6 +193,7 @@ module.exports = function(crowi) {
 
 
   userSchema.methods.updateIsGravatarEnabled = async function(isGravatarEnabled) {
   userSchema.methods.updateIsGravatarEnabled = async function(isGravatarEnabled) {
     this.isGravatarEnabled = isGravatarEnabled;
     this.isGravatarEnabled = isGravatarEnabled;
+    this.imageUrlCached = this.generateimageUrlCached();
     const userData = await this.save();
     const userData = await this.save();
     return userData;
     return userData;
   };
   };
@@ -225,6 +229,7 @@ module.exports = function(crowi) {
 
 
   userSchema.methods.updateImage = async function(attachment) {
   userSchema.methods.updateImage = async function(attachment) {
     this.imageAttachment = attachment;
     this.imageAttachment = attachment;
+    this.imageUrlCached = this.generateimageUrlCached();
     return this.save();
     return this.save();
   };
   };
 
 
@@ -240,9 +245,25 @@ module.exports = function(crowi) {
     }
     }
 
 
     this.imageAttachment = undefined;
     this.imageAttachment = undefined;
+    this.imageUrlCached = this.generateimageUrlCached();
     return this.save();
     return this.save();
   };
   };
 
 
+  userSchema.methods.generateimageUrlCached = function() {
+    if (this.isGravatarEnabled) {
+      const email = this.email || '';
+      const hash = md5(email.trim().toLowerCase());
+      return `https://gravatar.com/avatar/${hash}`;
+    }
+    if (this.image) {
+      return this.image;
+    }
+    if (this.imageAttachment != null) {
+      return this.imageAttachment.filePathProxied;
+    }
+    return '/images/icons/user.svg';
+  };
+
   userSchema.methods.updateGoogleId = function(googleId, callback) {
   userSchema.methods.updateGoogleId = function(googleId, callback) {
     this.googleId = googleId;
     this.googleId = googleId;
     this.save((err, userData) => {
     this.save((err, userData) => {

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

@@ -124,8 +124,9 @@ module.exports = function(crowi, app) {
 
 
   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/:id([0-9a-z]{24})'  , loginRequired, attachment.api.get);
   app.get('/attachment/:id([0-9a-z]{24})'  , loginRequired, attachment.api.get);
+  app.get('/attachment/profile/:id([0-9a-z]{24})' , loginRequired, attachment.api.get);
+  app.get('/attachment/:pageId/:fileName', loginRequired, attachment.api.obsoletedGetForMongoDB); // DEPRECATED: remains for backward compatibility for v3.3.x or below
   app.get('/download/:id([0-9a-z]{24})'    , loginRequired, attachment.api.download);
   app.get('/download/:id([0-9a-z]{24})'    , loginRequired, attachment.api.download);
 
 
   app.get('/_search'                 , loginRequired , search.searchPage);
   app.get('/_search'                 , loginRequired , search.searchPage);