changeset 48:021185105e2f

Move the node configuration retrieval to a util promise.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 23 May 2020 20:37:20 +0200
parents b76146a09e07
children d3b943ac148f
files nickname.js util.js
diffstat 2 files changed, 49 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/nickname.js
+++ b/nickname.js
@@ -11,53 +11,27 @@ function initNickname(connection) {
             .c('items', {node: 'http://jabber.org/protocol/nick'});
     connection.sendIQ(iq, onNickname, onNicknameRetrievalError);
     displaySpinner(spinner_img);
-    retrieveConfiguration();
+    getNicknameAccessModel();
     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)
+    function getNicknameAccessModel()
     {
-        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;
+        retrieveConfiguration(connection).then((access_model) => {
+            console.log('yay', access_model);
+            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;
             }
-        }
-        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);
+            hideSpinner(access_spinner_img);
+        }, (reason) => {
+            console.log('Failed to retrieve nickname configuration: ' + reason);
+            hideSpinner(access_spinner_img);
+        });
     }
 
     function onNickname(result_iq)
@@ -108,7 +82,7 @@ function initNickname(connection) {
         console.log('Nickname successfully ' + action + '.');
         nick_access.disabled = true;
         if (action === 'changed')
-            retrieveConfiguration();
+            getNicknameAccessModel();
         spinnerOk(spinner_img);
     }
 
--- a/util.js
+++ b/util.js
@@ -1,3 +1,5 @@
+'use strict';
+
 const NS = {
     xrd: 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
     roster: 'jabber:iq:roster',
@@ -69,6 +71,35 @@ function parseErrorIq(iq) {
     return condition.localName + ': ' + text.textContent;
 }
 
+function retrieveConfiguration(connection)
+{
+    return new Promise((resolve, reject) => {
+        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, onNodeConfigure.bind(null, resolve, reject), reject);
+    });
+}
+
+function onNodeConfigure(resolve, reject, 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)
+        return reject('no fields');
+    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;
+        }
+    }
+    return resolve(access_model);
+}
+
 function displaySpinner(spinner) {
     if ('timeoutid' in spinner.dataset)
         clearTimeout(spinner.dataset.timeoutid);