MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / smartCollectionSync

Function smartCollectionSync

lib/models/dav.js:9408–9537  ·  view source on GitHub ↗
(params)

Source from the content-addressed store, hash-verified

9406};
9407/** remote collection to local */
9408const smartCollectionSync = async (params) => {
9409 var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
9410 const { collection, method, headers, headersToExclude, account, detailedResult } = params;
9411 const requiredFields = ['accountType', 'homeUrl'];
9412 if (!account || !hasFields(account, requiredFields)) {
9413 if (!account) {
9414 throw new Error('no account for smartCollectionSync');
9415 }
9416 throw new Error(`account must have ${findMissingFieldNames(account, requiredFields)} before smartCollectionSync`);
9417 }
9418 const syncMethod = method !== null && method !== void 0 ? method : (((_a = collection.reports) === null || _a === void 0 ? void 0 : _a.includes('syncCollection')) ? 'webdav' : 'basic');
9419 debug$4(`smart collection sync with type ${account.accountType} and method ${syncMethod}`);
9420 if (syncMethod === 'webdav') {
9421 const result = await syncCollection({
9422 url: collection.url,
9423 props: {
9424 [`${DAVNamespaceShort.DAV}:getetag`]: {},
9425 [`${account.accountType === 'caldav' ? DAVNamespaceShort.CALDAV : DAVNamespaceShort.CARDDAV}:${account.accountType === 'caldav' ? 'calendar-data' : 'address-data'}`]: {},
9426 [`${DAVNamespaceShort.DAV}:displayname`]: {},
9427 },
9428 syncLevel: 1,
9429 syncToken: collection.syncToken,
9430 headers: excludeHeaders(headers, headersToExclude),
9431 });
9432 const objectResponses = result.filter((r) => {
9433 var _a;
9434 const extName = account.accountType === 'caldav' ? '.ics' : '.vcf';
9435 return ((_a = r.href) === null || _a === void 0 ? void 0 : _a.slice(-4)) === extName;
9436 });
9437 const changedObjectUrls = objectResponses.filter((o) => o.status !== 404).map((r) => r.href);
9438 const deletedObjectUrls = objectResponses.filter((o) => o.status === 404).map((r) => r.href);
9439 const multiGetObjectResponse = changedObjectUrls.length
9440 ? ((_c = (await ((_b = collection === null || collection === void 0 ? void 0 : collection.objectMultiGet) === null || _b === void 0 ? void 0 : _b.call(collection, {
9441 url: collection.url,
9442 props: {
9443 [`${DAVNamespaceShort.DAV}:getetag`]: {},
9444 [`${account.accountType === 'caldav'
9445 ? DAVNamespaceShort.CALDAV
9446 : DAVNamespaceShort.CARDDAV}:${account.accountType === 'caldav' ? 'calendar-data' : 'address-data'}`]: {},
9447 },
9448 objectUrls: changedObjectUrls,
9449 depth: '1',
9450 headers: excludeHeaders(headers, headersToExclude),
9451 })))) !== null && _c !== void 0 ? _c : [])
9452 : [];
9453 const remoteObjects = multiGetObjectResponse.map((res) => {
9454 var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
9455 return {
9456 url: (_a = res.href) !== null && _a !== void 0 ? _a : '',
9457 etag: (_b = res.props) === null || _b === void 0 ? void 0 : _b.getetag,
9458 data: (account === null || account === void 0 ? void 0 : account.accountType) === 'caldav'
9459 ? ((_e = (_d = (_c = res.props) === null || _c === void 0 ? void 0 : _c.calendarData) === null || _d === void 0 ? void 0 : _d._cdata) !== null && _e !== void 0 ? _e : (_f = res.props) === null || _f === void 0 ? void 0 : _f.calendarData)
9460 : ((_j = (_h = (_g = res.props) === null || _g === void 0 ? void 0 : _g.addressData) === null || _h === void 0 ? void 0 : _h._cdata) !== null && _j !== void 0 ? _j : (_k = res.props) === null || _k === void 0 ? void 0 : _k.addressData),
9461 };
9462 });
9463 const localObjects = (_d = collection.objects) !== null && _d !== void 0 ? _d : [];
9464 // no existing url
9465 const created = remoteObjects.filter((o) => localObjects.every((lo) => !urlContains(lo.url, o.url)));

Callers 1

syncCalendarsFunction · 0.85

Calls 6

hasFieldsFunction · 0.85
findMissingFieldNamesFunction · 0.85
syncCollectionFunction · 0.85
excludeHeadersFunction · 0.85
urlContainsFunction · 0.85
isCollectionDirtyFunction · 0.85

Tested by

no test coverage detected