Add /me messages to chatboxes (they're already in chatrooms)

This commit is contained in:
JC Brand 2013-05-30 18:06:40 +02:00
parent 74a224b34d
commit 7d1a7fd6b4

View File

@ -249,7 +249,6 @@
time = converse.toISOString(new Date()); time = converse.toISOString(new Date());
} }
if (from == converse.bare_jid) { if (from == converse.bare_jid) {
fullname = 'me';
sender = 'me'; sender = 'me';
} else { } else {
sender = 'them'; sender = 'them';
@ -283,7 +282,7 @@
action_template: _.template( action_template: _.template(
'<div class="chat-message {{extra_classes}}">' + '<div class="chat-message {{extra_classes}}">' +
'<span class="chat-message-{{sender}}">{{time}}:&nbsp;</span>' + '<span class="chat-message-{{sender}}">{{time}} **{{username}} </span>' +
'<span class="chat-message-content">{{message}}</span>' + '<span class="chat-message-content">{{message}}</span>' +
'</div>'), '</div>'),
@ -291,6 +290,31 @@
'<time class="chat-date" datetime="{{isodate}}">{{datestring}}</time>' '<time class="chat-date" datetime="{{isodate}}">{{datestring}}</time>'
), ),
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) { insertStatusNotification: function (message, replace) {
var $chat_content = this.$el.find('.chat-content'); var $chat_content = this.$el.find('.chat-content');
$chat_content.find('div.chat-event').remove().end() $chat_content.find('div.chat-event').remove().end()
@ -303,7 +327,7 @@
times = this.model.messages.pluck('time'), times = this.model.messages.pluck('time'),
this_date = converse.parseISO8601(time), this_date = converse.parseISO8601(time),
$chat_content = this.$el.find('.chat-content'), $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 // If this message is on a different day than the one received
// prior, then indicate it on the chatbox. // prior, then indicate it on the chatbox.
@ -325,15 +349,7 @@
this.insertStatusNotification(message.get('fullname')+' '+'is typing'); this.insertStatusNotification(message.get('fullname')+' '+'is typing');
return; return;
} else { } else {
$chat_content.find('div.chat-event').remove(); this.appendMessage($chat_content, _.clone(message.attributes));
$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' || ''
}));
} }
if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) { if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) {
converse.incrementMsgCounter(); converse.incrementMsgCounter();
@ -375,6 +391,7 @@
else if (match[1] === "help") { else if (match[1] === "help") {
msgs = [ msgs = [
'<strong>/help</strong>: Show this menu', '<strong>/help</strong>: Show this menu',
'<strong>/me</strong>: Refer to yourself in the third person',
'<strong>/clear</strong>: Remove messages' '<strong>/clear</strong>: Remove messages'
]; ];
this.addHelpMessages(msgs); this.addHelpMessages(msgs);
@ -395,7 +412,7 @@
converse.connection.send(forwarded); converse.connection.send(forwarded);
// Add the new message // Add the new message
this.model.messages.create({ this.model.messages.create({
fullname: 'me', fullname: converse.xmppstatus.get('fullname')||converse.bare_jid,
sender: 'me', sender: 'me',
time: converse.toISOString(new Date()), time: converse.toISOString(new Date()),
message: text message: text
@ -1414,25 +1431,12 @@
$chat_content.append(this.info_template({'message': 'Topic set by '+sender+' to: '+subject })); $chat_content.append(this.info_template({'message': 'Topic set by '+sender+' to: '+subject }));
} }
if (!body) { return true; } if (!body) { return true; }
match = body.match(/^\/(.*?)(?: (.*))?$/); this.appendMessage($chat_content,
if ((match) && (match[1] === 'me')) { {'message': body,
body = body.replace(/^\/me/, '*'+sender); 'sender': sender === this.model.get('nick') && 'me' || 'room',
template = this.action_template; 'fullname': sender,
} else { 'time': converse.toISOString(message_datetime)
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.scrollDown(); this.scrollDown();
return true; return true;
}, },
@ -1499,8 +1503,8 @@
}, },
messageReceived: function (message) { messageReceived: function (message) {
var partner_jid, $message = $(message), var partner_jid, $message = $(message),
message_from = $message.attr('from'); message_from = $message.attr('from');
if (message_from == converse.connection.jid) { if (message_from == converse.connection.jid) {
// FIXME: Forwarded messages should be sent to specific resources, // FIXME: Forwarded messages should be sent to specific resources,
// not broadcasted // not broadcasted