Commit 03b9447f authored by JC Brand's avatar JC Brand

Asynchronously render spoiler button only if all clients support it

parent 680f3041
...@@ -419,7 +419,7 @@ ...@@ -419,7 +419,7 @@
expect(view).toBeDefined(); expect(view).toBeDefined();
var $toolbar = $(view.el).find('ul.chat-toolbar'); var $toolbar = $(view.el).find('ul.chat-toolbar');
expect($toolbar.length).toBe(1); expect($toolbar.length).toBe(1);
expect($toolbar.children('li').length).toBe(4); expect($toolbar.children('li').length).toBe(3);
done(); done();
})); }));
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
} (this, function ($, jasmine, mock, converse, test_utils) { } (this, function ($, jasmine, mock, converse, test_utils) {
var Strophe = converse.env.Strophe; var Strophe = converse.env.Strophe;
var b64_sha1 = converse.env.b64_sha1; var b64_sha1 = converse.env.b64_sha1;
var $pres = converse.env.$pres;
var _ = converse.env._;
describe("A chatbox with an active OTR session", function() { describe("A chatbox with an active OTR session", function() {
...@@ -13,28 +15,57 @@ ...@@ -13,28 +15,57 @@
test_utils.createContacts(_converse, 'current'); test_utils.createContacts(_converse, 'current');
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(_converse, contact_jid);
var view = _converse.chatboxviews.get(contact_jid);
var spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).not.toBe(null);
view.model.set('otr_status', 0);
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).not.toBe(null);
view.model.set('otr_status', 1); // XXX: We need to send a presence from the contact, so that we
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler'); // have a resource, that resource is then queried to see
expect(spoiler_toggle).toBe(null); // whether Strophe.NS.SPOILER is supported, in which case
// the spoiler button will appear.
view.model.set('otr_status', 2); var presence = $pres({
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler'); 'from': contact_jid+'/phone',
expect(spoiler_toggle).toBe(null); 'to': 'dummy@localhost'
});
_converse.connection._dataRecv(test_utils.createRequest(presence));
test_utils.openChatBoxFor(_converse, contact_jid);
view.model.set('otr_status', 3); test_utils.waitUntilDiscoConfirmed(_converse, contact_jid+'/phone', [], [Strophe.NS.SPOILER]).then(function () {
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler'); var spoiler_toggle;
expect(spoiler_toggle).not.toBe(null); var view = _converse.chatboxviews.get(contact_jid);
done(); spyOn(view, 'addSpoilerButton').and.callThrough();
view.model.set('otr_status', 1);
test_utils.waitUntil(function () {
return _.isNull(view.el.querySelector('.toggle-compose-spoiler'));
}).then(function () {
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).toBe(null);
view.model.set('otr_status', 3);
return test_utils.waitUntil(function () {
return !_.isNull(view.el.querySelector('.toggle-compose-spoiler'));
});
}).then(function () {
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).not.toBe(null);
view.model.set('otr_status', 2);
return test_utils.waitUntil(function () {
return _.isNull(view.el.querySelector('.toggle-compose-spoiler'));
});
}).then(function () {
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).toBe(null);
view.model.set('otr_status', 4);
return test_utils.waitUntil(function () {
return !_.isNull(view.el.querySelector('.toggle-compose-spoiler'));
});
}).then(function () {
spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
expect(spoiler_toggle).not.toBe(null);
done();
});
});
})); }));
}); });
......
This diff is collapsed.
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
"tpl!message", "tpl!message",
"tpl!new_day", "tpl!new_day",
"tpl!spinner", "tpl!spinner",
"tpl!spoiler_button",
"tpl!spoiler_message", "tpl!spoiler_message",
"tpl!toolbar" "tpl!toolbar"
], factory); ], factory);
...@@ -40,11 +41,12 @@ ...@@ -40,11 +41,12 @@
tpl_message, tpl_message,
tpl_new_day, tpl_new_day,
tpl_spinner, tpl_spinner,
tpl_spoiler_button,
tpl_spoiler_message, tpl_spoiler_message,
tpl_toolbar tpl_toolbar
) { ) {
"use strict"; "use strict";
const { $msg, Backbone, Strophe, _, b64_sha1, sizzle, moment } = converse.env; const { $msg, Backbone, Promise, Strophe, _, b64_sha1, f, sizzle, moment } = converse.env;
const u = converse.env.utils; const u = converse.env.utils;
const KEY = { const KEY = {
ENTER: 13, ENTER: 13,
...@@ -323,6 +325,7 @@ ...@@ -323,6 +325,7 @@
this.getToolbarOptions(options || {}) this.getToolbarOptions(options || {})
); );
this.el.querySelector('.chat-toolbar').innerHTML = toolbar(options); this.el.querySelector('.chat-toolbar').innerHTML = toolbar(options);
this.addSpoilerButton(options);
this.insertEmojiPicker(); this.insertEmojiPicker();
return this; return this;
}, },
...@@ -343,14 +346,40 @@ ...@@ -343,14 +346,40 @@
'label_spoiler_hint': __('Optional hint'), 'label_spoiler_hint': __('Optional hint'),
'message_value': _.get(this.el.querySelector('.chat-textarea'), 'value'), 'message_value': _.get(this.el.querySelector('.chat-textarea'), 'value'),
'show_send_button': _converse.show_send_button, 'show_send_button': _converse.show_send_button,
'show_spoiler_button': _converse.visible_toolbar_buttons.spoiler,
'show_textarea': true,
'show_toolbar': _converse.show_toolbar, 'show_toolbar': _converse.show_toolbar,
'unread_msgs': __('You have unread messages') 'unread_msgs': __('You have unread messages')
})); }));
this.renderToolbar(); this.renderToolbar();
}, },
addSpoilerButton (options) {
/* Asynchronously adds a button for writing spoiler
* messages, based on whether the contact's client supports
* it.
*/
if (!options.show_spoiler_button || this.model.get('type') === 'chatroom') {
return;
}
const contact_jid = this.model.get('jid');
const resources = this.model.get('resources');
if (_.isEmpty(resources)) {
return;
}
Promise.all(_.map(_.keys(resources), (resource) =>
_converse.api.disco.supports(Strophe.NS.SPOILER, `${contact_jid}/${resource}`)
)).then((results) => {
const supported = _.every(f.map(f.get('supported'))(results));
if (supported) {
const html = tpl_spoiler_button(this.model.toJSON());
if (_converse.visible_toolbar_buttons.emoji) {
this.el.querySelector('.toggle-smiley').insertAdjacentHTML('afterEnd', html);
} else {
this.el.querySelector('.chat-toolbar').insertAdjacentHTML('afterBegin', html);
}
}
});
},
insertHeading () { insertHeading () {
this.heading = new _converse.ChatBoxHeading({'model': this.model}); this.heading = new _converse.ChatBoxHeading({'model': this.model});
this.heading.render(); this.heading.render();
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
define(["sizzle", define(["sizzle",
"es6-promise", "es6-promise",
"lodash.noconflict", "lodash.noconflict",
"lodash.fp",
"polyfill", "polyfill",
"i18n", "i18n",
"utils", "utils",
...@@ -19,7 +20,7 @@ ...@@ -19,7 +20,7 @@
"backbone.nativeview", "backbone.nativeview",
"backbone.browserStorage" "backbone.browserStorage"
], factory); ], factory);
}(this, function (sizzle, Promise, _, polyfill, i18n, utils, moment, Strophe, pluggable, Backbone) { }(this, function (sizzle, Promise, _, f, polyfill, i18n, utils, moment, Strophe, pluggable, Backbone) {
/* Cannot use this due to Safari bug. /* Cannot use this due to Safari bug.
* See https://github.com/jcbrand/converse.js/issues/196 * See https://github.com/jcbrand/converse.js/issues/196
...@@ -972,6 +973,7 @@ ...@@ -972,6 +973,7 @@
const resources = _.isObject(this.get('resources')) ? this.get('resources') : {}; const resources = _.isObject(this.get('resources')) ? this.get('resources') : {};
resources[resource] = { resources[resource] = {
'name': resource,
'priority': priority, 'priority': priority,
'status': chat_status, 'status': chat_status,
'timestamp': timestamp 'timestamp': timestamp
...@@ -2019,6 +2021,7 @@ ...@@ -2019,6 +2021,7 @@
'Promise': Promise, 'Promise': Promise,
'Strophe': Strophe, 'Strophe': Strophe,
'_': _, '_': _,
'f': f,
'b64_sha1': b64_sha1, 'b64_sha1': b64_sha1,
'moment': moment, 'moment': moment,
'sizzle': sizzle, 'sizzle': sizzle,
......
...@@ -146,8 +146,7 @@ ...@@ -146,8 +146,7 @@
} }
function registerHeadlineHandler () { function registerHeadlineHandler () {
_converse.connection.addHandler( _converse.connection.addHandler(onHeadlineMessage, null, 'message');
onHeadlineMessage, null, 'message');
} }
_converse.on('connected', registerHeadlineHandler); _converse.on('connected', registerHeadlineHandler);
_converse.on('reconnected', registerHeadlineHandler); _converse.on('reconnected', registerHeadlineHandler);
......
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
} }
const messages = []; const messages = [];
const message_handler = _converse.connection.addHandler(function (message) { const message_handler = _converse.connection.addHandler((message) => {
if (options.groupchat && message.getAttribute('from') !== options['with']) { // eslint-disable-line dot-notation if (options.groupchat && message.getAttribute('from') !== options['with']) { // eslint-disable-line dot-notation
return true; return true;
} }
......
define(['lodash', 'lodash.converter', 'converse-core'], function (_, lodashConverter, converse) { define(['lodash', 'lodash.converter'], function (_, lodashConverter) {
var fp = lodashConverter(_.runInContext()); var fp = lodashConverter(_.runInContext());
converse.env.fp = fp;
return fp; return fp;
}); });
{[ if (o.show_spoiler_button) { ]}
<!-- XXX: This markup is also in src/templates/toolbar.html -->
<li class="toggle-compose-spoiler"> <li class="toggle-compose-spoiler">
<a class=" <a class="
{[ if (o.sending_spoiler) { ]} icon-eye-blocked {[ } ]} {[ if (o.composing_spoiler) { ]} icon-eye-blocked {[ } ]}
{[ if (!o.sending_spoiler) { ]} icon-eye {[ } ]}" {[ if (!o.composing_spoiler) { ]} icon-eye {[ } ]}"
title="{{ o.title }}"></a> title="{{ o.label_toggle_spoiler }}"></a>
</li> </li>
{[ } ]}
...@@ -4,14 +4,6 @@ ...@@ -4,14 +4,6 @@
<span class="emoji-picker"></span> <span class="emoji-picker"></span>
</li> </li>
{[ } ]} {[ } ]}
{[ if (o.show_spoiler_button) { ]}
<li class="toggle-compose-spoiler">
<a class="
{[ if (o.composing_spoiler) { ]} icon-eye-blocked {[ } ]}
{[ if (!o.composing_spoiler) { ]} icon-eye {[ } ]}"
title="{{ o.label_toggle_spoiler }}"></a>
</li>
{[ } ]}
{[ if (o.show_call_button) { ]} {[ if (o.show_call_button) { ]}
<li class="toggle-call"><a class="icon-phone" title="{{{o.label_start_call}}}"></a></li> <li class="toggle-call"><a class="icon-phone" title="{{{o.label_start_call}}}"></a></li>
{[ } ]} {[ } ]}
......
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