Refactor XMPPStatus.

- Remove undocumented and unused event `update-status-ui`
- Remove xhr_custom_status and xhr_custom_status_url options
- Use default value
- Remove unnecessary getter and setter
This commit is contained in:
JC Brand 2018-02-21 14:40:41 +01:00
parent ad01ab3041
commit ec2bda338b
4 changed files with 37 additions and 79 deletions

View File

@ -1,5 +1,13 @@
# Changelog
## 4.0.0 (Unreleased)
* Removed the `xhr_custom_status` and `xhr_custom_status_url` configuration
settings. If you relied on these settings, you can instead listen for the
[statusMessageChanged](https://conversejs.org/docs/html/events.html#contactstatusmessagechanged)
event and make the XMLHttpRequest yourself.
## 3.3.4 (Unreleased)
- Avoid `eval` (via `_.template` from lodash).

View File

@ -1526,32 +1526,6 @@ An example from `the embedded room demo <https://conversejs.org/demo/embedded.ht
});
xhr_custom_status
-----------------
* Default: ``false``
.. note::
XHR stands for XMLHTTPRequest, and is meant here in the AJAX sense (Asynchronous JavaScript and XML).
This option will let converse.js make an AJAX POST with your changed custom chat status to a
remote server.
xhr_custom_status_url
---------------------
.. note::
XHR stands for XMLHTTPRequest, and is meant here in the AJAX sense (Asynchronous JavaScript and XML).
* Default: Empty string
Used only in conjunction with ``xhr_custom_status``.
This is the URL to which the AJAX POST request to set the user's custom status
message will be made.
The message itself is sent in the request under the key ``msg``.
xhr_user_search
---------------

View File

@ -329,9 +329,7 @@
synchronize_availability: true,
view_mode: 'overlayed', // Choices are 'overlayed', 'fullscreen', 'mobile'
websocket_url: undefined,
whitelisted_plugins: [],
xhr_custom_status: false,
xhr_custom_status_url: '',
whitelisted_plugins: []
};
_.assignIn(this, this.default_settings);
// Allow only whitelisted configuration attributes to be overwritten
@ -401,7 +399,7 @@
_converse.auto_changed_status = false;
// XXX: we should really remember the original state here, and
// then set it back to that...
_converse.xmppstatus.setStatus(_converse.default_state);
_converse.xmppstatus.set('status', _converse.default_state);
}
};
@ -414,7 +412,7 @@
// This can happen when the connection reconnects.
return;
}
const stat = _converse.xmppstatus.getStatus();
const stat = _converse.xmppstatus.get('status');
_converse.idle_seconds++;
if (_converse.csi_waiting_time > 0 &&
_converse.idle_seconds > _converse.csi_waiting_time &&
@ -425,12 +423,12 @@
_converse.idle_seconds > _converse.auto_away &&
stat !== 'away' && stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.setStatus('away');
_converse.xmppstatus.set('status', 'away');
} else if (_converse.auto_xa > 0 &&
_converse.idle_seconds > _converse.auto_xa &&
stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.setStatus('xa');
_converse.xmppstatus.set('status', 'xa');
}
};
@ -1482,18 +1480,21 @@
this.connfeedback = new this.ConnectionFeedback();
this.XMPPStatus = Backbone.Model.extend({
defaults: {
"status": _converse.default_state
},
initialize () {
this.set({
'status' : this.getStatus()
this.on('change:status', (item) => {
const status = this.get('status');
this.sendPresence(status);
_converse.emit('statusChanged', status);
});
this.on('change', (item) => {
if (_.has(item.changed, 'status')) {
_converse.emit('statusChanged', this.get('status'));
}
if (_.has(item.changed, 'status_message')) {
_converse.emit('statusMessageChanged', this.get('status_message'));
}
this.on('change:status_message', () => {
const status_message = this.get('status_message');
this.sendPresence(this.get('status'), status_message);
_converse.emit('statusMessageChanged', status_message);
});
},
@ -1529,30 +1530,6 @@
sendPresence (type, status_message) {
_converse.connection.send(this.constructPresence(type, status_message));
},
setStatus (value) {
this.sendPresence(value);
this.save({'status': value});
},
getStatus () {
return this.get('status') || _converse.default_state;
},
setStatusMessage (status_message) {
this.sendPresence(this.getStatus(), status_message);
this.save({'status_message': status_message});
if (this.xhr_custom_status) {
const xhr = new XMLHttpRequest();
xhr.open('POST', this.xhr_custom_status_url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhr.send({'msg': status_message});
}
const prev_status = this.get('status_message');
if (prev_status === status_message) {
this.trigger("update-status-ui", this);
}
}
});

View File

@ -35,11 +35,14 @@
* loaded by converse.js's plugin machinery.
*/
const { _converse } = this,
{ __ } = _converse;
{ __ } = _converse;
_converse.ChatStatusModal = Backbone.VDOMView.extend({
id: "modal-status-change",
events: {
"submit.set-xmpp-status .logout": "onFormSubmitted"
},
initialize () {
this.render().insertIntoDOM();
@ -72,14 +75,19 @@
'modal_title': __('Change chat status'),
'placeholder_status_message': __('Personal status message')
}));
},
onFormSubmitted (ev) {
ev.preventDefault();
debugger;
this.model.save('status_message', ev.target.querySelector('input').value);
}
});
_converse.XMPPStatusView = Backbone.VDOMView.extend({
tagName: "div",
events: {
"click a.change-status": "renderStatusChangeForm",
"submit": "setStatusMessage",
"click a.change-status": this.status_modal.show.bind(this.status_modal),
"click .dropdown dd ul li a": "setStatus",
"click .logout": "logOut"
},
@ -103,15 +111,6 @@
}));
},
renderStatusChangeForm (ev) {
this.status_modal.show();
},
setStatusMessage (ev) {
ev.preventDefault();
this.model.setStatusMessage(ev.target.querySelector('input').value);
},
logOut (ev) {
ev.preventDefault();
const result = confirm(__("Are you sure you want to log out?"));
@ -123,7 +122,7 @@
setStatus (ev) {
ev.preventDefault();
const value = ev.target.getAttribute('data-value');
this.model.setStatus(value);
this.model.set('status', value);
},
getPrettyStatus (stat) {