comparison nickname.js @ 0:2a8d4e8600d0

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 21 Dec 2018 21:34:17 +0100
parents
children d6df73b466f6
comparison
equal deleted inserted replaced
-1:000000000000 0:2a8d4e8600d0
1 'use strict';
2
3 function initNickname(connection) {
4 const nick_input = document.getElementById('nick');
5 const nick_change = document.getElementById('nick-change');
6
7 const iq = $iq({type: 'get'})
8 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
9 .c('items', {node: 'http://jabber.org/protocol/nick'});
10 connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.'));
11
12 function nsResolver(prefix) {
13 return {
14 pubsub: 'http://jabber.org/protocol/pubsub',
15 nickname: 'http://jabber.org/protocol/nick',
16 }[prefix] || null;
17 }
18
19 function parseXPath(elem, xpath)
20 {
21 return elem.getRootNode().evaluate(xpath, elem, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
22 }
23
24 function onNickname(result_iq)
25 {
26 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
27 if (item == null)
28 return onNicknameRetrievalError('no item found.');
29 const id = item.getAttributeNS(null, 'id');
30 const nick = parseXPath(item, './nickname:nick');
31 nick_input.value = nick.textContent;
32 }
33
34 function onNicknameRetrievalError(string)
35 {
36 console.log('Failed to retrieve nickname: ' + string);
37 }
38
39 nick_change.addEventListener('click', function (evt) {
40 const iq = $iq({type: 'set'})
41 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
42 .c('publish', {node: 'http://jabber.org/protocol/nick'})
43 .c('item', {id: 'current'})
44 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
45 .t(nick_input.value);
46 connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError);
47 });
48
49 function onNicknameChanged(iq)
50 {
51 console.log("onNicknameChanged", iq);
52 }
53
54 function onNicknameChangeError(iq)
55 {
56 console.log("onNicknameChangeError", iq);
57 }
58 }