Commit 607c2a81 authored by JC Brand's avatar JC Brand

Instead of GONE, set chat_state to INACTIVE when a box is closed.

Also, fixed all broken tests.
parent b4d53aaa
...@@ -209,10 +209,8 @@ ...@@ -209,10 +209,8 @@
var GONE = 'gone'; var GONE = 'gone';
this.TIMEOUTS = { // Set as module attr so that we can override in tests. this.TIMEOUTS = { // Set as module attr so that we can override in tests.
'PAUSED': 20000, 'PAUSED': 20000,
'INACTIVE': 90000, 'INACTIVE': 90000
'GONE': 510000
}; };
var HAS_CSPRNG = ((typeof crypto !== 'undefined') && var HAS_CSPRNG = ((typeof crypto !== 'undefined') &&
((typeof crypto.randomBytes === 'function') || ((typeof crypto.randomBytes === 'function') ||
(typeof crypto.getRandomValues === 'function') (typeof crypto.getRandomValues === 'function')
...@@ -886,56 +884,48 @@ ...@@ -886,56 +884,48 @@
createMessage: function ($message) { createMessage: function ($message) {
var body = $message.children('body').text(), var body = $message.children('body').text(),
composing = $message.find(COMPOSING),
paused = $message.find(PAUSED),
delayed = $message.find('delay').length > 0, delayed = $message.find('delay').length > 0,
fullname = this.get('fullname'), fullname = this.get('fullname'),
is_groupchat = $message.attr('type') === 'groupchat', is_groupchat = $message.attr('type') === 'groupchat',
msgid = $message.attr('id'), msgid = $message.attr('id'),
stamp, time, sender, from; chat_state = $message.find(COMPOSING).length && COMPOSING ||
$message.find(PAUSED).length && PAUSED ||
$message.find(INACTIVE).length && INACTIVE ||
$message.find(ACTIVE).length && ACTIVE ||
$message.find(GONE).length && GONE,
stamp, time, sender, from, createMessage;
if (is_groupchat) { if (is_groupchat) {
from = Strophe.unescapeNode(Strophe.getResourceFromJid($message.attr('from'))); from = Strophe.unescapeNode(Strophe.getResourceFromJid($message.attr('from')));
} else { } else {
from = Strophe.getBareJidFromJid($message.attr('from')); from = Strophe.getBareJidFromJid($message.attr('from'));
} }
fullname = (_.isEmpty(fullname)? from: fullname).split(' ')[0]; fullname = (_.isEmpty(fullname) ? from: fullname).split(' ')[0];
if (delayed) {
if (!body) { stamp = $message.find('delay').attr('stamp');
if (composing.length || paused.length) { time = stamp;
// FIXME: use one attribute for chat states (e.g.
// chatstate) instead of saving 'paused' and
// 'composing' separately.
this.messages.add({
fullname: fullname,
sender: 'them',
delayed: delayed,
time: moment().format(),
composing: composing.length,
paused: paused.length
});
}
} else { } else {
if (delayed) { time = moment().format();
stamp = $message.find('delay').attr('stamp'); }
time = stamp; if ((is_groupchat && from === this.get('nick')) || (!is_groupchat && from == converse.bare_jid)) {
} else { sender = 'me';
time = moment().format(); } else {
} sender = 'them';
if ((is_groupchat && from === this.get('nick')) || (!is_groupchat && from == converse.bare_jid)) {
sender = 'me';
} else {
sender = 'them';
}
this.messages.create({
fullname: fullname,
sender: sender,
delayed: delayed,
time: time,
message: body,
msgid: msgid
});
} }
if (!body) {
createMessage = this.messages.add;
} else {
createMessage = this.messages.create;
}
this.messages.create({
chat_state: chat_state,
delayed: delayed,
fullname: fullname,
message: body || undefined,
msgid: msgid,
sender: sender,
time: time
});
}, },
receiveMessage: function ($message) { receiveMessage: function ($message) {
...@@ -1134,12 +1124,20 @@ ...@@ -1134,12 +1124,20 @@
})); }));
} }
} }
if (message.get(COMPOSING)) { if (!message.get('message')) {
this.showStatusNotification(message.get('fullname')+' '+__('is typing')); if (message.get('chat_state') === COMPOSING) {
return; this.showStatusNotification(message.get('fullname')+' '+__('is typing'));
} else if (message.get(PAUSED)) { return;
this.showStatusNotification(message.get('fullname')+' '+__('has stopped typing')); } else if (message.get('chat_state') === PAUSED) {
return; this.showStatusNotification(message.get('fullname')+' '+__('has stopped typing'));
return;
} else if (_.contains([INACTIVE, ACTIVE], message.get('chat_state'))) {
this.$el.find('.chat-content div.chat-event').remove();
return;
} else if (message.get('chat_state') === GONE) {
this.showStatusNotification(message.get('fullname')+' '+__('has gone away'));
return;
}
} else { } else {
this.showMessage(_.clone(message.attributes)); this.showMessage(_.clone(message.attributes));
} }
...@@ -1413,11 +1411,11 @@ ...@@ -1413,11 +1411,11 @@
fullname = _.isEmpty(fullname)? item.get('jid'): fullname; fullname = _.isEmpty(fullname)? item.get('jid'): fullname;
if (this.$el.is(':visible')) { if (this.$el.is(':visible')) {
if (chat_status === 'offline') { if (chat_status === 'offline') {
this.showStatusNotification(fullname+' '+'has gone offline'); this.showStatusNotification(fullname+' '+__('has gone offline'));
} else if (chat_status === 'away') { } else if (chat_status === 'away') {
this.showStatusNotification(fullname+' '+'has gone away'); this.showStatusNotification(fullname+' '+__('has gone away'));
} else if ((chat_status === 'dnd')) { } else if ((chat_status === 'dnd')) {
this.showStatusNotification(fullname+' '+'is busy'); this.showStatusNotification(fullname+' '+__('is busy'));
} else if (chat_status === 'online') { } else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove(); this.$el.find('div.chat-event').remove();
} }
...@@ -1461,7 +1459,7 @@ ...@@ -1461,7 +1459,7 @@
} else { } else {
this.model.trigger('hide'); this.model.trigger('hide');
} }
this.setChatState(GONE); this.setChatState(INACTIVE);
converse.emit('chatBoxClosed', this); converse.emit('chatBoxClosed', this);
return this; return this;
}, },
...@@ -3096,7 +3094,7 @@ ...@@ -3096,7 +3094,7 @@
initialize: function () { initialize: function () {
this.model.messages.on('add', function (m) { this.model.messages.on('add', function (m) {
if (!(m.get(COMPOSING) || m.get(PAUSED))) { if (m.get('message')) {
this.updateUnreadMessagesCounter(); this.updateUnreadMessagesCounter();
} }
}, this); }, this);
......
...@@ -868,29 +868,6 @@ ...@@ -868,29 +868,6 @@
expect($stanza.children().prop('tagName')).toBe('inactive'); expect($stanza.children().prop('tagName')).toBe('inactive');
}, converse)); }, converse));
it("will be shown if received", $.proxy(function () {
// TODO: only show paused state if the previous state was composing
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
// <paused> state
msg = $msg({
from: sender_jid,
to: this.connection.jid,
type: 'chat',
id: (new Date()).getTime()
}).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg);
var chatboxview = this.chatboxviews.get(sender_jid);
$events = chatboxview.$el.find('.chat-event');
expect($events.length).toBe(1);
expect($events.text()).toEqual(mock.cur_names[1].split(' ')[0] + ' has stopped typing');
}, converse));
}, converse));
describe("An gone notifciation", $.proxy(function () {
it("is sent if the user closes a chat box", $.proxy(function () { it("is sent if the user closes a chat box", $.proxy(function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(contact_jid);
...@@ -898,17 +875,38 @@ ...@@ -898,17 +875,38 @@
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
view.close(); view.close();
expect(view.model.get('chat_state')).toBe('gone'); expect(view.model.get('chat_state')).toBe('inactive');
expect(converse.connection.send).toHaveBeenCalled(); expect(converse.connection.send).toHaveBeenCalled();
var $stanza = $(converse.connection.send.argsForCall[0][0].tree()); var $stanza = $(converse.connection.send.argsForCall[0][0].tree());
expect($stanza.attr('to')).toBe(contact_jid); expect($stanza.attr('to')).toBe(contact_jid);
expect($stanza.children().length).toBe(1); expect($stanza.children().length).toBe(1);
expect($stanza.children().prop('tagName')).toBe('gone'); expect($stanza.children().prop('tagName')).toBe('inactive');
}, converse)); }, converse));
xit("will be shown if received", $.proxy(function () { it("will clear any other chat status notifications if its received", $.proxy(function () {
// TODO: only show paused state if the previous state was composing
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(sender_jid);
var view = this.chatboxviews.get(sender_jid);
expect(view.$el.find('.chat-event').length).toBe(0);
view.showStatusNotification(sender_jid+' '+'is typing');
expect(view.$el.find('.chat-event').length).toBe(1);
msg = $msg({
from: sender_jid,
to: this.connection.jid,
type: 'chat',
id: (new Date()).getTime()
}).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg);
expect(view.$el.find('.chat-event').length).toBe(0);
}, converse));
}, converse));
describe("A gone notifciation", $.proxy(function () {
it("will be shown if received", $.proxy(function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
// <paused> state // <paused> state
...@@ -923,7 +921,7 @@ ...@@ -923,7 +921,7 @@
var chatboxview = this.chatboxviews.get(sender_jid); var chatboxview = this.chatboxviews.get(sender_jid);
$events = chatboxview.$el.find('.chat-event'); $events = chatboxview.$el.find('.chat-event');
expect($events.length).toBe(1); expect($events.length).toBe(1);
expect($events.text()).toEqual(mock.cur_names[1].split(' ')[0] + ' has stopped typing'); expect($events.text()).toEqual(mock.cur_names[1].split(' ')[0] + ' has gone away');
}, converse)); }, converse));
}, converse)); }, converse));
}, converse)); }, converse));
......
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