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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
import curry from 'lodash/fp/curry'
import ical from 'ical.js'
import { getClient } from '../dav/client'
import Axios from '@nextcloud/axios'
import Logger from '../logger'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { uidToHexColor } from '../util/calendarColor'
const canWrite = (properties) => {
let acls = properties?.acl?.ace
if (!acls) {
return false
}
// There might only be one ACL, not a list
if (!Array.isArray(acls)) {
acls = [acls]
}
for (const acl of acls) {
if (acl.grant?.privilege?.write !== undefined) {
return true
}
}
return false
}
const getCalendarData = (calendar) => {
return {
displayname: calendar.props.displayname,
order: calendar.props['calendar-order'],
components: {
vevent: true, // check if VEVENT exists in props['supported-calendar-component-set'].comps
},
writable: canWrite(calendar.props),
url: generateRemoteUrl(`dav/calendars/${getCurrentUser().uid}${calendar.filename}/`),
color: calendar.props['calendar-color'] ?? uidToHexColor(calendar.props.displayname ?? ''),
}
}
/**
* @returns {Promise}
*/
export const getUserCalendars = async() => {
const response = await getClient('calendars')
.getDirectoryContents('/', {
data: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:aapl="http://apple.com/ns/ical/" xmlns:oc="http://owncloud.org/ns" xmlns:cs="http://calendarserver.org/ns/">
<d:prop>
<d:displayname />
<c:calendar-description />
<c:calendar-timezone />
<aapl:calendar-order />
<aapl:calendar-color />
<c:supported-calendar-component-set />
<oc:calendar-enabled />
<d:acl />
<d:owner />
<oc:invite />
</d:prop>
</d:propfind>`,
details: true,
})
return response.data
.map(getCalendarData)
.filter(props => props.components.vevent && props.writable === true)
}
const getRandomString = () => {
let str = ''
for (let i = 0; i < 7; i++) {
str += Math.random().toString(36).substring(7)
}
return str
}
const createICalElement = () => {
const root = new ical.Component(['vcalendar', [], []])
root.updatePropertyWithValue('prodid', '-//' + OC.theme.name + ' Mail')
return root
}
const splitCalendar = (data) => {
const timezones = []
const allObjects = {}
const jCal = ical.parse(data)
const components = new ical.Component(jCal)
const vtimezones = components.getAllSubcomponents('vtimezone')
vtimezones.forEach((vtimezone) => timezones.push(vtimezone))
const componentNames = ['vevent', 'vjournal', 'vtodo']
componentNames.forEach((componentName) => {
const vobjects = components.getAllSubcomponents(componentName)
allObjects[componentName] = {}
vobjects.forEach((vobject) => {
const uid = vobject.getFirstPropertyValue('uid')
allObjects[componentName][uid] = allObjects[componentName][uid] || []
allObjects[componentName][uid].push(vobject)
})
})
const split = []
componentNames.forEach((componentName) => {
split[componentName] = []
for (const objectsId in allObjects[componentName]) {
const objects = allObjects[componentName][objectsId]
const component = createICalElement()
timezones.forEach(component.addSubcomponent.bind(component))
for (const objectId in objects) {
component.addSubcomponent(objects[objectId])
}
split[componentName].push(component.toString())
}
})
return {
name: components.getFirstPropertyValue('x-wr-calname'),
color: components.getFirstPropertyValue('x-apple-calendar-color'),
split,
}
}
/**
* @param {String} url the url
* @param {Object} data the data
* @returns {Promise}
*/
export const importCalendarEvent = curry((url, data) => {
Logger.debug('importing events into calendar', {
url,
data,
})
const promises = []
const file = splitCalendar(data)
const components = ['vevent', 'vjournal', 'vtodo']
components.forEach((componentName) => {
for (const componentId in file.split[componentName]) {
const component = file.split[componentName][componentId]
Logger.info('importing event component', { component })
promises.push(
Promise.resolve(
Axios.put(url + getRandomString() + '.ics', component, {
headers: {
'Content-Type': 'text/calendar; charset=utf-8',
},
})
)
)
}
})
return Promise.all(promises)
})
|