summaryrefslogtreecommitdiffstats
path: root/js/dav/lib/webdav.js
blob: cb2f0410cede9c3b54b037285ecba5918a872063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import co from 'co';

import fuzzyUrlEquals from './fuzzy_url_equals';
import * as ns from './namespace';
import * as request from './request';

let debug = require('./debug')('dav:webdav');

/**
 * @param {String} objectUrl url for webdav object.
 * @param {String} objectData webdav object data.
 */
export function createObject(objectUrl, objectData, options) {
  var req = request.basic({ method: 'PUT', data: objectData });
  return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
}

export function updateObject(objectUrl, objectData, etag, options) {
  var req = request.basic({ method: 'PUT', data: objectData, etag: etag });
  return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
}

export function deleteObject(objectUrl, etag, options) {
  var req = request.basic({ method: 'DELETE', etag: etag });
  return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
}

export function syncCollection(collection, options) {
  let syncMethod;
  if ('syncMethod' in options) {
    syncMethod = options.syncMethod;
  } else if (collection.reports &&
             collection.reports.indexOf('syncCollection') !== -1) {
    syncMethod = 'webdav';
  } else {
    syncMethod = 'basic';
  }

  if (syncMethod === 'webdav') {
    debug('rfc 6578 sync.');
    return options.webdavSync(collection, options);
  } else {
    debug('basic sync.');
    return options.basicSync(collection, options);
  }
}

export function updateProperties(objectUrl, options) {
  var req = request.proppatch({
      props: options.props
  });
  return options.xhr.send(req, objectUrl, { sandbox: options.sandbox });
}

export function createCollection(collectionUrl, options) {
  var req = request.mkcol({
	  props: options.props
  });
  return options.xhr.send(req, collectionUrl, { sandbox: options.sandbox });
}

export function deleteCollection(collectionUrl, options) {
  var req = request.basic({ method: 'DELETE', data: objectData });
  return options.xhr.send(req, collectionUrl, { sandbox: options.sandbox });
}

export function moveCollection(collectionUrl, options) {
  var req = request.basic({ method: 'MOVE', overwrite: 'F', destination: options.destination, data:objectData });
  return options.xhr.send(req, collectionUrl, { sandbox: options.sandbox });
}

/**
 * @param {dav.DAVCollection} collection to fetch report set for.
 */
export let supportedReportSet = co.wrap(function *(collection, options) {
  debug('Checking supported report set for collection at ' + collection.url);
  var req = request.propfind({
    props: [ { name: 'supported-report-set', namespace: ns.DAV } ],
    depth: 1,
    mergeResponses: true
  });

  let response = yield options.xhr.send(req, collection.url, {
    sandbox: options.sandbox
  });

  return response.props.supportedReportSet;
});

export let isCollectionDirty = co.wrap(function *(collection, options) {
  if (!collection.ctag) {
    debug('Missing ctag.');
    return false;
  }

  debug('Fetch remote getctag prop.');
  var req = request.propfind({
    props: [ { name: 'getctag', namespace: ns.CALENDAR_SERVER } ],
    depth: 0
  });

  let responses = yield options.xhr.send(req, collection.account.homeUrl, {
    sandbox: options.sandbox
  });

  let response = responses.filter(response => {
    // Find the response that corresponds to the parameter collection.
    return fuzzyUrlEquals(collection.url, response.href);
  })[0];

  if (!response) {
    throw new Error('Could not find collection on remote. Was it deleted?');
  }

  debug('Check whether cached ctag matches remote.');
  return collection.ctag !== response.props.getctag;
});