changeset 17:07543f7f5e89

Retrieve and change the default MAM prefs.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 22 Dec 2018 19:42:42 +0100
parents c3e2e0c62486
children 3c02cbced2df
files client.js index.xhtml mam.js util.js
diffstat 4 files changed, 67 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/client.js
+++ b/client.js
@@ -101,6 +101,7 @@ document.addEventListener('DOMContentLoa
         initPEP(connection);
         initNickname(connection);
         initAvatar(connection);
+        initMAM(connection);
     }
 
     function onDisconnected()
--- a/index.xhtml
+++ b/index.xhtml
@@ -139,11 +139,12 @@ Enter your nickname, so people you know 
 </div>
 <div class="col-sm-8">
 <p>
-<label>Which messages to store in your archive? <select id="mam-prefs" disabled="">
+<label>Which messages to store in your archive? <select id="mam-default">
 <option value="always">All messages</option>
 <option value="never">No messages</option>
 <option value="roster">Messages from your contacts only</option>
-</select></label><br/>
+</select></label>
+<img width="24" height="24" id="mam-prefs-spinner" hidden=""/><br/>
 <table>
 <tr>
 <th>Always:</th>
@@ -186,6 +187,7 @@ Enter your nickname, so people you know 
 <script src="avatar.js"/>
 <script src="pep.js"/>
 <script src="roster.js"/>
+<script src="mam.js"/>
 
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/mam.js
@@ -0,0 +1,61 @@
+'use strict';
+
+function initMAM(connection) {
+    const prefs_default = document.getElementById('mam-default');
+    const prefs_spinner = document.getElementById('mam-prefs-spinner');
+
+    const iq = $iq({type: 'get'})
+        .c('prefs', {xmlns: NS.mam});
+    connection.sendIQ(iq, onMAMPrefs, onMAMPrefsError.bind(null, 'query failed.'));
+
+    function onMAMPrefs(result_iq)
+    {
+        const prefs = parseXPath(result_iq, './mam:prefs');
+        if (prefs == null)
+            return onMAMPrefsError('server error.');
+        const default_ = prefs.getAttributeNS(null, 'default');
+        prefs_default.value = default_;
+        const always = parseXPath(prefs, './mam:always/mam:jid', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
+        while (true) {
+            const item = always.iterateNext();
+            if (!item)
+                break;
+            const jid = item.textContent;
+            console.log(jid);
+        }
+        const never = parseXPath(prefs, './mam:never/mam:jid', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
+        while (true) {
+            const item = never.iterateNext();
+            if (!item)
+                break;
+            const jid = item.textContent;
+            console.log(jid);
+        }
+    }
+
+    function onMAMPrefsError(string)
+    {
+        console.log('Failed to retrieve your message archiving preferences: ' + string);
+    }
+
+    prefs_default.addEventListener('change', function (evt) {
+        const value = evt.target.value;
+
+        const iq = $iq({type: 'set'})
+            .c('prefs', {xmlns: NS.mam, 'default': prefs_default.value});
+        connection.sendIQ(iq, onPrefsSet, onPrefsError);
+        displaySpinner(prefs_spinner);
+    });
+
+    function onPrefsSet(iq)
+    {
+        console.log('MAM preferences successfully updated.');
+        spinnerOk(prefs_spinner);
+    }
+
+    function onPrefsError(iq)
+    {
+        console.log('Error while changing MAM preferences.');
+        spinnerError(prefs_spinner);
+    }
+}
--- a/util.js
+++ b/util.js
@@ -9,6 +9,7 @@ const NS = {
     avatar_metadata: 'urn:xmpp:avatar:metadata',
     avatar_data: 'urn:xmpp:avatar:data',
     nickname: 'http://jabber.org/protocol/nick',
+    mam: 'urn:xmpp:mam:2',
 };
 
 function nsResolver(prefix) {