Mercurial > xmpp-account-manager
changeset 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 | af2874ff7234 |
children | 021185105e2f |
files | index.xhtml nickname.js |
diffstat | 2 files changed, 82 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/index.xhtml +++ b/index.xhtml @@ -45,7 +45,7 @@ <p class="form-group"> <label for="nick-access">Who can see your nickname?</label> <img width="24" height="24" id="nick-access-spinner" hidden=""/> -<select class="form-control" id="nick-access"> +<select class="form-control" id="nick-access" disabled=""> <option value="open">Anyone</option> <option value="presence">Only your contacts</option> </select>
--- a/nickname.js +++ b/nickname.js @@ -9,46 +9,112 @@ function initNickname(connection) { const iq = $iq({type: 'get'}) .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) .c('items', {node: 'http://jabber.org/protocol/nick'}); - connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.')); + connection.sendIQ(iq, onNickname, onNicknameRetrievalError); displaySpinner(spinner_img); + retrieveConfiguration(); + displaySpinner(access_spinner_img); + + function retrieveConfiguration() + { + const iq = $iq({type: 'get'}) + .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) + .c('configure', {node: 'http://jabber.org/protocol/nick'}); + connection.sendIQ(iq, onConfigure, onConfigureError); + } + + function onConfigure(result_iq) + { + const fields = parseXPath(result_iq, './pubsub_owner:pubsub/pubsub_owner:configure/dataforms:x/dataforms:field', XPathResult.UNORDERED_NODE_ITERATOR_TYPE); + if (fields === null) { + console.log('Failed to retrieve nickname configuration.'); + hideSpinner(spinner_img); + return; + } + let access_model = null; + while (true) { + const field = fields.iterateNext(); + if (field === null) + break; + const var_ = field.getAttributeNS(null, 'var'); + if (var_ === 'pubsub#access_model') { + const value = parseXPath(field, './dataforms:value'); + access_model = value.textContent; + } + } + if (access_model !== null) { + if (access_model === 'open') + nick_access.value = 'open'; + else if (access_model === 'presence') + nick_access.value = 'presence'; + else + console.log('Unsupported nickname access model: ' + access_model); + nick_access.disabled = false; + } + hideSpinner(access_spinner_img); + } + + function onConfigureError(iq) + { + console.log('Failed to retrieve nickname configuration: ' + parseErrorIq(iq)); + nick_access.disabled = true; + hideSpinner(access_spinner_img); + } function onNickname(result_iq) { const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); - if (item == null) - return onNicknameRetrievalError('no item found.'); + if (item == null) { + console.log('Failed to retrieve nickname: no item published.'); + hideSpinner(spinner_img); + return; + } + const id = item.getAttributeNS(null, 'id'); const nick = parseXPath(item, './nickname:nick'); nick_input.value = nick.textContent; hideSpinner(spinner_img); } - function onNicknameRetrievalError(string) + function onNicknameRetrievalError(iq) { - console.log('Failed to retrieve nickname: ' + string); + console.log('Failed to retrieve nickname: ' + parseErrorIq(iq)); hideSpinner(spinner_img); } nick_input.addEventListener('change', function (evt) { - const iq = $iq({type: 'set'}) - .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) - .c('publish', {node: 'http://jabber.org/protocol/nick'}) - .c('item', {id: 'current'}) - .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) - .t(nick_input.value); - connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError); + const new_nickname = evt.target.value; + let iq; + let changed_callback; + if (new_nickname) { + iq = $iq({type: 'set'}) + .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) + .c('publish', {node: 'http://jabber.org/protocol/nick'}) + .c('item', {id: 'current'}) + .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) + .t(new_nickname); + changed_callback = onNicknameChanged.bind(null, 'changed'); + } else { + iq = $iq({type: 'set'}) + .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) + .c('delete', {node: 'http://jabber.org/protocol/nick'}); + changed_callback = onNicknameChanged.bind(null, 'deleted'); + } + connection.sendIQ(iq, changed_callback, onNicknameChangeError); displaySpinner(spinner_img); }); - function onNicknameChanged(result_iq) + function onNicknameChanged(action, result_iq) { - console.log("Nickname successfully changed."); + console.log('Nickname successfully ' + action + '.'); + nick_access.disabled = true; + if (action === 'changed') + retrieveConfiguration(); spinnerOk(spinner_img); } function onNicknameChangeError(iq) { - console.log("onNicknameChangeError", iq); + console.log('onNicknameChangeError', iq); spinnerError(spinner_img); }