Commit 5ec7c67b authored by JC Brand's avatar JC Brand

Don't automatically show a chat box when creating it. updates #357

This caused a bug whereby a chat box would open only on chat state
notifications.

Also refactored the chats.open and chats.get methods so that they now reuse the
same map function and so that chats.get can now return any chat box and not
just already open ones.

Updated the tests to properly test this and updated the docs.
parent a902ba2d
......@@ -1016,12 +1016,7 @@
this.updateVCard();
this.$el.insertAfter(converse.chatboxviews.get("controlbox").$el);
this.render().model.messages.fetch({add: true});
if (this.model.get('minimized')) {
this.hide();
} else {
this.show();
}
this.hide().render().model.messages.fetch({add: true});
if ((_.contains([UNVERIFIED, VERIFIED], this.model.get('otr_status'))) || converse.use_otr_by_default) {
this.model.initiateOTR();
}
......@@ -1159,7 +1154,10 @@
if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) {
converse.incrementMsgCounter();
}
return this.scrollDown();
this.scrollDown();
if (!this.model.get('minimized') && !this.$el.is(':visible')) {
this.show();
}
},
sendMessageStanza: function (text) {
......@@ -1617,7 +1615,7 @@
this.initDragResize();
}
this.setChatState(ACTIVE);
return this;
return this.focus();
},
scrollDown: function () {
......@@ -3075,6 +3073,8 @@
},
onMessage: function (message) {
/* Handler method for all incoming single-user chat "message" stanzas.
*/
var $message = $(message);
var contact_jid, $forwarded, $received, $sent,
msgid = $message.attr('id'),
......@@ -3280,7 +3280,6 @@
showChat: function (attrs) {
/* Find the chat box and show it. If it doesn't exist, create it.
*/
// TODO: Send the chat state ACTIVE to the contact once the chat box is opened.
var chatbox = this.model.get(attrs.jid);
if (!chatbox) {
chatbox = this.model.create(attrs, {
......@@ -5432,6 +5431,7 @@
var wrappedChatBox = function (chatbox) {
var view = converse.chatboxviews.get(chatbox.get('jid'));
return {
'open': $.proxy(view.show, view),
'close': $.proxy(view.close, view),
'endOTR': $.proxy(chatbox.endOTR, chatbox),
'focus': $.proxy(view.focus, view),
......@@ -5442,6 +5442,27 @@
'set': $.proxy(chatbox.set, chatbox)
};
};
var getWrappedChatBox = function (jid) {
var chatbox = converse.chatboxes.get(jid);
if (!chatbox) {
var roster_item = converse.roster.get(jid);
if (roster_item === undefined) {
converse.log('Could not get roster item for JID '+jid, 'error');
return null;
}
chatbox = converse.chatboxes.create({
'id': jid,
'jid': jid,
'fullname': _.isEmpty(roster_item.get('fullname'))? jid: roster_item.get('fullname'),
'image_type': roster_item.get('image_type'),
'image': roster_item.get('image'),
'url': roster_item.get('url')
});
}
return wrappedChatBox(chatbox);
};
return {
'initialize': function (settings, callback) {
converse.initialize(settings, callback);
......@@ -5498,47 +5519,29 @@
},
'chats': {
'open': function (jids) {
var _transform = function (jid) {
var chatbox = converse.chatboxes.get(jid);
if (!chatbox) {
var roster_item = converse.roster.get(jid);
if (roster_item === undefined) {
converse.log('Could not get roster item for JID '+jid, 'error');
return null;
}
chatbox = converse.chatboxes.create({
'id': jid,
'jid': jid,
'fullname': _.isEmpty(roster_item.get('fullname'))? jid: roster_item.get('fullname'),
'image_type': roster_item.get('image_type'),
'image': roster_item.get('image'),
'url': roster_item.get('url')
});
}
return wrappedChatBox(chatbox);
};
var chatbox;
if (typeof jids === "undefined") {
converse.log("chats.open: You need to provide at least one JID", "error");
return null;
} else if (typeof jids === "string") {
return _transform(jids);
}
return _.map(jids, _transform);
chatbox = getWrappedChatBox(jids);
chatbox.open();
return chatbox;
}
return _.map(jids, function (jid) {
var chatbox = getWrappedChatBox(jid);
chatbox.open();
return chatbox;
});
},
'get': function (jids) {
var _transform = function (jid) {
var chatbox = converse.chatboxes.get(jid);
if (!chatbox) {
return null;
}
return wrappedChatBox(chatbox);
};
if (typeof jids === "undefined") {
jids = converse.roster.pluck('jid');
converse.log("chats.get: You need to provide at least one JID", "error");
return null;
} else if (typeof jids === "string") {
return _transform(jids);
return getWrappedChatBox(jids);
}
return _.filter(_.map(jids, _transform), function (i) {return i !== null;});
return _.map(jids, getWrappedChatBox);
}
},
'tokens': {
......
......@@ -5,7 +5,11 @@ Changelog
-----------------
* Bugfix. Prevent attaching twice during initialization. [jcbrand]
* API method chats.get can now also return chat boxes which haven't been opened yet. [jcbrand]
* Add API method contacts.add. [pzia]
* #356 Fix the plugin extend function. [floriancargoet]
* #357 Fix the known bug where a state notification reopens a chat box. [floriancargoet]
* #358 Bugfix. Chat rooms show the same occupants bug. [floriancargoet]
0.9.1 (2015-03-26)
------------------
......
......@@ -41,9 +41,8 @@ directory:
make dev
On Windows you need to specify Makefile.win to be used by running:
::
On Windows you need to specify Makefile.win to be used by running: ::
make -f Makefile.win dev
Or alternatively, if you don't have GNU Make:
......@@ -291,8 +290,7 @@ You may also provide the fullname. If not present, we use the jid as fullname.
get
~~~
Returns an object representing a chat box, if that chat box is already open.
If the chat box is not already open, this method will return ``null``.
Returns an object representing a chat box.
To return a single chat box, provide the JID of the contact you're chatting
with in that chat box::
......@@ -321,7 +319,7 @@ To return an array of chat boxes, provide an array of JIDs::
converse.chats.open(['buddy1@example.com', 'buddy2@example.com'])
*The returned chat box contains the following methods:*
*The returned chat box object contains the following methods:*
+-------------+------------------------------------------+
| Method | Description |
......@@ -340,6 +338,8 @@ To return an array of chat boxes, provide an array of JIDs::
+-------------+------------------------------------------+
| close | Close the chat box. |
+-------------+------------------------------------------+
| open | Opens the chat box. |
+-------------+------------------------------------------+
*The get and set methods can be used to retrieve and change the following attributes:*
......
This diff is collapsed.
......@@ -137,21 +137,23 @@
test_utils.createContacts('current');
}, converse));
it("has a method 'get' which returns a wrapped chat box if it's already open", $.proxy(function () {
it("has a method 'get' which returns a wrapped chat box", $.proxy(function () {
// Test on chat that doesn't exist.
expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy();
// Test on chat that's not open
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var box = converse_api.chats.get(jid);
expect(box).toBe(null);
expect(box instanceof Object).toBeTruthy();
var chatboxview = this.chatboxviews.get(jid);
expect(chatboxview.$el.is(':visible')).toBeFalsy();
// Test for single JID
test_utils.openChatBoxFor(jid);
box = converse_api.chats.get(jid);
expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(b64_sha1(jid));
var chatboxview = this.chatboxviews.get(jid);
chatboxview = this.chatboxviews.get(jid);
expect(chatboxview.$el.is(':visible')).toBeTruthy();
// Test for multiple JIDs
......
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