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
This commit is contained in:
parent
c38e4f1c81
commit
25cc229019
88
converse.js
88
converse.js
@ -777,7 +777,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)) {
|
||||||
@ -925,10 +925,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();
|
||||||
this.$el.appendTo(converse.chatboxesview.$el);
|
this.$el.appendTo(converse.chatboxesview.$el);
|
||||||
@ -956,40 +956,11 @@
|
|||||||
this.scrollDown();
|
this.scrollDown();
|
||||||
},
|
},
|
||||||
|
|
||||||
renderEmoticons: function (text) {
|
showMessage: function (msg_dict) {
|
||||||
if (converse.show_emoticons) {
|
var $el = this.$el.find('.chat-content'),
|
||||||
text = text.replace(/:\)/g, '<span class="emoticon icon-smiley"></span>');
|
msg_date = msg_dict.time ? converse.parseISO8601(msg_dict.time) : new Date(),
|
||||||
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),
|
|
||||||
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')) {
|
||||||
@ -998,12 +969,12 @@
|
|||||||
username = msg_dict.fullname;
|
username = msg_dict.fullname;
|
||||||
} else {
|
} else {
|
||||||
template = this.message_template;
|
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();
|
$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' || ''
|
||||||
@ -1012,24 +983,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(
|
|
||||||
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) {
|
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;
|
||||||
@ -1048,7 +1001,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
|
||||||
@ -1061,7 +1013,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(this.new_day_template({
|
this.$el.find('.chat-content').append(this.new_day_template({
|
||||||
isodate: isodate,
|
isodate: isodate,
|
||||||
datestring: this_date.toString().substring(0,15)
|
datestring: this_date.toString().substring(0,15)
|
||||||
}));
|
}));
|
||||||
@ -1071,7 +1023,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();
|
||||||
@ -1125,9 +1077,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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2411,7 +2363,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,
|
||||||
@ -2461,12 +2413,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');
|
||||||
},
|
},
|
||||||
@ -2497,7 +2446,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) {
|
||||||
@ -2532,7 +2481,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,
|
||||||
@ -2542,7 +2490,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;
|
||||||
@ -2572,7 +2520,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,9 @@ Changelog
|
|||||||
0.7.3 (Unreleased)
|
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)
|
0.7.3 (2014-02-23)
|
||||||
------------------
|
------------------
|
||||||
|
@ -216,6 +216,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';
|
||||||
@ -232,9 +241,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);
|
||||||
@ -288,7 +296,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];
|
||||||
@ -309,7 +317,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.
|
||||||
@ -414,7 +422,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);
|
||||||
@ -444,7 +452,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));
|
||||||
|
@ -80,7 +80,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
utils.openChatBoxFor = function (jid) {
|
utils.openChatBoxFor = function (jid) {
|
||||||
converse.rosterview.rosteritemviews[jid].openChat(mock.event);
|
return converse.rosterview.rosteritemviews[jid].openChat(mock.event);
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.clearChatBoxMessages = function (jid) {
|
utils.clearChatBoxMessages = function (jid) {
|
||||||
|
Loading…
Reference in New Issue
Block a user