Mercurial > xmpp-account-manager
diff mam.js @ 24:6c620e9f7d2c
Add support for retrieving all MAM messages, and for downloading it in a XEP-0227-like format.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 23 Dec 2018 15:39:51 +0100 |
parents | 46660687924b |
children | 45bd945c5722 |
line wrap: on
line diff
--- a/mam.js +++ b/mam.js @@ -6,6 +6,14 @@ function initMAM(connection) { 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.')); @@ -80,4 +88,42 @@ function initMAM(connection) { 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://prosody.im/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); + }); }