From 697a76c5a9707551061baf4abf6a50b23ae30081 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Sat, 28 Sep 2013 17:54:27 +0100 Subject: [PATCH] Client-side support for server-time CAP --- client/assets/src/helpers/utils.js | 28 +++++++++++++++++++++++++++ client/assets/src/models/channel.js | 30 ++++++++++++++--------------- client/assets/src/models/network.js | 29 +++++++++++++++------------- client/assets/src/views/channel.js | 5 +++-- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/client/assets/src/helpers/utils.js b/client/assets/src/helpers/utils.js index 8c9e78c..a9624e4 100644 --- a/client/assets/src/helpers/utils.js +++ b/client/assets/src/helpers/utils.js @@ -450,3 +450,31 @@ function emoticonFromText(str) { return words_out.join(' '); } + +// Code based on http://anentropic.wordpress.com/2009/06/25/javascript-iso8601-parser-and-pretty-dates/#comment-154 +function parseISO8601(str) { + if (Date.prototype.toISOString) { + return new Date(str); + } else { + var parts = str.split('T'), + dateParts = parts[0].split('-'), + timeParts = parts[1].split('Z'), + timeSubParts = timeParts[0].split(':'), + timeSecParts = timeSubParts[2].split('.'), + timeHours = Number(timeSubParts[0]), + _date = new Date(); + + _date.setUTCFullYear(Number(dateParts[0])); + _date.setUTCDate(1); + _date.setUTCMonth(Number(dateParts[1])-1); + _date.setUTCDate(Number(dateParts[2])); + _date.setUTCHours(Number(timeHours)); + _date.setUTCMinutes(Number(timeSubParts[1])); + _date.setUTCSeconds(Number(timeSecParts[0])); + if (timeSecParts[1]) { + _date.setUTCMilliseconds(Number(timeSecParts[1])); + } + + return _date; + } +} \ No newline at end of file diff --git a/client/assets/src/models/channel.js b/client/assets/src/models/channel.js index 06b9d38..7502237 100644 --- a/client/assets/src/models/channel.js +++ b/client/assets/src/models/channel.js @@ -16,35 +16,34 @@ _kiwi.model.Channel = _kiwi.model.Panel.extend({ members = this.get("members"); members.channel = this; - members.bind("add", function (member) { + members.bind("add", function (member, members, options) { var show_message = _kiwi.global.settings.get('show_joins_parts'); if (show_message === false) { return; } - this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_join').fetch(member.displayNick(true)), 'action join'); + this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_join').fetch(member.displayNick(true)), 'action join', {time: options.kiwi.time}); }, this); members.bind("remove", function (member, members, options) { var show_message = _kiwi.global.settings.get('show_joins_parts'); - var msg = (options.message) ? '(' + options.message + ')' : ''; + var msg = (options.kiwi.message) ? '(' + options.kiwi.message + ')' : ''; - if (options.type === 'quit' && show_message) { - this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_quit').fetch(member.displayNick(true), msg), 'action quit'); + if (options.kiwi.type === 'quit' && show_message) { + this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_quit').fetch(member.displayNick(true), msg), 'action quit', {time: options.kiwi.time}); - } else if(options.type === 'kick') { + } else if (options.kiwi.type === 'kick') { - if (!options.current_user_kicked) { + if (!options.kiwi.current_user_kicked) { //If user kicked someone, show the message regardless of settings. - if (show_message || options.current_user_initiated) { - this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_kicked').fetch(member.displayNick(true), options.by, msg), 'action kick'); + if (show_message || options.kiwi.current_user_initiated) { + this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_kicked').fetch(member.displayNick(true), options.kiwi.by, msg), 'action kick', {time: options.kiwi.time}); } } else { - this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_selfkick').fetch(options.by, msg), 'action kick'); + this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_selfkick').fetch(options.kiwi.by, msg), 'action kick', {time: options.kiwi.time}); } } else if (show_message) { - - this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_part').fetch(member.displayNick(true), msg), 'action part'); + this.addMsg(' ', '== ' + _kiwi.global.i18n.translate('client_models_channel_part').fetch(member.displayNick(true), msg), 'action part', {time: options.kiwi.time}); } }, this); }, @@ -57,9 +56,10 @@ _kiwi.model.Channel = _kiwi.model.Panel.extend({ opts = opts || {}; // Time defaults to now - if (!opts || typeof opts.time === 'undefined') { - d = opts.date = new Date(); - opts.time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0"); + if (typeof opts.time === 'undefined') { + opts.time = new Date(); + } else if (typeof opts.time === 'string') { + opts.time = parseISO8601(opts.time); } // CSS style defaults to empty string diff --git a/client/assets/src/models/network.js b/client/assets/src/models/network.js index 5f773b5..71c8c9c 100644 --- a/client/assets/src/models/network.js +++ b/client/assets/src/models/network.js @@ -271,7 +271,7 @@ if (!members) return; user = new _kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname}); - members.add(user); + members.add(user, {kiwi: event}); } @@ -282,6 +282,7 @@ part_options.type = 'part'; part_options.message = event.message || ''; + part_options.time = event.time; channel = this.panels.getByName(event.channel); if (!channel) return; @@ -298,7 +299,7 @@ user = members.getByNick(event.nick); if (!user) return; - members.remove(user, part_options); + members.remove(user, {kiwi: part_options}); } @@ -309,13 +310,14 @@ quit_options.type = 'quit'; quit_options.message = event.message || ''; + quit_options.time = event.time; $.each(this.panels.models, function (index, panel) { if (!panel.isChannel()) return; member = panel.get('members').getByNick(event.nick); if (member) { - panel.get('members').remove(member, quit_options); + panel.get('members').remove(member, {kiwi: quit_options}); } }); } @@ -329,8 +331,9 @@ part_options.type = 'kick'; part_options.by = event.nick; part_options.message = event.message || ''; - part_options.current_user_kicked = (event.kicked == this.get('nick')) - part_options.current_user_initiated = (event.nick == this.get('nick')) + part_options.current_user_kicked = (event.kicked == this.get('nick')); + part_options.current_user_initiated = (event.nick == this.get('nick')); + part_options.time = event.time; channel = this.panels.getByName(event.channel); if (!channel) return; @@ -342,7 +345,7 @@ if (!user) return; - members.remove(user, part_options); + members.remove(user, {kiwi: part_options}); if (part_options.current_user_kicked) { members.reset([]); @@ -377,7 +380,7 @@ } } - panel.addMsg(event.nick, event.msg); + panel.addMsg(event.nick, event.msg, 'privmsg', {time: event.time}); } @@ -394,7 +397,7 @@ member = panel.get('members').getByNick(event.nick); if (member) { member.set('nick', event.newnick); - panel.addMsg('', '== ' + _kiwi.global.i18n.translate('client_models_network_nickname_changed').fetch(event.nick, event.newnick) , 'action nick'); + panel.addMsg('', '== ' + _kiwi.global.i18n.translate('client_models_network_nickname_changed').fetch(event.nick, event.newnick) , 'action nick', {time: event.time}); } }); } @@ -421,7 +424,7 @@ return; } - this.panels.server.addMsg('[' + event.nick + ']', 'CTCP ' + event.msg); + this.panels.server.addMsg('[' + event.nick + ']', 'CTCP ' + event.msg, 'ctcp', {time: event.time}); } @@ -455,11 +458,11 @@ panel = this.panels.server; } - panel.addMsg('[' + (event.nick||'') + ']', event.msg); + panel.addMsg('[' + (event.nick||'') + ']', event.msg, 'notice', {time: event.time}); // Show this notice to the active panel if it didn't have a set target if (!event.from_server && panel === this.panels.server && _kiwi.app.panels().active !== this.panels.server) - _kiwi.app.panels().active.addMsg('[' + (event.nick||'') + ']', event.msg); + _kiwi.app.panels().active.addMsg('[' + (event.nick||'') + ']', event.msg, 'notice', {time: event.time}); } @@ -490,7 +493,7 @@ } } - panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action'); + panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action', {time: event.time}); } @@ -624,7 +627,7 @@ } } - channel.addMsg('', '== ' + _kiwi.global.i18n.translate('client_models_network_mode').fetch(event.nick, friendlyModeString()), 'action mode'); + channel.addMsg('', '== ' + _kiwi.global.i18n.translate('client_models_network_mode').fetch(event.nick, friendlyModeString()), 'action mode', {time: event.time}); } else { // This is probably a mode being set on us. if (event.target.toLowerCase() === this.get("nick").toLowerCase()) { diff --git a/client/assets/src/views/channel.js b/client/assets/src/views/channel.js index b95dac1..f8d3b86 100644 --- a/client/assets/src/views/channel.js +++ b/client/assets/src/views/channel.js @@ -131,7 +131,7 @@ _kiwi.view.Channel = _kiwi.view.Panel.extend({ if (prev_msg) { // Time difference between this message and the last (in minutes) - time_difference = (msg.date.getTime() - prev_msg.date.getTime())/1000/60; + time_difference = (msg.time.getTime() - prev_msg.time.getTime())/1000/60; if (prev_msg.nick === msg.nick && time_difference < 1) { msg_css_classes += ' repeated_nick'; } @@ -139,7 +139,8 @@ _kiwi.view.Channel = _kiwi.view.Panel.extend({ // Build up and add the line msg.msg_css_classes = msg_css_classes; - line_msg = '
<%- time %>
<%- nick %>
<%= msg %>
'; + msg.time_string = msg.time.getHours().toString().lpad(2, "0") + ":" + msg.time.getMinutes().toString().lpad(2, "0") + ":" + msg.time.getSeconds().toString().lpad(2, "0"); + line_msg = '
<%- time_string %>
<%- nick %>
<%= msg %>
'; this.$messages.append(_.template(line_msg, msg)); // Activity/alerts based on the type of new message -- 2.25.1