|
@@ -6,14 +6,14 @@
|
|
|
* Some code taken from the Strophe RSM plugin, licensed under the MIT License
|
|
|
* Copyright 2006-2017 Strophe (https://github.com/strophe/strophejs)
|
|
|
*/
|
|
|
-import converse from './api/public.js';
|
|
|
-import pick from 'lodash-es/pick';
|
|
|
+import converse from "./api/public.js";
|
|
|
+import pick from "lodash-es/pick";
|
|
|
|
|
|
const { Strophe, $build } = converse.env;
|
|
|
|
|
|
-Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
|
|
|
+Strophe.addNamespace("RSM", "http://jabber.org/protocol/rsm");
|
|
|
|
|
|
-const RSM_QUERY_PARAMETERS = ['after', 'before', 'index', 'max'];
|
|
|
+const RSM_QUERY_PARAMETERS = ["after", "before", "index", "max"];
|
|
|
|
|
|
const toNumber = (v) => Number(v);
|
|
|
const toString = (v) => v.toString();
|
|
@@ -25,35 +25,32 @@ export const RSM_TYPES = {
|
|
|
first: toString,
|
|
|
index: toNumber,
|
|
|
last: toString,
|
|
|
- max: toNumber
|
|
|
+ max: toNumber,
|
|
|
};
|
|
|
|
|
|
-const isUndefined = (x) => typeof x === 'undefined';
|
|
|
-
|
|
|
+const isUndefined = (x) => typeof x === "undefined";
|
|
|
|
|
|
// This array contains both query attributes and response attributes
|
|
|
export const RSM_ATTRIBUTES = Object.keys(RSM_TYPES);
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Instances of this class are used to page through query results according to XEP-0059 Result Set Management
|
|
|
* @class RSM
|
|
|
*/
|
|
|
export class RSM {
|
|
|
-
|
|
|
- static getQueryParameters (options={}) {
|
|
|
+ static getQueryParameters(options = {}) {
|
|
|
return pick(options, RSM_QUERY_PARAMETERS);
|
|
|
}
|
|
|
|
|
|
- static parseXMLResult (set) {
|
|
|
+ static parseXMLResult(set) {
|
|
|
const result = {};
|
|
|
for (var i = 0; i < RSM_ATTRIBUTES.length; i++) {
|
|
|
const attr = RSM_ATTRIBUTES[i];
|
|
|
const elem = set.getElementsByTagName(attr)[0];
|
|
|
if (!isUndefined(elem) && elem !== null) {
|
|
|
result[attr] = RSM_TYPES[attr](Strophe.getText(elem));
|
|
|
- if (attr == 'first') {
|
|
|
- result.index = RSM_TYPES['index'](elem.getAttribute('index'));
|
|
|
+ if (attr == "first") {
|
|
|
+ result.index = RSM_TYPES["index"](elem.getAttribute("index"));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -65,7 +62,7 @@ export class RSM {
|
|
|
* @param { Object } options - Configuration options
|
|
|
* @constructor
|
|
|
*/
|
|
|
- constructor (options={}) {
|
|
|
+ constructor(options = {}) {
|
|
|
this.query = RSM.getQueryParameters(options);
|
|
|
this.result = options.xml ? RSM.parseXMLResult(options.xml) : {};
|
|
|
}
|
|
@@ -76,18 +73,24 @@ export class RSM {
|
|
|
* that are set on this RSM instance.
|
|
|
* @returns {Element}
|
|
|
*/
|
|
|
- toXML () {
|
|
|
- const xml = $build('set', {xmlns: Strophe.NS.RSM});
|
|
|
- const reducer = (xml, a) => !isUndefined(this.query[a]) ? xml.c(a).t((this.query[a] || '').toString()).up() : xml;
|
|
|
+ toXML() {
|
|
|
+ const xml = $build("set", { xmlns: Strophe.NS.RSM });
|
|
|
+ const reducer = (xml, a) =>
|
|
|
+ !isUndefined(this.query[a])
|
|
|
+ ? xml
|
|
|
+ .c(a)
|
|
|
+ .t((this.query[a] || "").toString())
|
|
|
+ .up()
|
|
|
+ : xml;
|
|
|
return RSM_QUERY_PARAMETERS.reduce(reducer, xml).tree();
|
|
|
}
|
|
|
|
|
|
- next (max, before) {
|
|
|
+ next(max, before) {
|
|
|
const options = Object.assign({}, this.query, { after: this.result.last, before, max });
|
|
|
return new RSM(options);
|
|
|
}
|
|
|
|
|
|
- previous (max, after) {
|
|
|
+ previous(max, after) {
|
|
|
const options = Object.assign({}, this.query, { after, before: this.result.first, max });
|
|
|
return new RSM(options);
|
|
|
}
|