view mam.js @ 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
children 3c02cbced2df
line wrap: on
line source

'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);
    }
}