changeset 52:2f45bee88b47

Add pubsub#access_model retrieval for the avatar node.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 23 May 2020 21:24:34 +0200
parents c27a4598ec44
children 9b612be4dd0b
files avatar.js index.xhtml nickname.js util.js
diffstat 4 files changed, 40 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -11,7 +11,7 @@ function initAvatar(connection) {
     const avatar_change = document.getElementById('avatar-change');
     const avatar_access = document.getElementById('avatar-access');
     const spinner_img = document.getElementById('avatar-spinner');
-    const access_model_spinner_img = document.getElementById('access-model-avatar-spinner');
+    const access_spinner_img = document.getElementById('access-model-avatar-spinner');
 
     avatar_img.src = DEFAULT_AVATAR;
     const iq = $iq({type: 'get'})
@@ -19,6 +19,28 @@ function initAvatar(connection) {
             .c('items', {node: 'urn:xmpp:avatar:metadata'});
     connection.sendIQ(iq, onAvatarMetadata, onAvatarRetrievalError.bind(null, 'PubSub metadata query failed.'));
     displaySpinner(spinner_img);
+    getAccessModel();
+
+    function getAccessModel()
+    {
+        avatar_access.disabled = true;
+        displaySpinner(access_spinner_img);
+        retrieveConfiguration(connection, 'urn:xmpp:avatar:metadata').then((access_model) => {
+            if (access_model !== null) {
+                if (access_model === 'open')
+                    avatar_access.value = 'open';
+                else if (access_model === 'presence')
+                    avatar_access.value = 'presence';
+                else
+                    console.log('Unsupported avatar access model: ' + access_model);
+                avatar_access.disabled = false;
+            }
+            hideSpinner(access_spinner_img);
+        }, (reason) => {
+            console.log('Failed to retrieve avatar configuration: ' + reason);
+            hideSpinner(access_spinner_img);
+        });
+    }
 
     function onAvatarMetadata(result_iq)
     {
@@ -68,6 +90,7 @@ function initAvatar(connection) {
         */
         avatar_img.src = url;
         hideSpinner(spinner_img);
+        getAccessModel();
     }
 
     function onAvatarRetrievalError(string)
@@ -75,6 +98,7 @@ function initAvatar(connection) {
         console.log('Failed to retrieve avatar, an empty one is displayed instead: ' + string);
         avatar_img.src = DEFAULT_AVATAR;
         hideSpinner(spinner_img);
+        avatar_access.disabled = true;
     }
 
     avatar_upload.addEventListener('click', function (evt) {
@@ -96,6 +120,7 @@ function initAvatar(connection) {
                             });
         connection.sendIQ(metadata_iq, onAvatarMetadataUpload, onAvatarUploadError);
         displaySpinner(spinner_img);
+        avatar_access.disabled = true;
     });
 
     function onAvatarMetadataUpload(iq)
@@ -115,12 +140,14 @@ function initAvatar(connection) {
         avatar_change.hidden = true;
         avatar_size.innerHTML = '';
         spinnerOk(spinner_img);
+        getAccessModel();
     }
 
     function onAvatarUploadError(iq)
     {
         console.log("onAvatarUploadError", iq);
         spinnerError(spinner_img);
+        avatar_access.disabled = true;
     }
 
     avatar_file.addEventListener('change', function (evt) {
@@ -180,7 +207,7 @@ function initAvatar(connection) {
     avatar_access.addEventListener('change', function (evt) {
         const iq = configurePEPField('urn:xmpp:avatar:metadata', 'pubsub#access_model', evt.target.value);
         connection.sendIQ(iq, onAvatarMetadataConfigured, onAvatarConfigureError.bind(null, 'PubSub configuration of metadata failed.'));
-        displaySpinner(access_model_spinner_img);
+        displaySpinner(access_spinner_img);
     });
 
     function onAvatarMetadataConfigured(result_iq)
@@ -192,12 +219,12 @@ function initAvatar(connection) {
     function onAvatarConfigured(result_iq)
     {
         console.log('Successfully set avatar access model.')
-        spinnerOk(access_model_spinner_img);
+        spinnerOk(access_spinner_img);
     }
 
     function onAvatarConfigureError(string)
     {
         console.log('Failed to configure avatar node: ' + string);
-        spinnerError(access_model_spinner_img);
+        spinnerError(access_spinner_img);
     }
 }
--- a/index.xhtml
+++ b/index.xhtml
@@ -71,7 +71,7 @@
 <p class="form-group">
 <label for="avatar-access">{% trans 'Who can see your avatar?' %}</label>
 <img width="24" height="24" id="access-model-avatar-spinner" hidden=""/>
-<select class="form-control" id="avatar-access">
+<select class="form-control" id="avatar-access" disabled="">
 <option value="open">{% trans 'Anyone (pick this if you use public groupchats)' %}</option>
 <option value="presence">{% trans 'Only your contacts' %}</option>
 </select>
--- a/nickname.js
+++ b/nickname.js
@@ -11,13 +11,13 @@ function initNickname(connection) {
             .c('items', {node: 'http://jabber.org/protocol/nick'});
     connection.sendIQ(iq, onNickname, onNicknameRetrievalError);
     displaySpinner(spinner_img);
-    getNicknameAccessModel();
-    displaySpinner(access_spinner_img);
+    getAccessModel();
 
-    function getNicknameAccessModel()
+    function getAccessModel()
     {
-        retrieveConfiguration(connection).then((access_model) => {
-            console.log('yay', access_model);
+        nick_access.disabled = true;
+        displaySpinner(access_spinner_img);
+        retrieveConfiguration(connection, 'http://jabber.org/protocol/nick').then((access_model) => {
             if (access_model !== null) {
                 if (access_model === 'open')
                     nick_access.value = 'open';
@@ -82,7 +82,7 @@ function initNickname(connection) {
         console.log('Nickname successfully ' + action + '.');
         nick_access.disabled = true;
         if (action === 'changed')
-            getNicknameAccessModel();
+            getAccessModel();
         spinnerOk(spinner_img);
     }
 
--- a/util.js
+++ b/util.js
@@ -71,12 +71,12 @@ function parseErrorIq(iq) {
     return condition.localName + ': ' + text.textContent;
 }
 
-function retrieveConfiguration(connection)
+function retrieveConfiguration(connection, node)
 {
     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'});
+                .c('configure', {node: node});
         connection.sendIQ(iq, onNodeConfigure.bind(null, resolve, reject), reject);
     });
 }