comparison strophe.mam.js @ 36:599b324fac2b

Support querying more than a single page of MAM results.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 24 Dec 2018 15:45:14 +0100
parents 6c620e9f7d2c
children
comparison
equal deleted inserted replaced
35:084202870d76 36:599b324fac2b
32 if (queryid) { 32 if (queryid) {
33 delete options.queryid; 33 delete options.queryid;
34 } else { 34 } else {
35 queryid = _c.getUniqueId(); 35 queryid = _c.getUniqueId();
36 } 36 }
37 var iq = $iq(attr).c('query', {xmlns: Strophe.NS.MAM, queryid: queryid}).c('x',{xmlns:'jabber:x:data', type:'submit'}); 37 var baseIq = $iq(attr).c('query', {xmlns: Strophe.NS.MAM, queryid: queryid}).c('x',{xmlns:'jabber:x:data', type:'submit'});
38 38
39 iq.c('field',{var:'FORM_TYPE', type:'hidden'}).c('value').t(Strophe.NS.MAM).up().up(); 39 baseIq.c('field',{var:'FORM_TYPE', type:'hidden'}).c('value').t(Strophe.NS.MAM).up().up();
40 for (var i = 0; i < _p.length; i++) { 40 for (var i = 0; i < _p.length; i++) {
41 var pn = _p[i]; 41 var pn = _p[i];
42 var p = options[pn]; 42 var p = options[pn];
43 delete options[pn]; 43 delete options[pn];
44 if (p) { 44 if (p) {
45 iq.c('field',{var:pn}).c('value').t(p).up().up(); 45 baseIq.c('field',{var:pn}).c('value').t(p).up().up();
46 } 46 }
47 } 47 }
48 iq.up(); 48 baseIq.up();
49 49
50 var onMessage = options.onMessage; 50 var onMessage = options.onMessage;
51 delete options.onMessage; 51 delete options.onMessage;
52 var onComplete = options.onComplete; 52 var onComplete = options.onComplete;
53 delete options.onComplete; 53 delete options.onComplete;
54 iq.cnode(new Strophe.RSM(options).toXML()); 54
55 var iq = Strophe.copyElement(baseIq.tree());
56 iq.firstChild.appendChild(new Strophe.RSM(options).toXML());
55 57
56 var handler = _c.addHandler(function (message) { 58 var handler = _c.addHandler(function (message) {
57 // TODO: check the emitter too! 59 // TODO: check the emitter too!
58 var result = message.firstChild; 60 var result = message.firstChild;
59 if (!result || result.namespaceURI !== Strophe.NS.MAM || result.localName !== 'result' || result.getAttributeNS(null, 'queryid') !== queryid) 61 if (!result || result.namespaceURI !== Strophe.NS.MAM || result.localName !== 'result' || result.getAttributeNS(null, 'queryid') !== queryid)
72 } 74 }
73 if (childMessage !== null && delay !== null) 75 if (childMessage !== null && delay !== null)
74 onMessage(childMessage, delay, id); 76 onMessage(childMessage, delay, id);
75 return true; 77 return true;
76 }, Strophe.NS.MAM, 'message', null); 78 }, Strophe.NS.MAM, 'message', null);
77 return _c.sendIQ(iq, function(){ 79 function onIq(result_iq){
78 _c.deleteHandler(handler); 80 var fin = result_iq.firstChild;
79 onComplete.apply(this, arguments); 81 if (!fin || fin.namespaceURI !== Strophe.NS.MAM || fin.localName !== 'fin' || fin.getAttributeNS(null, 'queryid') !== queryid) {
80 }); 82 console.log('Invalid <fin/> result received in iq:', result_iq);
83 return;
84 }
85 var complete = fin.getAttributeNS(null, 'complete');
86 if (complete === 'true') {
87 // This is the last page, cancel the handler and call the user callback.
88 _c.deleteHandler(handler);
89 return onComplete(result_iq);
90 }
91 // This wasn’t the last page, query the one after the last id we received.
92 var set = fin.firstChild;
93 if (!set || set.namespaceURI !== Strophe.NS.RSM || set.localName !== 'set') {
94 console.log('Invalid <set/> result received in iq:', result_iq);
95 return;
96 }
97 // TODO: is there really nothing better than this to get a direct child in DOM?
98 var last = fin.getElementsByTagNameNS(Strophe.NS.RSM, 'last');
99 if (!last) {
100 console.log('No <last/> in <set/>:', result_iq);
101 return;
102 }
103 options.after = last[0].textContent;
104 var iq = Strophe.copyElement(baseIq.tree());
105 iq.firstChild.appendChild(new Strophe.RSM(options).toXML());
106 _c.sendIQ(iq, onIq);
107 }
108 return _c.sendIQ(iq, onIq);
81 } 109 }
82 }); 110 });