Commit 317ab95c 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
parent 2094ca12
...@@ -829,7 +829,7 @@ ...@@ -829,7 +829,7 @@
} }
}, },
messageReceived: function (message) { receiveMessage: function (message) {
var $body = $(message).children('body'); var $body = $(message).children('body');
var text = ($body.length > 0 ? $body.text() : undefined); var text = ($body.length > 0 ? $body.text() : undefined);
if ((!text) || (!converse.allow_otr)) { if ((!text) || (!converse.allow_otr)) {
...@@ -886,10 +886,10 @@ ...@@ -886,10 +886,10 @@
this.model.on('showHelpMessages', this.showHelpMessages, this); this.model.on('showHelpMessages', this.showHelpMessages, this);
this.model.on('sendMessageStanza', this.sendMessageStanza, this); this.model.on('sendMessageStanza', this.sendMessageStanza, this);
this.model.on('showSentOTRMessage', function (text) { this.model.on('showSentOTRMessage', function (text) {
this.showOTRMessage(text, 'me'); this.showMessage({'message': text, 'sender': 'me'});
}, this); }, this);
this.model.on('showReceivedOTRMessage', function (text) { this.model.on('showReceivedOTRMessage', function (text) {
this.showOTRMessage(text, 'them'); this.showMessage({'message': text, 'sender': 'them'});
}, this); }, this);
this.updateVCard(); this.updateVCard();
...@@ -937,11 +937,11 @@ ...@@ -937,11 +937,11 @@
this.scrollDown(); this.scrollDown();
}, },
showMessage: function ($el, msg_dict) { showMessage: function (msg_dict) {
var this_date = converse.parseISO8601(msg_dict.time), var $el = this.$el.find('.chat-content'),
msg_date = msg_dict.time ? converse.parseISO8601(msg_dict.time) : new Date(),
text = msg_dict.message, text = msg_dict.message,
match = text.match(/^\/(.*?)(?: (.*))?$/), match = text.match(/^\/(.*?)(?: (.*))?$/),
sender = msg_dict.sender,
template, username; template, username;
if ((match) && (match[1] === 'me')) { if ((match) && (match[1] === 'me')) {
...@@ -950,12 +950,12 @@ ...@@ -950,12 +950,12 @@
username = msg_dict.fullname; username = msg_dict.fullname;
} else { } else {
template = converse.templates.message; template = converse.templates.message;
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(); $el.find('div.chat-event').remove();
var message = template({ var message = template({
'sender': sender, 'sender': msg_dict.sender,
'time': this_date.toTimeString().substring(0,5), 'time': msg_date.toTimeString().substring(0,5),
'username': username, 'username': username,
'message': '', 'message': '',
'extra_classes': msg_dict.delayed && 'delayed' || '' 'extra_classes': msg_dict.delayed && 'delayed' || ''
...@@ -964,24 +964,6 @@ ...@@ -964,24 +964,6 @@
return this.scrollDown(); 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(
converse.templates.message({
'sender': sender,
'time': (new Date()).toTimeString().substring(0,5),
'message': text,
'username': username,
'extra_classes': ''
}));
return this.scrollDown();
},
showHelpMessages: function (msgs, type, spinner) { showHelpMessages: function (msgs, type, spinner) {
var $chat_content = this.$el.find('.chat-content'), i, var $chat_content = this.$el.find('.chat-content'), i,
msgs_length = msgs.length; msgs_length = msgs.length;
...@@ -1000,7 +982,6 @@ ...@@ -1000,7 +982,6 @@
var time = message.get('time'), var time = message.get('time'),
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'),
previous_message, idx, prev_date, isodate, text, match; 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
...@@ -1013,7 +994,7 @@ ...@@ -1013,7 +994,7 @@
isodate.setUTCHours(0,0,0,0); isodate.setUTCHours(0,0,0,0);
isodate = converse.toISOString(isodate); isodate = converse.toISOString(isodate);
if (this.isDifferentDay(prev_date, this_date)) { if (this.isDifferentDay(prev_date, this_date)) {
$chat_content.append(converse.templates.new_day({ this.$el.find('.chat-content').append(converse.templates.new_day({
isodate: isodate, isodate: isodate,
datestring: this_date.toString().substring(0,15) datestring: this_date.toString().substring(0,15)
})); }));
...@@ -1023,7 +1004,7 @@ ...@@ -1023,7 +1004,7 @@
this.showStatusNotification(message.get('fullname')+' '+'is typing'); this.showStatusNotification(message.get('fullname')+' '+'is typing');
return; return;
} else { } else {
this.showMessage($chat_content, _.clone(message.attributes)); this.showMessage(_.clone(message.attributes));
} }
if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) { if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) {
converse.incrementMsgCounter(); converse.incrementMsgCounter();
...@@ -1077,9 +1058,9 @@ ...@@ -1077,9 +1058,9 @@
]; ];
this.showHelpMessages(msgs); this.showHelpMessages(msgs);
return; return;
} else if ((converse.allow_otr) || (match[1] === "endotr")) { } else if ((converse.allow_otr) && (match[1] === "endotr")) {
return this.endOTR(); return this.endOTR();
} else if ((converse.allow_otr) || (match[1] === "otr")) { } else if ((converse.allow_otr) && (match[1] === "otr")) {
return this.model.initiateOTR(); return this.model.initiateOTR();
} }
} }
...@@ -2337,7 +2318,7 @@ ...@@ -2337,7 +2318,7 @@
} }
if (!body) { return true; } if (!body) { return true; }
var display_sender = sender === this.model.get('nick') && 'me' || 'room'; var display_sender = sender === this.model.get('nick') && 'me' || 'room';
this.showMessage($chat_content, { this.showMessage({
'message': body, 'message': body,
'sender': display_sender, 'sender': display_sender,
'fullname': sender, 'fullname': sender,
...@@ -2376,12 +2357,9 @@ ...@@ -2376,12 +2357,9 @@
model: converse.ChatBox, model: converse.ChatBox,
registerMessageHandler: function () { 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( converse.connection.addHandler(
$.proxy(function (message) { $.proxy(function (message) {
this.messageReceived(message); this.onMessage(message);
return true; return true;
}, this), null, 'message', 'chat'); }, this), null, 'message', 'chat');
}, },
...@@ -2411,7 +2389,7 @@ ...@@ -2411,7 +2389,7 @@
}); });
}, },
messageReceived: function (message) { onMessage: function (message) {
var buddy_jid, $message = $(message), var buddy_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) {
...@@ -2446,7 +2424,6 @@ ...@@ -2446,7 +2424,6 @@
if (!chatbox) { if (!chatbox) {
var fullname = roster_item.get('fullname'); var fullname = roster_item.get('fullname');
fullname = _.isEmpty(fullname)? buddy_jid: fullname; fullname = _.isEmpty(fullname)? buddy_jid: fullname;
chatbox = this.create({ chatbox = this.create({
'id': buddy_jid, 'id': buddy_jid,
'jid': buddy_jid, 'jid': buddy_jid,
...@@ -2456,7 +2433,7 @@ ...@@ -2456,7 +2433,7 @@
'url': roster_item.get('url') 'url': roster_item.get('url')
}); });
} }
chatbox.messageReceived(message); chatbox.receiveMessage(message);
converse.roster.addResource(buddy_jid, resource); converse.roster.addResource(buddy_jid, resource);
converse.emit('onMessage', message); converse.emit('onMessage', message);
return true; return true;
...@@ -2488,7 +2465,7 @@ ...@@ -2488,7 +2465,7 @@
view.model = item; view.model = item;
view.initialize(); view.initialize();
if (item.get('id') !== 'controlbox') { 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); view.$el.appendTo(this.$el);
} }
} }
......
...@@ -287,6 +287,15 @@ ...@@ -287,6 +287,15 @@
}, converse)); }, converse));
describe("A Chat Message", $.proxy(function () { 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 () { it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var message = 'This is a received message'; var message = 'This is a received message';
...@@ -303,9 +312,8 @@ ...@@ -303,9 +312,8 @@
expect(this.chatboxes.get(sender_jid)).not.toBeDefined(); expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
runs($.proxy(function () { runs($.proxy(function () {
// messageReceived is a handler for received XMPP // onMessage is a handler for received XMPP messages
// messages this.chatboxes.onMessage(msg);
this.chatboxes.messageReceived(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
}, converse)); }, converse));
waits(300); waits(300);
...@@ -359,7 +367,7 @@ ...@@ -359,7 +367,7 @@
}).c('body').t(message).up() }).c('body').t(message).up()
.c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) }) .c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) })
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg); this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
expect(chatbox.messages.length).toEqual(1); expect(chatbox.messages.length).toEqual(1);
msg_obj = chatbox.messages.models[0]; msg_obj = chatbox.messages.models[0];
...@@ -380,7 +388,7 @@ ...@@ -380,7 +388,7 @@
id: new Date().getTime() id: new Date().getTime()
}).c('body').t(message).up() }).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg); this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
// Check that there is a <time> element, with the required // Check that there is a <time> element, with the required
// props. // props.
...@@ -489,7 +497,7 @@ ...@@ -489,7 +497,7 @@
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').t(message).up() }).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg); this.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).toHaveBeenCalled(); expect(converse.incrementMsgCounter).toHaveBeenCalled();
expect(this.msg_counter).toBe(1); expect(this.msg_counter).toBe(1);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
...@@ -519,7 +527,7 @@ ...@@ -519,7 +527,7 @@
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').t(message).up() }).c('body').t(message).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.messageReceived(msg); this.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).not.toHaveBeenCalled(); expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
expect(this.msg_counter).toBe(0); expect(this.msg_counter).toBe(0);
}, converse)); }, converse));
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
}; };
utils.openChatBoxFor = function (jid) { utils.openChatBoxFor = function (jid) {
converse.rosterview.get(jid).openChat(mock.event); return converse.rosterview.get(jid).openChat(mock.event);
}; };
utils.clearChatBoxMessages = function (jid) { 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