From 7d1a7fd6b413ee78fb3a9a50c602c9daa43b5f5d Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 30 May 2013 18:06:40 +0200 Subject: [PATCH] Add /me messages to chatboxes (they're already in chatrooms) --- converse.js | 72 ++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/converse.js b/converse.js index 23336dd9d..add263832 100644 --- a/converse.js +++ b/converse.js @@ -249,7 +249,6 @@ time = converse.toISOString(new Date()); } if (from == converse.bare_jid) { - fullname = 'me'; sender = 'me'; } else { sender = 'them'; @@ -283,7 +282,7 @@ action_template: _.template( '
' + - '{{time}}: ' + + '{{time}} **{{username}} ' + '{{message}}' + '
'), @@ -291,6 +290,31 @@ '' ), + appendMessage: function ($el, msg_dict) { + var this_date = converse.parseISO8601(msg_dict.time), + text = msg_dict.message, + match = text.match(/^\/(.*?)(?: (.*))?$/), + sender = msg_dict.sender, + template, username; + if ((match) && (match[1] === 'me')) { + text = text.replace(/^\/me/, ''); + template = this.action_template; + username = msg_dict.fullname; + } else { + template = this.message_template; + username = sender === 'me' && sender || msg_dict.fullname; + } + $el.find('div.chat-event').remove(); + $el.append( + template({ + 'sender': sender, + 'time': this_date.toLocaleTimeString().substring(0,4), + 'message': text, + 'username': username, + 'extra_classes': msg_dict.delayed && 'delayed' || '' + })); + }, + insertStatusNotification: function (message, replace) { var $chat_content = this.$el.find('.chat-content'); $chat_content.find('div.chat-event').remove().end() @@ -303,7 +327,7 @@ times = this.model.messages.pluck('time'), this_date = converse.parseISO8601(time), $chat_content = this.$el.find('.chat-content'), - previous_message, idx, prev_date, isodate; + previous_message, idx, prev_date, isodate, text, match; // If this message is on a different day than the one received // prior, then indicate it on the chatbox. @@ -325,15 +349,7 @@ this.insertStatusNotification(message.get('fullname')+' '+'is typing'); return; } else { - $chat_content.find('div.chat-event').remove(); - $chat_content.append( - this.message_template({ - 'sender': message.get('sender'), - 'time': this_date.toLocaleTimeString().substring(0,5), - 'message': message.get('message'), - 'username': message.get('fullname'), - 'extra_classes': message.get('delayed') && 'delayed' || '' - })); + this.appendMessage($chat_content, _.clone(message.attributes)); } if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) { converse.incrementMsgCounter(); @@ -375,6 +391,7 @@ else if (match[1] === "help") { msgs = [ '/help: Show this menu', + '/me: Refer to yourself in the third person', '/clear: Remove messages' ]; this.addHelpMessages(msgs); @@ -395,7 +412,7 @@ converse.connection.send(forwarded); // Add the new message this.model.messages.create({ - fullname: 'me', + fullname: converse.xmppstatus.get('fullname')||converse.bare_jid, sender: 'me', time: converse.toISOString(new Date()), message: text @@ -1414,25 +1431,12 @@ $chat_content.append(this.info_template({'message': 'Topic set by '+sender+' to: '+subject })); } if (!body) { return true; } - match = body.match(/^\/(.*?)(?: (.*))?$/); - if ((match) && (match[1] === 'me')) { - body = body.replace(/^\/me/, '*'+sender); - template = this.action_template; - } else { - template = this.message_template; - } - if (sender === this.model.get('nick')) { - sender = 'me'; - } - $chat_content.append( - template({ - 'sender': sender == 'me' && sender || 'room', - 'time': message_datetime.toLocaleTimeString().substring(0,5), - 'message': body, - 'username': sender, - 'extra_classes': delayed && 'delayed' || '' - }) - ); + this.appendMessage($chat_content, + {'message': body, + 'sender': sender === this.model.get('nick') && 'me' || 'room', + 'fullname': sender, + 'time': converse.toISOString(message_datetime) + }); this.scrollDown(); return true; }, @@ -1499,8 +1503,8 @@ }, messageReceived: function (message) { - var partner_jid, $message = $(message), - message_from = $message.attr('from'); + var partner_jid, $message = $(message), + message_from = $message.attr('from'); if (message_from == converse.connection.jid) { // FIXME: Forwarded messages should be sent to specific resources, // not broadcasted