ソースを参照

Merge branch 'experimental' of https://github.com/Musare/MusareNode into experimental

Kristian Vos 5 年 前
コミット
94ec8cfafd

+ 32 - 5
backend/logic/actions/users.js

@@ -201,8 +201,10 @@ module.exports = {
 	 * @param {Function} cb - gets called with the result
 	 */
 	register: async function(session, username, email, password, recaptcha, cb) {
+
 		email = email.toLowerCase();
 		let verificationToken = await utils.generateRandomString(64);
+
 		async.waterfall([
 
 			// verify the request with google recaptcha
@@ -255,9 +257,9 @@ module.exports = {
 				});
 			},
 
-			// save the new user to the database
+			// create the user object
 			(hash, _id, next) => {
-				db.models.user.create({
+				next(null, {
 					_id,
 					username,
 					email: {
@@ -269,12 +271,24 @@ module.exports = {
 							password: hash
 						}
 					}
-				}, next);
+				});
+			},
+
+			// generate the url for gravatar avatar
+			(user, next) => {
+				this.utils.createGravatar(user.email.address).then(url => {
+					user.avatar = url;
+					next(null, user);
+				});
+			},
+
+			// save the new user to the database
+			(user, next) => {
+				db.models.user.create(user, next);
 			},
 
 			// respond with the new user
 			(newUser, next) => {
-				//TODO Send verification email
 				mail.schemas.verifyEmail(email, username, verificationToken, () => {
 					next();
 				});
@@ -423,6 +437,7 @@ module.exports = {
 						username: account.username,
 						role: account.role,
 						email: account.email.address,
+						avatar: account.avatar,
 						createdAt: account.createdAt,
 						statistics: account.statistics,
 						liked: account.liked,
@@ -610,8 +625,20 @@ module.exports = {
 				next('That email is already in use.');
 			},
 
+			// regenerate the url for gravatar avatar
 			(next) => {
-				db.models.user.updateOne({_id: updatingUserId}, {$set: {"email.address": newEmail, "email.verified": false, "email.verificationToken": verificationToken}}, {runValidators: true}, next);
+				utils.createGravatar(newEmail).then(url => next(null, url));
+			},
+
+			(avatar, next) => {
+				db.models.user.updateOne({ _id: updatingUserId }, {
+					$set: {
+						"avatar": avatar,
+						"email.address": newEmail,
+						"email.verified": false,
+						"email.verificationToken": verificationToken
+					}
+				}, { runValidators: true }, next);
 			},
 
 			(res, next) => {

+ 33 - 7
backend/logic/app.js

@@ -75,12 +75,16 @@ module.exports = class extends coreClass {
 
 			app.get('/auth/github/authorize/callback', async (req, res) => {
 				try { await this._validateHook(); } catch { return; }
+
 				let code = req.query.code;
 				let access_token;
 				let body;
 				let address;
+
 				const state = req.query.state;
 
+				const verificationToken = await this.utils.generateRandomString(64);
+
 				async.waterfall([
 					(next) => {
 						if (req.query.error) return next(req.query.error_description);
@@ -158,26 +162,48 @@ module.exports = class extends coreClass {
 					(httpResponse, body2, next) => {
 						body2 = JSON.parse(body2);
 						if (!Array.isArray(body2)) return next(body2.message);
+
 						body2.forEach(email => {
 							if (email.primary) address = email.email.toLowerCase();
 						});
+
 						db.models.user.findOne({'email.address': address}, next);
 					},
 
-					async (user, next) => {
-						const verificationToken = await this.utils.generateRandomString(64);
+					
+					(user, next) => {
+						this.utils.generateRandomString(12).then((_id) => {
+							next(null, user, _id);
+						});
+					},
+
+					(user, _id, next) => {
 						if (user) return next('An account with that email address already exists.');
-						db.models.user.create({
-							_id: await this.utils.generateRandomString(12),//TODO Check if exists
+
+						next(null, {
+							_id, //TODO Check if exists
 							username: body.login,
 							email: {
 								address,
-								verificationToken: verificationToken
+								verificationToken
 							},
 							services: {
-								github: {id: body.id, access_token}
+								github: { id: body.id, access_token }
 							}
-						}, next);
+						});
+					},
+
+					// generate the url for gravatar avatar
+					(user, next) => {
+						this.utils.createGravatar(user.email.address).then(url => {
+							user.avatar = url;
+							next(null, user);
+						});
+					},
+
+					// save the new user to the database
+					(user, next) => {
+						db.models.user.create(user, next);
 					},
 
 					(user, next) => {

+ 1 - 0
backend/logic/db/schemas/user.js

@@ -6,6 +6,7 @@ module.exports = {
 		verificationToken: String,
 		address: String
 	},
+	avatar: { type: String },
 	services: {
 		password: {
 			password: String,

+ 12 - 3
backend/logic/utils.js

@@ -2,9 +2,10 @@
 
 const coreClass = require("../core");
 
-const config  = require('config'),
-	  async	  = require('async'),
-	  request = require('request');
+const config = require('config');
+const async = require('async');
+const request = require('request');
+const crypto = require('crypto');
 
 class Timer {
 	constructor(callback, delay, paused) {
@@ -613,4 +614,12 @@ module.exports = class extends coreClass {
 		}
 		return error;
 	}
+
+	async createGravatar(email) {
+		try { await this._validateHook(); } catch { return; }
+
+		const hash = crypto.createHash('md5').update(email).digest('hex');
+
+		return `https://www.gravatar.com/avatar/${hash}`;
+	}
 }

+ 15 - 1
frontend/components/User/Show.vue

@@ -3,7 +3,15 @@
 		<metadata v-bind:title="`Profile | ${user.username}`" />
 		<main-header />
 		<!--div class="container">
-			<img class="avatar" src="/assets/notes.png" />
+			<img
+				class="avatar"
+				:src="
+					user.avatar
+						? `${user.avatar}?d=${notes}&s=250`
+						: '/assets/notes.png'
+				"
+				onerror="this.src='/assets/notes.png'; this.onerror=''"
+			/>
 			<h2 class="has-text-centered username">@{{ user.username }}</h2>
 			<h5>A member since {{ user.createdAt }}</h5>
 			<div
@@ -121,6 +129,7 @@ export default {
 	data() {
 		return {
 			user: {},
+			notes: "",
 			isUser: false
 		};
 	},
@@ -129,6 +138,11 @@ export default {
 		userId: state => state.user.auth.userId
 	}),
 	mounted() {
+		lofig.get("frontendDomain").then(frontendDomain => {
+			this.frontendDomain = frontendDomain;
+			this.notes = encodeURI(`${this.frontendDomain}/assets/notes.png`);
+		});
+
 		io.getSocket(socket => {
 			this.socket = socket;
 			this.socket.emit(