Merge pull request #17 from seripap/bugfix/usernameChange

Bugfix/username change
This commit is contained in:
Alan Friedman 2016-02-25 19:07:12 -05:00
commit 0d9b41bb3a
4 changed files with 91 additions and 49 deletions

View File

@ -34,8 +34,12 @@ export default class Chat {
} }
if (options && options.error) { if (options && options.error) {
$el = $('<li class="log-error">').addClass('log').html(message); $el = $('<li class="log-error">').addClass('log').html('ERROR: ' + message);
} else if (options && options.info) { } else if (options && options.warning) {
$el = $('<li class="log-warning">').addClass('log').html('WARNING: ' + message);
} else if (options && options.notice) {
$el = $('<li class="log-info">').addClass('log').html('NOTICE: ' + message);
} else if (options && options.info) {
$el = $('<li class="log-info">').addClass('log').html(message); $el = $('<li class="log-info">').addClass('log').html(message);
} else { } else {
$el = $('<li>').addClass('log').html(message); $el = $('<li>').addClass('log').html(message);
@ -68,9 +72,10 @@ export default class Chat {
const COLORS = [ const COLORS = [
'#e21400', '#ffe400', '#ff8f00', '#e21400', '#ffe400', '#ff8f00',
'#58dc00', '#dd9cff', '#4ae8c4', '#58dc00', '#dd9cff', '#4ae8c4',
'#3b88eb', '#f47777', '#d300e7', '#3b88eb', '#f47777', '#EEACB7',
'#99FF33', '#99CC33', '#999933', '#D3FF3E', '#99CC33', '#999933',
'#996633', '#993333', '#990033', '#996633', '#B8D5B8', '#7FFF38',
'#FADBBC', '#FAE2B7', '#EBE8AF',
]; ];
// Compute hash code // Compute hash code
let hash = 7; let hash = 7;
@ -162,18 +167,18 @@ export default class Chat {
action: () => { action: () => {
let newUsername = trigger.params[0] || false; let newUsername = trigger.params[0] || false;
if (newUsername > 16) { if (newUsername.toString().length > 16) {
return this.log('Username cannot be greater than 16 characters.', {error: true}); return this.log('Username cannot be greater than 16 characters.', {error: true});
} }
// Remove things that arent digits or chars // Remove things that arent digits or chars
newUsername = newUsername.replace(/[^A-Za-z0-9]/g, '-'); newUsername = newUsername.replace(/[^A-Za-z0-9]/g, '-');
if (!newUsername.match(/^[A-Z0-9]/i)) { if (!newUsername.match(/^[A-Z]/i)) {
return this.log('Username must start with a letter or number.', {error: true}); return this.log('Username must start with a letter.', {error: true});
} }
this.darkwire.updateUsername(window.username, newUsername).then((socketData) => { this.darkwire.updateUsername(newUsername).then((socketData) => {
let modifiedSocketData = { let modifiedSocketData = {
username: window.username, username: window.username,
newUsername: socketData.username newUsername: socketData.username
@ -181,6 +186,8 @@ export default class Chat {
this.socket.emit('update user', modifiedSocketData); this.socket.emit('update user', modifiedSocketData);
window.username = username = socketData.username; window.username = username = socketData.username;
}).catch((err) => {
return this.log(err, {error: true});
}); });
} }
}, { }, {
@ -194,7 +201,7 @@ export default class Chat {
return '/' + command; return '/' + command;
}); });
this.log('Valid commands: ' + validCommands.sort().join(', '), {info: true}); this.log('Valid commands: ' + validCommands.sort().join(', '), {notice: true});
} }
}, { }, {
command: 'me', command: 'me',

View File

@ -53,16 +53,12 @@ export default class Darkwire {
return _.findWhere(this._users, {id: id}); return _.findWhere(this._users, {id: id});
} }
getUserByName(username) {
return _.findWhere(this._users, {username: username});
}
updateUser(data) { updateUser(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let user = this.getUserById(data.id); let user = this.getUserById(data.id);
if (!user) { if (!user) {
return reject(); return reject('User cannot be found');
} }
let oldUsername = user.username; let oldUsername = user.username;
@ -105,46 +101,78 @@ export default class Darkwire {
return importKeysPromises; return importKeysPromises;
} }
createUser(resolve, reject, username) {
Promise.all([
this._cryptoUtil.createPrimaryKeys()
])
.then((data) => {
this._keys = {
public: data[0].publicKey,
private: data[0].privateKey
};
return Promise.all([
this._cryptoUtil.exportKey(data[0].publicKey, 'spki')
]);
})
.then((exportedKeys) => {
if (!exportedKeys) {
return reject('Could not create a user session');
}
return resolve({
username: username,
publicKey: exportedKeys[0]
});
});
}
checkSessionUsernames(username) {
let matches = _.find(this._users, (users) => {
return username.toLowerCase() === users.username.toLowerCase();
});
if (matches && matches.username) {
return matches;
}
return false;
}
removeUser(data) { removeUser(data) {
this._users = _.without(this._users, _.findWhere(this._users, {id: data.id})); this._users = _.without(this._users, _.findWhere(this._users, {id: data.id}));
return this._users; return this._users;
} }
updateUsername(username, newUsername) { updateUsername(username) {
let user = null; let user = null;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (newUsername) { // Check if user is here
user = this.getUserByName(username); if (username) {
user = this.getUserById(this._myUserId);
} }
if (username) { if (user) {
if (!user) { // User exists and is attempting to change username
Promise.all([ // Check if anyone else is using the requested username
this._cryptoUtil.createPrimaryKeys() let userExists = this.checkSessionUsernames(username);
])
.then((data) => { if (userExists) {
this._keys = { // Someone else is using the username requested, allow reformatting
public: data[0].publicKey, // if it is owned by the user, else reject the promise
private: data[0].privateKey if (userExists.id !== this._myUserId) {
}; return reject(username + ' is being used by someone else in this chat session.');
return Promise.all([ }
this._cryptoUtil.exportKey(data[0].publicKey, 'spki')
]);
})
.then((exportedKeys) => {
resolve({
username: username,
publicKey: exportedKeys[0]
});
});
} else {
resolve({
username: newUsername,
publicKey: user.publicKey
});
} }
return resolve({
username: username,
publicKey: user.publicKey
});
} }
// User doesn't exist, create the user
return this.createUser(resolve, reject, username);
}); });
} }

View File

@ -48,7 +48,7 @@ $(function() {
fs(window.TEMPORARY, fs(window.TEMPORARY,
100, 100,
() => { () => {
chat.log('WARNING: Your browser is not in incognito mode!', {error: true}); chat.log('Your browser is not in incognito mode!', {warning: true});
}); });
} }
chat.log(moment().format('MMMM Do YYYY, h:mm:ss a'), {info: true}); chat.log(moment().format('MMMM Do YYYY, h:mm:ss a'), {info: true});
@ -211,9 +211,10 @@ $(function() {
darkwire.encodeMessage(cleanedMessage, 'text').then((socketData) => { darkwire.encodeMessage(cleanedMessage, 'text').then((socketData) => {
message.val(''); message.val('');
$('#send-message-btn').removeClass('active'); $('#send-message-btn').removeClass('active');
// Add escaped message since message did not come from the server
chat.addChatMessage({ chat.addChatMessage({
username: username, username: username,
message: cleanedMessage message: escape(cleanedMessage)
}); });
socket.emit('new message', socketData); socket.emit('new message', socketData);
}).catch((err) => { }).catch((err) => {

View File

@ -317,14 +317,20 @@ html.no-touchevents .chat #input-icons {
line-height: 10px; line-height: 10px;
} }
.log-info { .log-error, .log-info, .log-warning {
color: #FFF;
font-weight: bold; font-weight: bold;
} }
.log-error { .log-error {
color: yellow; color: #D8D638;
font-weight: bold; }
.log-info {
color: #FFF;
}
.log-warning {
color: #E0873E;
} }
.action { .action {