Add support for message actions (/me command).

This commit is contained in:
JC Brand 2012-07-28 23:28:32 +02:00
parent d1efb1c7dc
commit a912068e13

View File

@ -115,31 +115,51 @@ xmppchat.ChatBoxView = Backbone.View.extend({
'<span class="chat-message-content"><%=message%></span>' + '<span class="chat-message-content"><%=message%></span>' +
'</div>'), '</div>'),
action_template: _.template(
'<div class="chat-message <%=extra_classes%>">' +
'<span class="chat-message-<%=sender%>"><%=time%>:&nbsp;</span>' +
'<span class="chat-message-content"><%=message%></span>' +
'</div>'),
appendMessage: function (message) { appendMessage: function (message) {
var time, var time,
now = new Date(), now = new Date(),
minutes = now.getMinutes().toString(), minutes = now.getMinutes().toString(),
list, list,
$chat_content; $chat_content,
match;
message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;"); message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;").replace(/^\s*/, "");
list = message.match(/\b(http:\/\/www\.\S+\.\w+|www\.\S+\.\w+|http:\/\/(?=[^w]){3}\S+[\.:]\S+)[^ ]+\b/g); list = message.match(/\b(http:\/\/www\.\S+\.\w+|www\.\S+\.\w+|http:\/\/(?=[^w]){3}\S+[\.:]\S+)[^ ]+\b/g);
if (list) { if (list) {
for (i = 0; i < list.length; i++) { for (i = 0; i < list.length; i++) {
message = message.replace(list[i], "<a target='_blank' href='" + escape( list[i] ) + "'>"+ list[i] + "</a>" ); message = message.replace(list[i], "<a target='_blank' href='" + escape( list[i] ) + "'>"+ list[i] + "</a>" );
} }
} }
if (minutes.length==1) {minutes = '0'+minutes;} if (minutes.length==1) {minutes = '0'+minutes;}
time = now.toLocaleTimeString().substring(0,5); time = now.toLocaleTimeString().substring(0,5);
$chat_content = $(this.el).find('.chat-content'); $chat_content = $(this.el).find('.chat-content');
$chat_content.find('div.chat-event').remove(); $chat_content.find('div.chat-event').remove();
$chat_content.append(this.message_template({
'sender': 'me', match = message.match(/^\/(.*?)(?: (.*))?$/);
'time': time, if ((match) && (match[1] === 'me')) {
'message': message, message = message.replace(/^\/me/, '*'+xmppchat.username);
'username': 'me', $chat_content.append(this.action_template({
'extra_classes': '' 'sender': 'me',
})); 'time': time,
'message': message,
'username': xmppchat.username,
'extra_classes': ''
}));
} else {
$chat_content.append(this.message_template({
'sender': 'me',
'time': time,
'message': message,
'username': 'me',
'extra_classes': ''
}));
}
$chat_content.scrollTop($chat_content[0].scrollHeight); $chat_content.scrollTop($chat_content[0].scrollHeight);
}, },
@ -175,48 +195,83 @@ xmppchat.ChatBoxView = Backbone.View.extend({
// TODO: ClientStorage // TODO: ClientStorage
xmppchat.messages.ClientStorage.addMessage(jid, body, 'from'); xmppchat.messages.ClientStorage.addMessage(jid, body, 'from');
$chat_content.find('div.chat-event').remove(); $chat_content.find('div.chat-event').remove();
$chat_content.append(
this.message_template({ match = body.match(/^\/(.*?)(?: (.*))?$/);
'sender': 'them', if ((match) && (match[1] === 'me')) {
'time': (new Date()).toLocaleTimeString().substring(0,5), $chat_content.append(this.action_template({
'message': body.replace(/<br \/>/g, ""), 'sender': 'them',
'username': user_id, 'time': (new Date()).toLocaleTimeString().substring(0,5),
'extra_classes': ($(message).find('delay').length > 0) && 'delayed' || '' 'message': body.replace(/^\/me/, '*'+user_id).replace(/<br \/>/g, ""),
})); 'username': xmppchat.username,
'extra_classes': ($(message).find('delay').length > 0) && 'delayed' || ''
}));
} else {
$chat_content.append(
this.message_template({
'sender': 'them',
'time': (new Date()).toLocaleTimeString().substring(0,5),
'message': body.replace(/<br \/>/g, ""),
'username': user_id,
'extra_classes': ($(message).find('delay').length > 0) && 'delayed' || ''
}));
}
$chat_content.scrollTop($chat_content[0].scrollHeight); $chat_content.scrollTop($chat_content[0].scrollHeight);
} }
}, },
insertClientStoredMessages: function () { insertClientStoredMessages: function () {
var that = this; xmppchat.messages.getMessages(this.model.get('jid'), $.proxy(function (msgs) {
xmppchat.messages.getMessages(this.model.get('jid'), function (msgs) { var $content = this.$el.find('.chat-content');
var $content = that.$el.find('.chat-content');
for (var i=0; i<_.size(msgs); i++) { for (var i=0; i<_.size(msgs); i++) {
var msg = msgs[i], var msg = msgs[i],
msg_array = msg.split(' ', 2), msg_array = msg.split(' ', 2),
date = msg_array[0]; date = msg_array[0],
match;
msg = String(msg).replace(/(.*?\s.*?\s)/, '');
match = msg.match(/^\/(.*?)(?: (.*))?$/);
if (msg_array[1] == 'to') { if (msg_array[1] == 'to') {
$content.append( if ((match) && (match[1] === 'me')) {
that.message_template({ $content.append(
this.action_template({
'sender': 'me', 'sender': 'me',
'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5), 'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5),
'message': String(msg).replace(/(.*?\s.*?\s)/, ''), 'message': msg.replace(/^\/me/, '*'+xmppchat.username),
'username': xmppchat.username,
'extra_classes': 'delayed'
}));
} else {
$content.append(
this.message_template({
'sender': 'me',
'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5),
'message': msg,
'username': 'me', 'username': 'me',
'extra_classes': 'delayed' 'extra_classes': 'delayed'
})); }));
}
} else { } else {
$content.append( if ((match) && (match[1] === 'me')) {
that.message_template({ $content.append(
this.action_template({
'sender': 'them', 'sender': 'them',
'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5), 'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5),
'message': String(msg).replace(/(.*?\s.*?\s)/, ''), 'message': msg.replace(/^\/me/, '*'+this.model.get('user_id')),
'username': that.model.get('user_id'), 'username': this.model.get('user_id'),
'extra_classes': 'delayed' 'extra_classes': 'delayed'
})); }));
} else {
$content.append(
this.message_template({
'sender': 'them',
'time': new Date(Date.parse(date)).toLocaleTimeString().substring(0,5),
'message': msg,
'username': this.model.get('user_id'),
'extra_classes': 'delayed'
}));
}
} }
} }
}); }, this));
}, },
sendMessage: function (text) { sendMessage: function (text) {
@ -226,6 +281,7 @@ xmppchat.ChatBoxView = Backbone.View.extend({
// TODO: Look in ChatPartners to see what resources we have for the recipient. // TODO: Look in ChatPartners to see what resources we have for the recipient.
// if we have one resource, we sent to only that resources, if we have multiple // if we have one resource, we sent to only that resources, if we have multiple
// we send to the bare jid. // we send to the bare jid.
var bare_jid = this.model.get('jid'); var bare_jid = this.model.get('jid');
var message = $msg({to: bare_jid, type: 'chat'}) var message = $msg({to: bare_jid, type: 'chat'})
.c('body').t(text).up() .c('body').t(text).up()
@ -576,6 +632,8 @@ xmppchat.ChatRoomView = xmppchat.ChatBoxView.extend({
} else if (match[1] === "deop") { } else if (match[1] === "deop") {
xmppchat.connection.muc.deop(this.model.get('jid'), match[2]); xmppchat.connection.muc.deop(this.model.get('jid'), match[2]);
} else {
this.last_msgid = xmppchat.connection.muc.groupchat(this.model.get('jid'), body);
} }
} else { } else {
this.last_msgid = xmppchat.connection.muc.groupchat(this.model.get('jid'), body); this.last_msgid = xmppchat.connection.muc.groupchat(this.model.get('jid'), body);