view mam.js @ 63:ee1df80a1715 default tip

Nicer-looking input form
author mathieui
date Sun, 24 May 2020 14:19:29 +0200
parents 78c873be0caa
children
line wrap: on
line source

// SPDX-License-Identifier: AGPL-3.0-only
/*
 * Copyright © 2018-2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 * Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net>
 *
 * Released under GNU AGPL v3 only, read the file 'LICENSE' for more information.
 */

'use strict';

function initMAM(connection) {
    const prefs_default = document.getElementById('mam-prefs-default');
    const prefs_always = document.getElementById('mam-prefs-always');
    const prefs_never = document.getElementById('mam-prefs-never');
    const prefs_spinner = document.getElementById('mam-prefs-spinner');

    const mam_retrieve = document.getElementById('mam-retrieve');
    const download_button = document.getElementById('mam-download');
    const retrieve_spinner = document.getElementById('mam-retrieve-spinner');

    const mam_data = [];

    download_button.disabled = true;

    const iq = $iq({type: 'get'})
        .c('prefs', {xmlns: NS.mam});
    connection.sendIQ(iq, onMAMPrefs, onMAMPrefsError.bind(null, 'query failed.'));
    displaySpinner(prefs_spinner);

    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_;
        prefs_default.disabled = false;
        hideSpinner(prefs_spinner);
        const always = parseXPath(prefs, './mam:always/mam:jid', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
        const always_array = [];
        while (true) {
            const item = always.iterateNext();
            if (!item)
                break;
            const jid = item.textContent;
            always_array.push(jid);
        }
        prefs_always.value = always_array.join('\n');
        const never = parseXPath(prefs, './mam:never/mam:jid', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
        const never_array = [];
        while (true) {
            const item = never.iterateNext();
            if (!item)
                break;
            const jid = item.textContent;
            never_array.push(jid);
        }
        prefs_never.value = never_array.join('\n');
    }

    function onMAMPrefsError(string)
    {
        console.log('Failed to retrieve your message archiving preferences: ' + string);
        spinnerError(prefs_spinner);
    }

    function submitPrefs(evt) {
        const iq = $iq({type: 'set'})
            .c('prefs', {xmlns: NS.mam, 'default': prefs_default.value})
                .c('always');
        const always_array = prefs_always.value.split('\n');
        for (let line of always_array) {
            iq.c('jid').t(line).up();
        }
        iq.up().c('never');
        const never_array = prefs_never.value.split('\n');
        for (let line of never_array) {
            iq.c('jid').t(line).up();
        }
        connection.sendIQ(iq, onPrefsSet, onPrefsError);
        displaySpinner(prefs_spinner);
    }

    prefs_default.addEventListener('change', submitPrefs);
    prefs_always.addEventListener('change', submitPrefs);
    prefs_never.addEventListener('change', submitPrefs);

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

    mam_retrieve.addEventListener('click', function (evt) {
        connection.mam.query(null, {
            onMessage: function (message, delay, id) {
                console.log('Got a MAM message:', message, delay, id);
                const result = document.createElementNS(NS.mam, 'result');
                result.setAttributeNS(null, 'id', id);
                const forwarded = document.createElementNS(NS.forward, 'forwarded');
                forwarded.appendChild(delay);
                forwarded.appendChild(message);
                result.appendChild(forwarded);
                mam_data.push(result);
            },
            onComplete: function () {
                console.log('Received all messages, you can now download a backup of your archive.');
                download_button.disabled = false;
                spinnerOk(retrieve_spinner);
            },
        });
        displaySpinner(retrieve_spinner);
    });

    download_button.addEventListener('click', function (evt) {
        // TODO: extend XEP-0227 with MAM support.
        const messages = document.createElementNS('https://linkmauve.fr/protocol/pie-mam', 'message-archive');
        for (let result of mam_data)
            messages.appendChild(result);
        const blob = new Blob([messages.outerHTML], {type: 'text/xml'});
        const link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
        link.hidden = false;
        link.href = URL.createObjectURL(blob);
        link.download = Strophe.getBareJidFromJid(connection.jid) + '-archive.xml';
        console.log(link.download);
        document.body.appendChild(link);
        link.click();
        URL.revokeObjectURL(link.href);
        document.body.removeChild(link);
    });
}