|
@@ -6,6 +6,7 @@ import { _converse, api, converse } from '@converse/headless/core';
|
|
|
const { Strophe, $pres } = converse.env;
|
|
|
|
|
|
const XMPPStatus = Model.extend({
|
|
|
+
|
|
|
defaults () {
|
|
|
return { "status": api.settings.get("default_state") }
|
|
|
},
|
|
@@ -30,40 +31,57 @@ const XMPPStatus = Model.extend({
|
|
|
return '';
|
|
|
},
|
|
|
|
|
|
+ /** Constructs a presence stanza
|
|
|
+ * @param { string } [type]
|
|
|
+ * @param { string } [to] - The JID to which this presence should be sent
|
|
|
+ * @param { string } [status_message]
|
|
|
+ */
|
|
|
async constructPresence (type, to=null, status_message) {
|
|
|
type = typeof type === 'string' ? type : (this.get('status') || api.settings.get("default_state"));
|
|
|
status_message = typeof status_message === 'string' ? status_message : this.get('status_message');
|
|
|
+
|
|
|
let presence;
|
|
|
- const attrs = {to};
|
|
|
- if ((type === 'unavailable') ||
|
|
|
+
|
|
|
+ if (type === 'subscribe') {
|
|
|
+ presence = $pres({ to, type });
|
|
|
+ const { xmppstatus } = _converse;
|
|
|
+ const nick = xmppstatus.getNickname();
|
|
|
+ if (nick) presence.c('nick', {'xmlns': Strophe.NS.NICK}).t(nick).up();
|
|
|
+
|
|
|
+ } else if ((type === 'unavailable') ||
|
|
|
(type === 'probe') ||
|
|
|
(type === 'error') ||
|
|
|
(type === 'unsubscribe') ||
|
|
|
(type === 'unsubscribed') ||
|
|
|
- (type === 'subscribe') ||
|
|
|
(type === 'subscribed')) {
|
|
|
- attrs['type'] = type;
|
|
|
- presence = $pres(attrs);
|
|
|
+ presence = $pres({ to, type });
|
|
|
+
|
|
|
} else if (type === 'offline') {
|
|
|
- attrs['type'] = 'unavailable';
|
|
|
- presence = $pres(attrs);
|
|
|
+ presence = $pres({ to, type: 'unavailable' });
|
|
|
+
|
|
|
} else if (type === 'online') {
|
|
|
- presence = $pres(attrs);
|
|
|
+ presence = $pres({ to });
|
|
|
+
|
|
|
} else {
|
|
|
- presence = $pres(attrs).c('show').t(type).up();
|
|
|
+ presence = $pres({ to }).c('show').t(type).up();
|
|
|
}
|
|
|
|
|
|
- if (status_message) {
|
|
|
- presence.c('status').t(status_message).up();
|
|
|
- }
|
|
|
+ if (status_message) presence.c('status').t(status_message).up();
|
|
|
|
|
|
const priority = api.settings.get("priority");
|
|
|
presence.c('priority').t(isNaN(Number(priority)) ? 0 : priority).up();
|
|
|
- if (_converse.idle) {
|
|
|
+
|
|
|
+ const { idle, idle_seconds } = _converse;
|
|
|
+ if (idle) {
|
|
|
const idle_since = new Date();
|
|
|
- idle_since.setSeconds(idle_since.getSeconds() - _converse.idle_seconds);
|
|
|
- presence.c('idle', {xmlns: Strophe.NS.IDLE, since: idle_since.toISOString()});
|
|
|
+ idle_since.setSeconds(idle_since.getSeconds() - idle_seconds);
|
|
|
+ presence.c('idle', { xmlns: Strophe.NS.IDLE, since: idle_since.toISOString() });
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * *Hook* which allows plugins to modify a presence stanza
|
|
|
+ * @event _converse#constructedPresence
|
|
|
+ */
|
|
|
presence = await api.hook('constructedPresence', null, presence);
|
|
|
return presence;
|
|
|
}
|