-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
71 lines (66 loc) · 1.87 KB
/
time.js
File metadata and controls
71 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @file time.js
* An implementation of XEP-0202 (Entity Time)
* @author Christoph Burschka
* @see http://xmpp.org/extensions/xep-0202.html
*/
define(['strophe.js'], ({Strophe, $iq}) => {
Strophe.addNamespace('TIME', 'urn:xmpp:time');
Strophe.addConnectionPlugin('time', {
init(conn) {
this._c = conn;
if (this._c.disco) {
this._c.disco.addFeature(Strophe.NS.TIME);
}
this.addHandler();
},
/**
* Request time information from an entity.
*
* @param {string} to
* @param {int} timeout
*
* @return {Promise} A promise that resolves to the response.
*/
query(to, timeout) {
const id = this._c.getUniqueId('time');
const iq = $iq({id, to, type: 'get'});
iq.c('time', {xmlns: Strophe.NS.TIME});
return new Promise((resolve, reject) => this._c.sendIQ(iq, resolve, reject, timeout));
},
/**
* Respond to a time request.
*
* @param {Stanza} request
*
* @return {boolean} Always returns true.
*/
respond(request) {
const iq = $iq({
id: request.getAttribute('id'),
to: request.getAttribute('from'),
type: 'result',
});
iq.c('time', {xmlns: Strophe.NS.TIME});
iq.c('utc', {}, moment().toISOString());
iq.c('tzo', {}, moment().format('Z'));
this._c.sendIQ(iq);
return true;
},
/**
* Add a time request handler.
*
* @param {function} handler (optional, defaults to a simple response).
*
* @return A reference to the handler that can be used to remove it.
*/
addHandler(handler=null) {
if (this.handler) {
this._c.deleteHandler(this.handler);
}
handler = handler || (request => this.respond(request));
this.handler = this._c.addHandler(handler, Strophe.NS.TIME, 'iq', 'get');
return this.handler;
}
});
});