Fixing colors, allowing only unique usernames per chat session

This commit is contained in:
Dan Seripap 2016-02-25 12:44:36 -05:00
parent 6ce7c10584
commit d0d47c098a
4 changed files with 69 additions and 43 deletions

View File

@ -34,8 +34,12 @@ export default class Chat {
}
if (options && options.error) {
$el = $('<li class="log-error">').addClass('log').html(message);
} else if (options && options.info) {
$el = $('<li class="log-error">').addClass('log').html('ERROR: ' + message);
} 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);
} else {
$el = $('<li>').addClass('log').html(message);
@ -68,9 +72,10 @@ export default class Chat {
const COLORS = [
'#e21400', '#ffe400', '#ff8f00',
'#58dc00', '#dd9cff', '#4ae8c4',
'#3b88eb', '#f47777', '#d300e7',
'#99FF33', '#99CC33', '#999933',
'#996633', '#993333', '#990033',
'#3b88eb', '#f47777', '#EEACB7',
'#D3FF3E', '#99CC33', '#999933',
'#996633', '#B8D5B8', '#7FFF38',
'#FADBBC', '#FAE2B7', '#EBE8AF',
];
// Compute hash code
let hash = 7;
@ -173,7 +178,7 @@ export default class Chat {
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 = {
username: window.username,
newUsername: socketData.username
@ -181,6 +186,8 @@ export default class Chat {
this.socket.emit('update user', modifiedSocketData);
window.username = username = socketData.username;
}).catch((err) => {
return this.log(err, {error: true});
});
}
}, {
@ -194,7 +201,7 @@ export default class Chat {
return '/' + command;
});
this.log('Valid commands: ' + validCommands.sort().join(', '), {info: true});
this.log('Valid commands: ' + validCommands.sort().join(', '), {notice: true});
}
}, {
command: 'me',

View File

@ -53,16 +53,12 @@ export default class Darkwire {
return _.findWhere(this._users, {id: id});
}
getUserByName(username) {
return _.findWhere(this._users, {username: username});
}
updateUser(data) {
return new Promise((resolve, reject) => {
let user = this.getUserById(data.id);
if (!user) {
return reject();
return reject('User cannot be found');
}
let oldUsername = user.username;
@ -105,45 +101,62 @@ export default class Darkwire {
return importKeysPromises;
}
checkSessionUsernames(username) {
let matches = _.find(this._users, (users) => {
return username.toLowerCase() === users.username.toLowerCase();
});
if (matches && matches.username) {
return matches;
}
return false;
}
removeUser(data) {
this._users = _.without(this._users, _.findWhere(this._users, {id: data.id}));
return this._users;
}
updateUsername(username, newUsername) {
updateUsername(username) {
let user = null;
return new Promise((resolve, reject) => {
if (newUsername) {
user = this.getUserByName(username);
if (username) {
user = this.getUserById(this._myUserId);
}
if (username) {
if (!user) {
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) => {
resolve({
username: username,
publicKey: exportedKeys[0]
});
});
} else {
if (!user) {
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) => {
resolve({
username: newUsername,
publicKey: user.publicKey
username: username,
publicKey: exportedKeys[0]
});
});
} else {
let userExists = this.checkSessionUsernames(username);
if (userExists) {
if (userExists.id !== this._myUserId) {
return reject(username + ' is being used by someone else in this chat session.');
}
}
return resolve({
username: username,
publicKey: user.publicKey
});
}
});
}

View File

@ -48,7 +48,7 @@ $(function() {
fs(window.TEMPORARY,
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});

View File

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