Bladeren bron

Added pub/sub to username changing

theflametrooper 8 jaren geleden
bovenliggende
commit
3ec5693f3b
3 gewijzigde bestanden met toevoegingen van 28 en 15 verwijderingen
  1. 24 8
      backend/logic/actions/users.js
  2. 4 4
      frontend/components/User/Settings.vue
  3. 0 3
      frontend/io.js

+ 24 - 8
backend/logic/actions/users.js

@@ -11,6 +11,14 @@ const utils = require('../utils');
 const hooks = require('./hooks');
 const sha256 = require('sha256');
 
+cache.sub('user.updateUsername', user => {
+	utils.socketsFromUser(user._id, sockets => {
+		sockets.forEach(socket => {
+			socket.emit('event:user.username.changed', user.username);
+		});
+	});
+});
+
 module.exports = {
 
 	login: (session, identifier, password, cb) => {
@@ -210,24 +218,32 @@ module.exports = {
 	updateUsername: hooks.loginRequired((session, newUsername, cb, userId) => {
 		db.models.user.findOne({ _id: userId }, (err, user) => {
 			if (err) console.error(err);
-			if (!user) return cb({ status: 'error', message: 'User not found.' });
+			if (!user) return cb({ status: 'error', message: 'User not found' });
 			if (user.username !== newUsername) {
 				if (user.username.toLowerCase() !== newUsername.toLowerCase()) {
-					db.models.user.findOne({username: new RegExp(`^${newUsername}$`, 'i')}, (err, _user) => {
+					db.models.user.findOne({ username: new RegExp(`^${newUsername}$`, 'i') }, (err, _user) => {
 						if (err) return cb({ status: 'error', message: err.message });
-						if (_user) return cb({ status: 'failure', message: 'That username is already in use.' });
-						db.models.user.update({_id: userId}, {$set: {username: newUsername}}, (err) => {
+						if (_user) return cb({ status: 'failure', message: 'That username is already in use' });
+						db.models.user.update({ _id: userId }, { $set: { username: newUsername } }, (err) => {
 							if (err) return cb({ status: 'error', message: err.message });
-							cb({ status: 'success', message: 'Username updated successfully.' });
+							cache.pub('user.updateUsername', {
+								username: newUsername,
+								_id: userId
+							});
+							cb({ status: 'success', message: 'Username updated successfully' });
 						});
 					});
 				} else {
-					db.models.user.update({_id: userId}, {$set: {username: newUsername}}, (err) => {
+					db.models.user.update({ _id: userId }, { $set: { username: newUsername } }, (err) => {
 						if (err) return cb({ status: 'error', message: err.message });
-						cb({ status: 'success', message: 'Username updated successfully.' });
+						cache.pub('user.updateUsername', {
+							username: newUsername,
+							_id: userId
+						});
+						cb({ status: 'success', message: 'Username updated successfully' });
 					});
 				}
-			} else cb({ status: 'error', message: 'Username has not changed. Your new username cannot be the same as your old username.' });
+			} else cb({ status: 'error', message: 'Your new username cannot be the same as your old username' });
 		});
 	}),
 

+ 4 - 4
frontend/components/User/Settings.vue

@@ -51,6 +51,9 @@
 						Toast.methods.addToast('Your are currently not signed in', 3000);
 					}
 				});
+				_this.socket.on('event:user.username.changed', username => {
+					_this.$parent.username = username;
+				});
 			});
 		},
 		methods: {
@@ -66,10 +69,7 @@
 				if (!_this.user.username) return Toast.methods.addToast('Username cannot be empty', 8000);
 				_this.socket.emit('users.updateUsername', _this.user.username, res => {
 					if (res.status !== 'success') Toast.methods.addToast(res.message, 8000);
-					else {
-						Toast.methods.addToast('Successfully changed username', 4000);
-						_this.$parent.username = _this.user.username;
-					}
+					else Toast.methods.addToast('Successfully changed username', 4000);
 				});
 			}
 		},

+ 0 - 3
frontend/io.js

@@ -36,19 +36,16 @@ export default {
 	init: function (url) {
 		this.socket = window.socket = io(url);
 		this.socket.on('connect', () => {
-			// Connect
 			onConnectCallbacks.forEach((cb) => {
 				cb();
 			});
 		});
 		this.socket.on('disconnect', () => {
-			// Disconnect
 			onDisconnectCallbacks.forEach((cb) => {
 				cb();
 			});
 		});
 		this.socket.on('connect_error', () => {
-			// Connect error
 			onConnectErrorCallbacks.forEach((cb) => {
 				cb();
 			});