Skip to content

Commit

Permalink
Merge pull request #39 from nextcloud/feature/noid/addressbook-multig…
Browse files Browse the repository at this point in the history
…et-and-download

allow to export data from multiget
  • Loading branch information
skjnldsv authored Nov 5, 2018
2 parents 5649eea + 415950a commit b211b53
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/dist.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dist.js.map

Large diffs are not rendered by default.

49 changes: 43 additions & 6 deletions src/models/addressBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,48 @@ export class AddressBook extends davCollectionShareable(DavCollection) {
return [];
}

const headers = {
'Depth': '1'
};
const body = this._buildMultiGetBody(hrefs, prop);
const response = await this._request.report(this.url, headers, body);
return super._handleMultiStatusResponse(response, AddressBook._isRetrievalPartial(prop));
}

/**
* sends an addressbook multiget query as defined in
* https://tools.ietf.org/html/rfc6352#section-8.7
* and requests a download of the result
*
* @param {String[]} hrefs
* @param {Object[]} prop
* @returns {Promise<{Object}>}
* @property {String|Object} body
* @property {Number} status
* @property {XMLHttpRequest} xhr
*/
async addressbookMultigetExport(hrefs = [], prop) {
debug('sending an addressbook-multiget request and request download');

if (hrefs.length === 0) {
return '';
}

const headers = {
'Depth': '1'
};
const body = this._buildMultiGetBody(hrefs, prop);
return this._request.report(this.url + '?export', headers, body);
}

/**
*
* @param {String[]} hrefs
* @param {Object[]} prop
* @returns String
* @private
*/
_buildMultiGetBody(hrefs, prop) {
const [skeleton] = XMLUtility.getRootSkeleton(
[NS.IETF_CARDDAV, 'addressbook-multiget']
);
Expand All @@ -215,12 +257,7 @@ export class AddressBook extends davCollectionShareable(DavCollection) {
});
});

const headers = {
'Depth': '1'
};
const body = XMLUtility.serialize(skeleton);
const response = await this._request.report(this.url, headers, body);
return super._handleMultiStatusResponse(response, AddressBook._isRetrievalPartial(prop));
return XMLUtility.serialize(skeleton);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/unit/models/addressBookTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ describe('Address book model', () => {
fail('AddressBook addressbook-multiget was not supposed to fail');
});
});

it('should send an addressbook-multiget and request export of data', () => {
const parent = jasmine.createSpyObj('DavCollection', ['findAll', 'findAllByFilter', 'find',
'createCollection', 'createObject', 'update', 'delete', 'isReadable', 'isWriteable']);
const request = jasmine.createSpyObj('Request', ['propFind', 'put', 'delete', 'report', 'pathname']);
const url = '/foo/bar/folder';
const props = returnDefaultProps();

request.report.and.callFake(() => {
return Promise.resolve({
status: 200,
body: 'RAW DATA',
xhr: null
});
});

request.pathname.and.callFake((p) => p);

const addressbook = new AddressBook(parent, request, url, props);
return addressbook.addressbookMultigetExport(['/foo/bar/folder/a', '/foo/bar/folder/b']).then((res) => {
expect(res.status).toEqual(200);
expect(res.body).toEqual('RAW DATA');

expect(request.report).toHaveBeenCalledTimes(1);
expect(request.report).toHaveBeenCalledWith('/foo/bar/folder/?export', { Depth: '1' },
'<x0:addressbook-multiget xmlns:x0="urn:ietf:params:xml:ns:carddav"><x1:prop xmlns:x1="DAV:"><x1:getcontenttype/><x1:getetag/><x1:resourcetype/><x1:displayname/><x1:owner/><x1:resourcetype/><x1:sync-token/><x1:current-user-privilege-set/><x1:getcontenttype/><x1:getetag/><x1:resourcetype/><x0:address-data/></x1:prop><x1:href xmlns:x1="DAV:">/foo/bar/folder/a</x1:href><x1:href xmlns:x1="DAV:">/foo/bar/folder/b</x1:href></x0:addressbook-multiget>');
}).catch(() => {
fail('AddressBook addressbook-multiget was not supposed to fail');
});
});
});

function returnDefaultProps() {
Expand Down

0 comments on commit b211b53

Please sign in to comment.