Don't try to create ChatBox from controlbox data in embedded mode

updates #1374
This commit is contained in:
JC Brand 2018-12-12 14:08:00 +01:00
parent e38daf34f7
commit 5dacb96b78
3 changed files with 130 additions and 24 deletions

83
dist/converse.js vendored
View File

@ -50894,6 +50894,11 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
* NB: These plugins need to have already been loaded via require.js. * NB: These plugins need to have already been loaded via require.js.
*/ */
dependencies: ["converse-modal", "converse-chatboxes", "converse-rosterview", "converse-chatview"], dependencies: ["converse-modal", "converse-chatboxes", "converse-rosterview", "converse-chatview"],
enabled(_converse) {
return _converse.view_mode !== 'embedded';
},
overrides: { overrides: {
// Overrides mentioned here will be picked up by converse.js's // Overrides mentioned here will be picked up by converse.js's
// plugin architecture they will replace existing methods on the // plugin architecture they will replace existing methods on the
@ -50915,6 +50920,16 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
}, },
ChatBoxes: { ChatBoxes: {
model(attrs, options) {
const _converse = this.__super__._converse;
if (attrs.id == 'controlbox') {
return new _converse.ControlBox(attrs, options);
} else {
return this.__super__.model.apply(this, arguments);
}
},
chatBoxMayBeShown(chatbox) { chatBoxMayBeShown(chatbox) {
return this.__super__.chatBoxMayBeShown.apply(this, arguments) && chatbox.get('id') !== 'controlbox'; return this.__super__.chatBoxMayBeShown.apply(this, arguments) && chatbox.get('id') !== 'controlbox';
} }
@ -50953,6 +50968,14 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
}, },
ChatBox: { ChatBox: {
validate(attrs, options) {
const _converse = this.__super__._converse;
if (_converse.view_mode === 'embedded' && attrs.type === _converse.CONTROLBOX_TYPE) {
return 'Controlbox not relevant in embedded view mode';
}
},
initialize() { initialize() {
if (this.get('id') === 'controlbox') { if (this.get('id') === 'controlbox') {
this.set({ this.set({
@ -50997,15 +51020,28 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
_converse.api.promises.add('controlboxInitialized'); _converse.api.promises.add('controlboxInitialized');
_converse.addControlBox = () => { const addControlBox = () => _converse.chatboxes.add({
return _converse.chatboxes.add({ 'id': 'controlbox'
'id': 'controlbox',
'box_id': 'controlbox',
'type': _converse.CONTROLBOX_TYPE,
'closed': !_converse.show_controlbox_by_default
}); });
};
_converse.ControlBox = _converse.ChatBox.extend({
defaults: {
'bookmarked': false,
'box_id': 'controlbox',
'chat_state': undefined,
'closed': !_converse.show_controlbox_by_default,
'num_unread': 0,
'type': _converse.CONTROLBOX_TYPE,
'url': ''
},
initialize() {
u.safeSave(this, {
'time_opened': this.get('time_opened') || moment().valueOf()
});
}
});
_converse.ControlBoxView = _converse.ChatBoxView.extend({ _converse.ControlBoxView = _converse.ChatBoxView.extend({
tagName: 'div', tagName: 'div',
className: 'chatbox', className: 'chatbox',
@ -51395,7 +51431,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
let controlbox = _converse.chatboxes.get('controlbox'); let controlbox = _converse.chatboxes.get('controlbox');
if (!controlbox) { if (!controlbox) {
controlbox = _converse.addControlBox(); controlbox = addControlBox();
} }
if (_converse.connection.connected) { if (_converse.connection.connected) {
@ -51462,11 +51498,10 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
} }
}); });
Promise.all([_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('chatBoxViewsInitialized')]).then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL)); Promise.all([_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('chatBoxViewsInitialized')]).then(addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.on('chatBoxesFetched', () => { _converse.on('chatBoxesFetched', () => {
const controlbox = _converse.chatboxes.get('controlbox') || _converse.addControlBox(); const controlbox = _converse.chatboxes.get('controlbox') || addControlBox();
controlbox.save({ controlbox.save({
connected: true connected: true
}); });
@ -61571,6 +61606,18 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
initialize() { initialize() {
const jid = this.get('jid'); const jid = this.get('jid');
if (!jid) {
// XXX: The `validate` method will prevent this model
// from being persisted if there's no jid, but that gets
// called after model instantiation, so we have to deal
// with invalid models here also.
//
// This happens when the controlbox is in browser storage,
// but we're in embedded mode.
return;
}
this.vcard = _converse.vcards.findWhere({ this.vcard = _converse.vcards.findWhere({
'jid': jid 'jid': jid
}) || _converse.vcards.create({ }) || _converse.vcards.create({
@ -61595,8 +61642,10 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
this.sendMessageStanza(this.createMessageStanza(message)); this.sendMessageStanza(this.createMessageStanza(message));
} }
}); });
this.on('change:chat_state', this.sendChatState, this); this.on('change:chat_state', this.sendChatState, this); // Models get saved immediately after creation, so no need to
this.save({ // call `save` here.
this.set({
// The chat_state will be set to ACTIVE once the chat box is opened // The chat_state will be set to ACTIVE once the chat box is opened
// and we listen for change:chat_state, so shouldn't set it to ACTIVE here. // and we listen for change:chat_state, so shouldn't set it to ACTIVE here.
'box_id': b64_sha1(this.get('jid')), 'box_id': b64_sha1(this.get('jid')),
@ -61605,6 +61654,14 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
}); });
}, },
validate(attrs, options) {
const _converse = this.__super__._converse;
if (!attrs.jid) {
return 'Ignored ChatBox without JID';
}
},
getDisplayName() { getDisplayName() {
return this.vcard.get('fullname') || this.get('jid'); return this.vcard.get('fullname') || this.get('jid');
}, },

View File

@ -73,6 +73,10 @@ converse.plugins.add('converse-controlbox', {
*/ */
dependencies: ["converse-modal", "converse-chatboxes", "converse-rosterview", "converse-chatview"], dependencies: ["converse-modal", "converse-chatboxes", "converse-rosterview", "converse-chatview"],
enabled (_converse) {
return _converse.view_mode !== 'embedded';
},
overrides: { overrides: {
// Overrides mentioned here will be picked up by converse.js's // Overrides mentioned here will be picked up by converse.js's
// plugin architecture they will replace existing methods on the // plugin architecture they will replace existing methods on the
@ -94,6 +98,15 @@ converse.plugins.add('converse-controlbox', {
}, },
ChatBoxes: { ChatBoxes: {
model (attrs, options) {
const { _converse } = this.__super__;
if (attrs.id == 'controlbox') {
return new _converse.ControlBox(attrs, options);
} else {
return this.__super__.model.apply(this, arguments);
}
},
chatBoxMayBeShown (chatbox) { chatBoxMayBeShown (chatbox) {
return this.__super__.chatBoxMayBeShown.apply(this, arguments) && return this.__super__.chatBoxMayBeShown.apply(this, arguments) &&
chatbox.get('id') !== 'controlbox'; chatbox.get('id') !== 'controlbox';
@ -132,6 +145,13 @@ converse.plugins.add('converse-controlbox', {
}, },
ChatBox: { ChatBox: {
validate (attrs, options) {
const { _converse } = this.__super__;
if (_converse.view_mode === 'embedded' && attrs.type === _converse.CONTROLBOX_TYPE) {
return 'Controlbox not relevant in embedded view mode';
}
},
initialize () { initialize () {
if (this.get('id') === 'controlbox') { if (this.get('id') === 'controlbox') {
this.set({'time_opened': moment(0).valueOf()}); this.set({'time_opened': moment(0).valueOf()});
@ -171,14 +191,23 @@ converse.plugins.add('converse-controlbox', {
_converse.api.promises.add('controlboxInitialized'); _converse.api.promises.add('controlboxInitialized');
_converse.addControlBox = () => { const addControlBox = () => _converse.chatboxes.add({'id': 'controlbox'});
return _converse.chatboxes.add({
'id': 'controlbox', _converse.ControlBox = _converse.ChatBox.extend({
defaults: {
'bookmarked': false,
'box_id': 'controlbox', 'box_id': 'controlbox',
'chat_state': undefined,
'closed': !_converse.show_controlbox_by_default,
'num_unread': 0,
'type': _converse.CONTROLBOX_TYPE, 'type': _converse.CONTROLBOX_TYPE,
'closed': !_converse.show_controlbox_by_default 'url': ''
}) },
initialize () {
u.safeSave(this, {'time_opened': this.get('time_opened') || moment().valueOf()});
} }
});
_converse.ControlBoxView = _converse.ChatBoxView.extend({ _converse.ControlBoxView = _converse.ChatBoxView.extend({
@ -545,7 +574,7 @@ converse.plugins.add('converse-controlbox', {
showControlBox () { showControlBox () {
let controlbox = _converse.chatboxes.get('controlbox'); let controlbox = _converse.chatboxes.get('controlbox');
if (!controlbox) { if (!controlbox) {
controlbox = _converse.addControlBox(); controlbox = addControlBox();
} }
if (_converse.connection.connected) { if (_converse.connection.connected) {
controlbox.save({closed: false}); controlbox.save({closed: false});
@ -601,10 +630,10 @@ converse.plugins.add('converse-controlbox', {
Promise.all([ Promise.all([
_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('connectionInitialized'),
_converse.api.waitUntil('chatBoxViewsInitialized') _converse.api.waitUntil('chatBoxViewsInitialized')
]).then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL)); ]).then(addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.on('chatBoxesFetched', () => { _converse.on('chatBoxesFetched', () => {
const controlbox = _converse.chatboxes.get('controlbox') || _converse.addControlBox(); const controlbox = _converse.chatboxes.get('controlbox') || addControlBox();
controlbox.save({connected:true}); controlbox.save({connected:true});
}); });

View File

@ -234,6 +234,17 @@ converse.plugins.add('converse-chatboxes', {
initialize () { initialize () {
const jid = this.get('jid'); const jid = this.get('jid');
if (!jid) {
// XXX: The `validate` method will prevent this model
// from being persisted if there's no jid, but that gets
// called after model instantiation, so we have to deal
// with invalid models here also.
//
// This happens when the controlbox is in browser storage,
// but we're in embedded mode.
return;
}
this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid}); this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid});
// XXX: this creates a dependency on converse-roster, which we // XXX: this creates a dependency on converse-roster, which we
// probably shouldn't have here, so we should probably move // probably shouldn't have here, so we should probably move
@ -254,7 +265,9 @@ converse.plugins.add('converse-chatboxes', {
this.on('change:chat_state', this.sendChatState, this); this.on('change:chat_state', this.sendChatState, this);
this.save({ // Models get saved immediately after creation, so no need to
// call `save` here.
this.set({
// The chat_state will be set to ACTIVE once the chat box is opened // The chat_state will be set to ACTIVE once the chat box is opened
// and we listen for change:chat_state, so shouldn't set it to ACTIVE here. // and we listen for change:chat_state, so shouldn't set it to ACTIVE here.
'box_id' : b64_sha1(this.get('jid')), 'box_id' : b64_sha1(this.get('jid')),
@ -263,6 +276,13 @@ converse.plugins.add('converse-chatboxes', {
}); });
}, },
validate (attrs, options) {
const { _converse } = this.__super__;
if (!attrs.jid) {
return 'Ignored ChatBox without JID';
}
},
getDisplayName () { getDisplayName () {
return this.vcard.get('fullname') || this.get('jid'); return this.vcard.get('fullname') || this.get('jid');
}, },