|
|
@@ -6,6 +6,7 @@ const mongoose = require('mongoose');
|
|
|
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
|
const path = require('path');
|
|
|
const uniqueValidator = require('mongoose-unique-validator');
|
|
|
+const md5 = require('md5');
|
|
|
|
|
|
const ObjectId = mongoose.Schema.Types.ObjectId;
|
|
|
const crypto = require('crypto');
|
|
|
@@ -16,7 +17,8 @@ module.exports = function(crowi) {
|
|
|
const STATUS_SUSPENDED = 3;
|
|
|
const STATUS_DELETED = 4;
|
|
|
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 LANG_EN = 'en';
|
|
|
@@ -38,6 +40,7 @@ module.exports = function(crowi) {
|
|
|
userId: String,
|
|
|
image: String,
|
|
|
imageAttachment: { type: ObjectId, ref: 'Attachment' },
|
|
|
+ imageUrlCached: String,
|
|
|
isGravatarEnabled: { type: Boolean, default: false },
|
|
|
isEmailPublished: { type: Boolean, default: true },
|
|
|
googleId: String,
|
|
|
@@ -190,6 +193,7 @@ module.exports = function(crowi) {
|
|
|
|
|
|
userSchema.methods.updateIsGravatarEnabled = async function(isGravatarEnabled) {
|
|
|
this.isGravatarEnabled = isGravatarEnabled;
|
|
|
+ this.imageUrlCached = this.generateimageUrlCached();
|
|
|
const userData = await this.save();
|
|
|
return userData;
|
|
|
};
|
|
|
@@ -225,6 +229,7 @@ module.exports = function(crowi) {
|
|
|
|
|
|
userSchema.methods.updateImage = async function(attachment) {
|
|
|
this.imageAttachment = attachment;
|
|
|
+ this.imageUrlCached = this.generateimageUrlCached();
|
|
|
return this.save();
|
|
|
};
|
|
|
|
|
|
@@ -240,9 +245,25 @@ module.exports = function(crowi) {
|
|
|
}
|
|
|
|
|
|
this.imageAttachment = undefined;
|
|
|
+ this.imageUrlCached = this.generateimageUrlCached();
|
|
|
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) {
|
|
|
this.googleId = googleId;
|
|
|
this.save((err, userData) => {
|