Commit 25cc2290 authored by JC Brand's avatar JC Brand

Refactor message handling

- use the same method for both normal and OTR messages
- fix /me actions for OTR messages
- rename messaging methods to minimize ambiguity

Conflicts:
	converse.js
	tests/utils.js
parent c38e4f1c
......@@ -777,7 +777,7 @@
}
},
messageReceived: function (message) {
receiveMessage: function (message) {
var $body = $(message).children('body');
var text = ($body.length > 0 ? $body.text() : undefined);
if ((!text) || (!converse.allow_otr)) {
......@@ -925,10 +925,10 @@
this.model.on('showHelpMessages', this.showHelpMessages, this);
this.model.on('sendMessageStanza', this.sendMessageStanza, this);
this.model.on('showSentOTRMessage', function (text) {
this.showOTRMessage(text, 'me');
this.showMessage({'message': text, 'sender': 'me'});
}, this);
this.model.on('showReceivedOTRMessage', function (text) {
this.showOTRMessage(text, 'them');
this.showMessage({'message': text, 'sender': 'them'});
}, this);
this.updateVCard();
this.$el.appendTo(converse.chatboxesview.$el);
......@@ -956,40 +956,11 @@
this.scrollDown();
},
renderEmoticons: function (text) {
if (converse.show_emoticons) {
text = text.replace(/:\)/g, '<span class="emoticon icon-smiley"></span>');
text = text.replace(/:\-\)/g, '<span class="emoticon icon-smiley"></span>');
text = text.replace(/;\)/g, '<span class="emoticon icon-wink"></span>');
text = text.replace(/;\-\)/g, '<span class="emoticon icon-wink"></span>');
text = text.replace(/:D/g, '<span class="emoticon icon-grin"></span>');
text = text.replace(/:\-D/g, '<span class="emoticon icon-grin"></span>');
text = text.replace(/:P/g, '<span class="emoticon icon-tongue"></span>');
text = text.replace(/:\-P/g, '<span class="emoticon icon-tongue"></span>');
text = text.replace(/:p/g, '<span class="emoticon icon-tongue"></span>');
text = text.replace(/:\-p/g, '<span class="emoticon icon-tongue"></span>');
text = text.replace(/8\)/g, '<span class="emoticon icon-cool"></span>');
text = text.replace(/>:\)/g, '<span class="emoticon icon-evil"></span>');
text = text.replace(/:S/g, '<span class="emoticon icon-confused"></span>');
text = text.replace(/:\\/g, '<span class="emoticon icon-wondering"></span>');
text = text.replace(/:\//g, '<span class="emoticon icon-wondering"></span>');
text = text.replace(/>:\(/g, '<span class="emoticon icon-angry"></span>');
text = text.replace(/:\(/g, '<span class="emoticon icon-sad"></span>');
text = text.replace(/:\-\(/g, '<span class="emoticon icon-sad"></span>');
text = text.replace(/:O/g, '<span class="emoticon icon-shocked"></span>');
text = text.replace(/:\-O/g, '<span class="emoticon icon-shocked"></span>');
text = text.replace(/\=\-O/g, '<span class="emoticon icon-shocked"></span>');
text = text.replace(/\(\^.\^\)b/g, '<span class="emoticon icon-thumbs-up"></span>');
text = text.replace(/<3/g, '<span class="emoticon icon-heart"></span>');
}
return text;
},
showMessage: function ($el, msg_dict) {
var this_date = converse.parseISO8601(msg_dict.time),
showMessage: function (msg_dict) {
var $el = this.$el.find('.chat-content'),
msg_date = msg_dict.time ? converse.parseISO8601(msg_dict.time) : new Date(),
text = msg_dict.message,
match = text.match(/^\/(.*?)(?: (.*))?$/),
sender = msg_dict.sender,
template, username;
if ((match) && (match[1] === 'me')) {
......@@ -998,12 +969,12 @@
username = msg_dict.fullname;
} else {
template = this.message_template;
username = sender === 'me' && __('me') || msg_dict.fullname;
username = msg_dict.sender === 'me' && __('me') || msg_dict.fullname || this.model.get('fullname');
}
$el.find('div.chat-event').remove();
var message = template({
'sender': sender,
'time': this_date.toTimeString().substring(0,5),
'sender': msg_dict.sender,
'time': msg_date.toTimeString().substring(0,5),
'username': username,
'message': '',
'extra_classes': msg_dict.delayed && 'delayed' || ''
......@@ -1012,24 +983,6 @@
return this.scrollDown();
},
showOTRMessage: function (text, sender) {
/* "Off-the-record" messages are encrypted and not stored at all,
* so we don't have a backbone converse.Message object to work with.
*/
var username = sender === 'me' && sender || this.model.get('fullname');
var $el = this.$el.find('.chat-content');
$el.find('div.chat-event').remove();
$el.append(
this.message_template({
'sender': sender,
'time': (new Date()).toTimeString().substring(0,5),
'message': text,
'username': username,
'extra_classes': ''
}));
return this.scrollDown();
},
showHelpMessages: function (msgs, type, spinner) {
var $chat_content = this.$el.find('.chat-content'), i,
msgs_length = msgs.length;
......@@ -1048,7 +1001,6 @@
var time = message.get('time'),
times = this.model.messages.pluck('time'),
this_date = converse.parseISO8601(time),
$chat_content = this.$el.find('.chat-content'),
previous_message, idx, prev_date, isodate, text, match;
// If this message is on a different day than the one received
......@@ -1061,7 +1013,7 @@
isodate.setUTCHours(0,0,0,0);
isodate = converse.toISOString(isodate);
if (this.isDifferentDay(prev_date, this_date)) {
$chat_content.append(this.new_day_template({
this.$el.find('.chat-content').append(this.new_day_template({
isodate: isodate,
datestring: this_date.toString().substring(0,15)
}));
......@@ -1071,7 +1023,7 @@
this.showStatusNotification(message.get('fullname')+' '+'is typing');
return;
} else {
this.showMessage($chat_content, _.clone(message.attributes));
this.showMessage(_.clone(message.attributes));
}
if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) {
converse.incrementMsgCounter();
......@@ -1125,9 +1077,9 @@
];
this.showHelpMessages(msgs);
return;
} else if ((converse.allow_otr) || (match[1] === "endotr")) {
} else if ((converse.allow_otr) && (match[1] === "endotr")) {
return this.endOTR();
} else if ((converse.allow_otr) || (match[1] === "otr")) {
} else if ((converse.allow_otr) && (match[1] === "otr")) {
return this.model.initiateOTR();
}
}
......@@ -2411,7 +2363,7 @@
}
if (!body) { return true; }
var display_sender = sender === this.model.get('nick') && 'me' || 'room';
this.showMessage($chat_content, {
this.showMessage({
'message': body,
'sender': display_sender,
'fullname': sender,
......@@ -2461,12 +2413,9 @@
model: converse.ChatBox,
registerMessageHandler: function () {
// TODO: Make this method global to converse, trigger an event
// and let messageReceived be called via a handler for that
// event.
converse.connection.addHandler(
$.proxy(function (message) {
this.messageReceived(message);
this.onMessage(message);
return true;
}, this), null, 'message', 'chat');
},
......@@ -2497,7 +2446,7 @@
});
},
messageReceived: function (message) {
onMessage: function (message) {
var buddy_jid, $message = $(message),
message_from = $message.attr('from');
if (message_from == converse.connection.jid) {
......@@ -2532,7 +2481,6 @@
if (!chatbox) {
var fullname = roster_item.get('fullname');
fullname = _.isEmpty(fullname)? buddy_jid: fullname;
chatbox = this.create({
'id': buddy_jid,
'jid': buddy_jid,
......@@ -2542,7 +2490,7 @@
'url': roster_item.get('url')
});
}
chatbox.messageReceived(message);
chatbox.receiveMessage(message);
converse.roster.addResource(buddy_jid, resource);
converse.emit('onMessage', message);
return true;
......@@ -2572,7 +2520,7 @@
view.model = item;
view.initialize();
if (item.get('id') !== 'controlbox') {
// FIXME: Why is it necessary to again append chatboxes?
// XXX: Why is it necessary to again append chatboxes?
view.$el.appendTo(this.$el);
}
}
......
......@@ -4,7 +4,9 @@ Changelog
0.7.3 (Unreleased)
------------------
* #125 Fix crypto dependencies [jcbrand]
* #125 Bugfix: crypto dependencies loaded in wrong order [jcbrand]
* Bugfix: action messages (i.e. /me) didn't work in OTR mode. [jcbrand]
0.7.3 (2014-02-23)
------------------
......
......@@ -216,6 +216,15 @@
}, converse));
describe("A Chat Message", $.proxy(function () {
beforeEach(function () {
runs(function () {
utils.closeAllChatBoxes();
});
waits(250);
runs(function () {});
});
it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
spyOn(converse, 'emit');
var message = 'This is a received message';
......@@ -232,9 +241,8 @@
expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
runs($.proxy(function () {
// messageReceived is a handler for received XMPP
// messages
this.chatboxes.messageReceived(msg);
// onMessage is a handler for received XMPP messages
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
}, converse));
waits(300);
......@@ -288,7 +296,7 @@
}).c('body').t(message).up()
.c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) })
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg);
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
expect(chatbox.messages.length).toEqual(1);
msg_obj = chatbox.messages.models[0];
......@@ -309,7 +317,7 @@
id: new Date().getTime()
}).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg);
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
// Check that there is a <time> element, with the required
// props.
......@@ -414,7 +422,7 @@
id: (new Date()).getTime()
}).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg);
this.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).toHaveBeenCalled();
expect(this.msg_counter).toBe(1);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
......@@ -444,7 +452,7 @@
id: (new Date()).getTime()
}).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg);
this.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
expect(this.msg_counter).toBe(0);
}, converse));
......
......@@ -80,7 +80,7 @@
};
utils.openChatBoxFor = function (jid) {
converse.rosterview.rosteritemviews[jid].openChat(mock.event);
return converse.rosterview.rosteritemviews[jid].openChat(mock.event);
};
utils.clearChatBoxMessages = function (jid) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment