comparison strophe.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
children 599b324fac2b
comparison
equal deleted inserted replaced
23:e99984564b17 24:6c620e9f7d2c
1 /* XEP-0313: Message Archive Management
2 * Copyright (C) 2012 Kim Alvefur
3 * Copyright (C) 2018 Emmanuel Gil Peyrot
4 *
5 * This file is MIT/X11 licensed. Please see the
6 * LICENSE.txt file in the source package for more information.
7 *
8 * Modified by: Chris Tunbridge (github.com/Destreyf/)
9 * Updated to support v0.3 of the XMPP XEP-0313 standard
10 * http://xmpp.org/extensions/xep-0313.html
11 *
12 */
13 //import { $iq, Strophe } from 'strophe.js';
14
15 Strophe.addConnectionPlugin('mam', {
16 _c: null,
17 _p: [ 'with', 'start', 'end' ],
18 init: function (conn) {
19 this._c = conn;
20 Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
21 Strophe.addNamespace('Forward', 'urn:xmpp:forward:0');
22 },
23 query: function (jid, options) {
24 var _c = this._c;
25 var _p = this._p;
26 var attr = {
27 type:'set',
28 to:jid
29 };
30 options = options || {};
31 var queryid = options.queryid;
32 if (queryid) {
33 delete options.queryid;
34 } else {
35 queryid = _c.getUniqueId();
36 }
37 var iq = $iq(attr).c('query', {xmlns: Strophe.NS.MAM, queryid: queryid}).c('x',{xmlns:'jabber:x:data', type:'submit'});
38
39 iq.c('field',{var:'FORM_TYPE', type:'hidden'}).c('value').t(Strophe.NS.MAM).up().up();
40 for (var i = 0; i < _p.length; i++) {
41 var pn = _p[i];
42 var p = options[pn];
43 delete options[pn];
44 if (p) {
45 iq.c('field',{var:pn}).c('value').t(p).up().up();
46 }
47 }
48 iq.up();
49
50 var onMessage = options.onMessage;
51 delete options.onMessage;
52 var onComplete = options.onComplete;
53 delete options.onComplete;
54 iq.cnode(new Strophe.RSM(options).toXML());
55
56 var handler = _c.addHandler(function (message) {
57 // TODO: check the emitter too!
58 var result = message.firstChild;
59 if (!result || result.namespaceURI !== Strophe.NS.MAM || result.localName !== 'result' || result.getAttributeNS(null, 'queryid') !== queryid)
60 return true;
61 var id = result.getAttributeNS(null, 'id');
62 var forwarded = result.firstChild;
63 if (!forwarded || forwarded.namespaceURI !== Strophe.NS.Forward || forwarded.localName !== 'forwarded')
64 return true;
65 var delay = null;
66 var childMessage = null;
67 for (var child of forwarded.childNodes.values()) {
68 if (child.namespaceURI === 'urn:xmpp:delay' && child.localName === 'delay' && delay === null)
69 delay = child;
70 else if (child.namespaceURI === 'jabber:client' && child.localName === 'message' && childMessage === null)
71 childMessage = child;
72 }
73 if (childMessage !== null && delay !== null)
74 onMessage(childMessage, delay, id);
75 return true;
76 }, Strophe.NS.MAM, 'message', null);
77 return _c.sendIQ(iq, function(){
78 _c.deleteHandler(handler);
79 onComplete.apply(this, arguments);
80 });
81 }
82 });