Mercurial > xmpp-account-manager
comparison nickname.js @ 47:b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 23 May 2020 20:20:25 +0200 |
parents | 084202870d76 |
children | 021185105e2f |
comparison
equal
deleted
inserted
replaced
46:af2874ff7234 | 47:b76146a09e07 |
---|---|
7 const access_spinner_img = document.getElementById('nick-access-spinner'); | 7 const access_spinner_img = document.getElementById('nick-access-spinner'); |
8 | 8 |
9 const iq = $iq({type: 'get'}) | 9 const iq = $iq({type: 'get'}) |
10 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | 10 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) |
11 .c('items', {node: 'http://jabber.org/protocol/nick'}); | 11 .c('items', {node: 'http://jabber.org/protocol/nick'}); |
12 connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.')); | 12 connection.sendIQ(iq, onNickname, onNicknameRetrievalError); |
13 displaySpinner(spinner_img); | 13 displaySpinner(spinner_img); |
14 retrieveConfiguration(); | |
15 displaySpinner(access_spinner_img); | |
16 | |
17 function retrieveConfiguration() | |
18 { | |
19 const iq = $iq({type: 'get'}) | |
20 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) | |
21 .c('configure', {node: 'http://jabber.org/protocol/nick'}); | |
22 connection.sendIQ(iq, onConfigure, onConfigureError); | |
23 } | |
24 | |
25 function onConfigure(result_iq) | |
26 { | |
27 const fields = parseXPath(result_iq, './pubsub_owner:pubsub/pubsub_owner:configure/dataforms:x/dataforms:field', XPathResult.UNORDERED_NODE_ITERATOR_TYPE); | |
28 if (fields === null) { | |
29 console.log('Failed to retrieve nickname configuration.'); | |
30 hideSpinner(spinner_img); | |
31 return; | |
32 } | |
33 let access_model = null; | |
34 while (true) { | |
35 const field = fields.iterateNext(); | |
36 if (field === null) | |
37 break; | |
38 const var_ = field.getAttributeNS(null, 'var'); | |
39 if (var_ === 'pubsub#access_model') { | |
40 const value = parseXPath(field, './dataforms:value'); | |
41 access_model = value.textContent; | |
42 } | |
43 } | |
44 if (access_model !== null) { | |
45 if (access_model === 'open') | |
46 nick_access.value = 'open'; | |
47 else if (access_model === 'presence') | |
48 nick_access.value = 'presence'; | |
49 else | |
50 console.log('Unsupported nickname access model: ' + access_model); | |
51 nick_access.disabled = false; | |
52 } | |
53 hideSpinner(access_spinner_img); | |
54 } | |
55 | |
56 function onConfigureError(iq) | |
57 { | |
58 console.log('Failed to retrieve nickname configuration: ' + parseErrorIq(iq)); | |
59 nick_access.disabled = true; | |
60 hideSpinner(access_spinner_img); | |
61 } | |
14 | 62 |
15 function onNickname(result_iq) | 63 function onNickname(result_iq) |
16 { | 64 { |
17 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); | 65 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); |
18 if (item == null) | 66 if (item == null) { |
19 return onNicknameRetrievalError('no item found.'); | 67 console.log('Failed to retrieve nickname: no item published.'); |
68 hideSpinner(spinner_img); | |
69 return; | |
70 } | |
71 | |
20 const id = item.getAttributeNS(null, 'id'); | 72 const id = item.getAttributeNS(null, 'id'); |
21 const nick = parseXPath(item, './nickname:nick'); | 73 const nick = parseXPath(item, './nickname:nick'); |
22 nick_input.value = nick.textContent; | 74 nick_input.value = nick.textContent; |
23 hideSpinner(spinner_img); | 75 hideSpinner(spinner_img); |
24 } | 76 } |
25 | 77 |
26 function onNicknameRetrievalError(string) | 78 function onNicknameRetrievalError(iq) |
27 { | 79 { |
28 console.log('Failed to retrieve nickname: ' + string); | 80 console.log('Failed to retrieve nickname: ' + parseErrorIq(iq)); |
29 hideSpinner(spinner_img); | 81 hideSpinner(spinner_img); |
30 } | 82 } |
31 | 83 |
32 nick_input.addEventListener('change', function (evt) { | 84 nick_input.addEventListener('change', function (evt) { |
33 const iq = $iq({type: 'set'}) | 85 const new_nickname = evt.target.value; |
34 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | 86 let iq; |
35 .c('publish', {node: 'http://jabber.org/protocol/nick'}) | 87 let changed_callback; |
36 .c('item', {id: 'current'}) | 88 if (new_nickname) { |
37 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) | 89 iq = $iq({type: 'set'}) |
38 .t(nick_input.value); | 90 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) |
39 connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError); | 91 .c('publish', {node: 'http://jabber.org/protocol/nick'}) |
92 .c('item', {id: 'current'}) | |
93 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) | |
94 .t(new_nickname); | |
95 changed_callback = onNicknameChanged.bind(null, 'changed'); | |
96 } else { | |
97 iq = $iq({type: 'set'}) | |
98 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) | |
99 .c('delete', {node: 'http://jabber.org/protocol/nick'}); | |
100 changed_callback = onNicknameChanged.bind(null, 'deleted'); | |
101 } | |
102 connection.sendIQ(iq, changed_callback, onNicknameChangeError); | |
40 displaySpinner(spinner_img); | 103 displaySpinner(spinner_img); |
41 }); | 104 }); |
42 | 105 |
43 function onNicknameChanged(result_iq) | 106 function onNicknameChanged(action, result_iq) |
44 { | 107 { |
45 console.log("Nickname successfully changed."); | 108 console.log('Nickname successfully ' + action + '.'); |
109 nick_access.disabled = true; | |
110 if (action === 'changed') | |
111 retrieveConfiguration(); | |
46 spinnerOk(spinner_img); | 112 spinnerOk(spinner_img); |
47 } | 113 } |
48 | 114 |
49 function onNicknameChangeError(iq) | 115 function onNicknameChangeError(iq) |
50 { | 116 { |
51 console.log("onNicknameChangeError", iq); | 117 console.log('onNicknameChangeError', iq); |
52 spinnerError(spinner_img); | 118 spinnerError(spinner_img); |
53 } | 119 } |
54 | 120 |
55 nick_access.addEventListener('change', function (evt) { | 121 nick_access.addEventListener('change', function (evt) { |
56 const iq = configurePEPField('http://jabber.org/protocol/nick', 'pubsub#access_model', evt.target.value); | 122 const iq = configurePEPField('http://jabber.org/protocol/nick', 'pubsub#access_model', evt.target.value); |