comparison 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
comparison
equal deleted inserted replaced
23:e99984564b17 24:6c620e9f7d2c
3 function initMAM(connection) { 3 function initMAM(connection) {
4 const prefs_default = document.getElementById('mam-prefs-default'); 4 const prefs_default = document.getElementById('mam-prefs-default');
5 const prefs_always = document.getElementById('mam-prefs-always'); 5 const prefs_always = document.getElementById('mam-prefs-always');
6 const prefs_never = document.getElementById('mam-prefs-never'); 6 const prefs_never = document.getElementById('mam-prefs-never');
7 const prefs_spinner = document.getElementById('mam-prefs-spinner'); 7 const prefs_spinner = document.getElementById('mam-prefs-spinner');
8
9 const mam_retrieve = document.getElementById('mam-retrieve');
10 const download_button = document.getElementById('mam-download');
11 const retrieve_spinner = document.getElementById('mam-retrieve-spinner');
12
13 const mam_data = [];
14
15 download_button.disabled = true;
8 16
9 const iq = $iq({type: 'get'}) 17 const iq = $iq({type: 'get'})
10 .c('prefs', {xmlns: NS.mam}); 18 .c('prefs', {xmlns: NS.mam});
11 connection.sendIQ(iq, onMAMPrefs, onMAMPrefsError.bind(null, 'query failed.')); 19 connection.sendIQ(iq, onMAMPrefs, onMAMPrefsError.bind(null, 'query failed.'));
12 displaySpinner(prefs_spinner); 20 displaySpinner(prefs_spinner);
78 function onPrefsError(iq) 86 function onPrefsError(iq)
79 { 87 {
80 console.log('Error while changing MAM preferences.'); 88 console.log('Error while changing MAM preferences.');
81 spinnerError(prefs_spinner); 89 spinnerError(prefs_spinner);
82 } 90 }
91
92 mam_retrieve.addEventListener('click', function (evt) {
93 connection.mam.query(null, {
94 onMessage: function (message, delay, id) {
95 console.log('Got a MAM message:', message, delay, id);
96 const result = document.createElementNS(NS.mam, 'result');
97 result.setAttributeNS(null, 'id', id);
98 const forwarded = document.createElementNS(NS.forward, 'forwarded');
99 forwarded.appendChild(delay);
100 forwarded.appendChild(message);
101 result.appendChild(forwarded);
102 mam_data.push(result);
103 },
104 onComplete: function () {
105 console.log('Received all messages, you can now download a backup of your archive.');
106 download_button.disabled = false;
107 spinnerOk(retrieve_spinner);
108 },
109 });
110 displaySpinner(retrieve_spinner);
111 });
112
113 download_button.addEventListener('click', function (evt) {
114 // TODO: extend XEP-0227 with MAM support.
115 const messages = document.createElementNS('https://prosody.im/protocol/pie-mam', 'message-archive');
116 for (let result of mam_data)
117 messages.appendChild(result);
118 const blob = new Blob([messages.outerHTML], {type: 'text/xml'});
119 const link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
120 link.hidden = false;
121 link.href = URL.createObjectURL(blob);
122 link.download = Strophe.getBareJidFromJid(connection.jid) + '-archive.xml';
123 console.log(link.download);
124 document.body.appendChild(link);
125 link.click();
126 URL.revokeObjectURL(link.href);
127 document.body.removeChild(link);
128 });
83 } 129 }