Started adding the API for querying archived messages.

This commit is contained in:
JC Brand 2015-07-10 17:26:54 +02:00
parent eeeaddbe04
commit b77d76b364
3 changed files with 256 additions and 11 deletions

View File

@ -160,6 +160,7 @@
Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user");
Strophe.addNamespace('REGISTER', 'jabber:iq:register');
Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');
Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
Strophe.addNamespace('XFORM', 'jabber:x:data');
// Add Strophe Statuses
@ -6106,6 +6107,47 @@
return _.map(jids, getWrappedChatBox);
}
},
'archive': {
'query': function (options, callback, errback) {
var date;
// Available options are jid, limit, start, end, after, before
if (typeof options == "function") {
callback = options;
errback = callback;
}
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
throw new Error('This server does not support XEP-0313, Message Archive Management');
}
var stanza = $iq({'type':'set'}).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':converse.connection.getUniqueId()});
if (typeof options != "undefined") {
stanza.c('x', {'xmlns':'jabber:x:data'})
.c('field', {'var':'FORM_TYPE'})
.c('value').t(Strophe.NS.MAM).up().up();
if (options.jid) {
stanza.c('field', {'var':'with'}).c('value').t(options.jid).up().up();
}
_.each(['start', 'end'], function (t) {
if (options[t]) {
date = moment(options[t]);
if (date.isValid()) {
stanza.c('field', {'var':t}).c('value').t(date.format()).up().up();
} else {
throw new TypeError('archive.query: invalid date provided for: '+t);
}
}
});
stanza.up();
if (options.limit) {
stanza.c('set', {'xmlns':Strophe.NS.RSM}).c('max').t(options.limit).up();
}
if (options.after) {
stanza.c('after').t(options.after).up();
}
}
converse.connection.sendIQ(stanza, callback, errback);
}
},
'rooms': {
'open': function (jids, nick) {
if (!nick) {

View File

@ -26,7 +26,7 @@ require.config({
"jquery-private": "src/jquery-private",
"jquery.browser": "components/jquery.browser/dist/jquery.browser",
"jquery.easing": "components/jquery-easing-original/index", // XXX: Only required for https://conversejs.org website
"moment": "components/momentjs/min/moment.min",
"moment": "components/momentjs/moment",
"strophe-base64": "components/strophejs/src/base64",
"strophe-bosh": "components/strophejs/src/bosh",
"strophe-core": "components/strophejs/src/core",

View File

@ -12,21 +12,224 @@
var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq;
var $pres = converse_api.env.$pres;
// See:
// https://xmpp.org/rfcs/rfc3921.html
// See: https://xmpp.org/rfcs/rfc3921.html
describe("Message Archive Management", $.proxy(function (mock, test_utils) {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
describe("The default preference", $.proxy(function (mock, test_utils) {
beforeEach(function () {
test_utils.closeAllChatBoxes();
test_utils.removeControlBox();
converse.roster.browserStorage._clear();
test_utils.initConverse();
test_utils.openControlBox();
test_utils.openContactsPanel();
describe("The archive.query API", $.proxy(function (mock, test_utils) {
it("can be used to query for all archived messages", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
converse_api.archive.query();
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'><query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'/></iq>");
});
it("can be used to query for all messages to/from a particular JID", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
converse_api.archive.query({'jid':'juliet@capulet.lit'});
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'>"+
"<x xmlns='jabber:x:data'>"+
"<field var='FORM_TYPE'>"+
"<value>urn:xmpp:mam:0</value>"+
"</field>"+
"<field var='with'>"+
"<value>juliet@capulet.lit</value>"+
"</field>"+
"</x>"+
"</query>"+
"</iq>"
);
});
it("can be used to query for all messages in a certain timespan", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
// Mock the browser's method for returning the timezone
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = function () {
return -120;
};
converse_api.archive.query({
'start': '2010-06-07T00:00:00Z',
'end': '2010-07-07T13:23:54Z'
});
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'>"+
"<x xmlns='jabber:x:data'>"+
"<field var='FORM_TYPE'>"+
"<value>urn:xmpp:mam:0</value>"+
"</field>"+
"<field var='start'>"+
"<value>2010-06-07T02:00:00+02:00</value>"+
"</field>"+
"<field var='end'>"+
"<value>2010-07-07T15:23:54+02:00</value>"+
"</field>"+
"</x>"+
"</query>"+
"</iq>"
);
// Restore
Date.prototype.getTimezoneOffset = getTimezoneOffset;
});
it("throws a TypeError if an invalid date is provided", function () {
expect(_.partial(converse_api.archive.query, {'start': 'not a real date'})).toThrow(
new TypeError('archive.query: invalid date provided for: start')
);
});
it("can be used to query for all messages after a certain time", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
// Mock the browser's method for returning the timezone
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = function () {
return -120;
};
converse_api.archive.query({'start': '2010-06-07T00:00:00Z'});
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'>"+
"<x xmlns='jabber:x:data'>"+
"<field var='FORM_TYPE'>"+
"<value>urn:xmpp:mam:0</value>"+
"</field>"+
"<field var='start'>"+
"<value>2010-06-07T02:00:00+02:00</value>"+
"</field>"+
"</x>"+
"</query>"+
"</iq>"
);
// Restore
Date.prototype.getTimezoneOffset = getTimezoneOffset;
});
it("can be used to query for a limited set of results", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
// Mock the browser's method for returning the timezone
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = function () {
return -120;
};
converse_api.archive.query({'start': '2010-06-07T00:00:00Z', 'limit':10});
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'>"+
"<x xmlns='jabber:x:data'>"+
"<field var='FORM_TYPE'>"+
"<value>urn:xmpp:mam:0</value>"+
"</field>"+
"<field var='start'>"+
"<value>2010-06-07T02:00:00+02:00</value>"+
"</field>"+
"</x>"+
"<set xmlns='http://jabber.org/protocol/rsm'>"+
"<max>10</max>"+
"</set>"+
"</query>"+
"</iq>"
);
// Restore
Date.prototype.getTimezoneOffset = getTimezoneOffset;
});
it("can be used to page through results", function () {
var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
// Mock the browser's method for returning the timezone
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = function () {
return -120;
};
converse_api.archive.query({
'start': '2010-06-07T00:00:00Z',
'after': '09af3-cc343-b409f',
'limit':10
});
var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'>"+
"<x xmlns='jabber:x:data'>"+
"<field var='FORM_TYPE'>"+
"<value>urn:xmpp:mam:0</value>"+
"</field>"+
"<field var='start'>"+
"<value>2010-06-07T02:00:00+02:00</value>"+
"</field>"+
"</x>"+
"<set xmlns='http://jabber.org/protocol/rsm'>"+
"<max>10</max>"+
"<after>09af3-cc343-b409f</after>"+
"</set>"+
"</query>"+
"</iq>"
);
// Restore
Date.prototype.getTimezoneOffset = getTimezoneOffset;
});
}, converse, mock, test_utils));
describe("The default preference", $.proxy(function (mock, test_utils) {
it("is set once server support for MAM has been confirmed", function () {
var sent_stanza, IQ_id;