profile: drop gravatar support
gravatar is owned by an external entity (Automattic) and we have an unnecessary dep to this service. users can just upload a profile pic
This commit is contained in:
+17
-62
@@ -6,7 +6,7 @@ exports = module.exports = {
|
||||
setDisplayName,
|
||||
setEmail,
|
||||
setFallbackEmail,
|
||||
getAvatar,
|
||||
getAvatarById,
|
||||
setAvatar,
|
||||
setLanguage,
|
||||
getBackgroundImage,
|
||||
@@ -22,14 +22,8 @@ exports = module.exports = {
|
||||
const assert = require('assert'),
|
||||
AuditSource = require('../auditsource.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
constants = require('../constants.js'),
|
||||
crypto = require('crypto'),
|
||||
dashboard = require('../dashboard.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
https = require('https'),
|
||||
path = require('path'),
|
||||
paths = require('../paths.js'),
|
||||
safe = require('safetydance'),
|
||||
userDirectory = require('../user-directory.js'),
|
||||
users = require('../users.js');
|
||||
@@ -48,19 +42,9 @@ async function canEditProfile(req, res, next) {
|
||||
async function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id));
|
||||
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
const [avatarError, avatar] = await safe(users.getAvatar(req.user.id));
|
||||
if (avatarError) return next(BoxError.toHttpError(error));
|
||||
|
||||
let avatarType;
|
||||
if (avatar.equals(constants.AVATAR_GRAVATAR)) avatarType = constants.AVATAR_GRAVATAR;
|
||||
else if (avatar.equals(constants.AVATAR_NONE)) avatarType = constants.AVATAR_NONE;
|
||||
else avatarType = constants.AVATAR_CUSTOM;
|
||||
|
||||
const { fqdn:dashboardFqdn } = await dashboard.getLocation();
|
||||
|
||||
next(new HttpSuccess(200, {
|
||||
id: req.user.id,
|
||||
username: req.user.username,
|
||||
@@ -73,8 +57,6 @@ async function get(req, res, next) {
|
||||
hasBackgroundImage: !!backgroundImage,
|
||||
language: req.user.language,
|
||||
notificationConfig: req.user.notificationConfig,
|
||||
avatarUrl: `https://${dashboardFqdn}/api/v1/profile/avatar/${req.user.id}`,
|
||||
avatarType: avatarType.toString() // this is a Buffer
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -128,57 +110,30 @@ async function setLanguage(req, res, next) {
|
||||
|
||||
async function setAvatar(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
assert.strictEqual(typeof req.files, 'object');
|
||||
|
||||
let avatar = typeof req.body.avatar === 'string' ? Buffer.from(req.body.avatar, 'utf8') : null;
|
||||
const avatar = safe.fs.readFileSync(req.files.avatar.path);
|
||||
safe.fs.unlinkSync(req.files.avatar.path);
|
||||
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
|
||||
|
||||
if (req.files && req.files.avatar) {
|
||||
avatar = safe.fs.readFileSync(req.files.avatar.path);
|
||||
safe.fs.unlinkSync(req.files.avatar.path);
|
||||
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
|
||||
} else if (!avatar || (!avatar.equals(constants.AVATAR_GRAVATAR) && !avatar.equals(constants.AVATAR_NONE))) {
|
||||
return next(new HttpError(400, `avatar must be a file, ${constants.AVATAR_GRAVATAR} or ${constants.AVATAR_NONE}`));
|
||||
}
|
||||
|
||||
const [error] = await safe(users.setAvatar(req.user.id, avatar));
|
||||
const [error] = await safe(users.setAvatar(req.user, avatar));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(202, {}));
|
||||
}
|
||||
|
||||
async function getAvatar(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.identifier, 'string');
|
||||
async function getAvatarById(req, res, next) {
|
||||
assert.strictEqual(typeof req.params, 'object');
|
||||
|
||||
const [,userId, ext] = req.params.identifier.match(/^(.*?)(?:\.(\w+))?$/);
|
||||
const [userError, user] = await safe(users.get(userId));
|
||||
const [userError, user] = await safe(users.get(req.params.identifier));
|
||||
if (userError) return next(BoxError.toHttpError(userError));
|
||||
|
||||
const [avatarError, avatar] = await safe(users.getAvatar(userId));
|
||||
const [avatarError, avatar] = await safe(users.getAvatar(user));
|
||||
if (avatarError) return next(BoxError.toHttpError(avatarError));
|
||||
if (!avatar) return next(new HttpError(404, 'no avatar'));
|
||||
|
||||
if (avatar.equals(constants.AVATAR_GRAVATAR)) {
|
||||
const gravatarHash = crypto.createHash('md5').update(user.email).digest('hex');
|
||||
https.get(`https://www.gravatar.com/avatar/${gravatarHash}.jpg`, function (upstreamRes) {
|
||||
if (upstreamRes.status !== 200) {
|
||||
console.error('Gravatar error:', upstreamRes.status);
|
||||
return res.status(404).end();
|
||||
}
|
||||
|
||||
res.set('content-type', 'image/jpeg');
|
||||
upstreamRes.pipe(res);
|
||||
}).on('error', (e) => {
|
||||
console.error('Gravatar error:', e.message);
|
||||
return res.status(404).end();
|
||||
});
|
||||
} else if (avatar.equals(constants.AVATAR_NONE)) {
|
||||
if (ext) {
|
||||
res.sendFile(path.join(paths.DASHBOARD_DIR, '/img/avatar-default-symbolic.png'));
|
||||
} else {
|
||||
res.sendFile(path.join(paths.DASHBOARD_DIR, '/img/avatar-default-symbolic.svg'));
|
||||
}
|
||||
} else {
|
||||
res.set('Content-Type', 'image/png');
|
||||
res.send(avatar);
|
||||
}
|
||||
res.set('Content-Type', 'image/png');
|
||||
res.status(200).send(avatar);
|
||||
}
|
||||
|
||||
async function setBackgroundImage(req, res, next) {
|
||||
@@ -190,7 +145,7 @@ async function setBackgroundImage(req, res, next) {
|
||||
safe.fs.unlinkSync(req.files.backgroundImage.path);
|
||||
if (!backgroundImage) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
|
||||
|
||||
const [error] = await safe(users.setBackgroundImage(req.user.id, backgroundImage));
|
||||
const [error] = await safe(users.setBackgroundImage(req.user, backgroundImage));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
@@ -199,7 +154,7 @@ async function setBackgroundImage(req, res, next) {
|
||||
async function unsetBackgroundImage(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
const [error] = await safe(users.setBackgroundImage(req.user.id, null));
|
||||
const [error] = await safe(users.setBackgroundImage(req.user, null));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
@@ -208,7 +163,7 @@ async function unsetBackgroundImage(req, res, next) {
|
||||
async function getBackgroundImage(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id));
|
||||
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
if (!backgroundImage) return next(new HttpError(404, 'no background set'));
|
||||
|
||||
Reference in New Issue
Block a user