Welcome to mirror list, hosted at ThFree Co, Russian Federation.

calendars.js « store « src - github.com/nextcloud/tasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b392ba55d6ef011feba8818d3fc29ecb795593d8 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/**
 * Nextcloud - Tasks
 *
 * @author Raimund Schlüßler
 *
 * @copyright 2018 Raimund Schlüßler <raimund.schluessler@mailbox.org>
 *
 * @author John Molakvoæ
 *
 * @copyright 2018 John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @author Georg Ehrke
 *
 * @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
 *
 * @author Thomas Citharel <tcit@tcit.fr>
 *
 * @copyright 2018 Thomas Citharel <tcit@tcit.fr>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
'use strict'

import client from '../services/cdav'
import Task from '../models/task'
import { isParentInList, searchSubTasks } from './storeHelper'
import { findVTODObyState } from './cdav-requests'
import router from '../router'
import { detectColor, uidToHexColor } from '../utils/color'
import { mapCDavObjectToCalendarObject } from '../models/calendarObject'

import Vue from 'vue'

const calendarModel = {
	id: '',
	color: '',
	displayName: '',
	enabled: true,
	owner: '',
	shares: [],
	tasks: {},
	url: '',
	readOnly: false,
	dav: false,
	supportsEvents: true,
	supportsTasks: true,
	loadedCompleted: false,
	// Whether or not the calendar is shared with me
	isSharedWithMe: false,
	// Whether or not the calendar can be shared by me
	canBeShared: false,
	// The order of this calendar in the calendar-list
	order: 0,
}

const state = {
	calendars: [],
	trashBin: undefined,
	deletedCalendars: [],
	deletedCalendarObjects: [],
}

/**
 * Maps a dav collection to our calendar object model
 *
 * @param {object} calendar The calendar object from the cdav library
 * @param {object} currentUserPrincipal The principal model of the current user principal
 * @return {object}
 */
export function Calendar(calendar, currentUserPrincipal) {
	const owner = calendar.owner
	let isSharedWithMe = false
	if (!currentUserPrincipal) {
		// If the user is not authenticated, the calendar
		// will always be marked as shared with them
		isSharedWithMe = true
	} else {
		isSharedWithMe = (owner !== currentUserPrincipal.url)
	}
	const displayName = calendar.displayname || getCalendarUriFromUrl(calendar.url)
	// calendar.color can be set to anything on the server,
	// so make sure it's something that remotely looks like a color
	let color = detectColor(calendar.color)
	if (!color) {
		// As fallback if we don't know what color that is supposed to be
		color = uidToHexColor(displayName)
	}

	const shares = []
	if (!!currentUserPrincipal && Array.isArray(calendar.shares)) {
		for (const share of calendar.shares) {
			if (share.href === currentUserPrincipal.principalScheme) {
				continue
			}

			shares.push(mapDavShareeToSharee(share))
		}
	}

	const order = +calendar.order || 0

	return {
		// get last part of url
		id: calendar.url.split('/').slice(-2, -1)[0],
		displayName,
		color,
		order,
		enabled: calendar.enabled !== false,
		owner,
		readOnly: !calendar.isWriteable(),
		tasks: {},
		url: calendar.url,
		dav: calendar,
		shares,
		supportsEvents: calendar.components.includes('VEVENT'),
		supportsTasks: calendar.components.includes('VTODO'),
		loadedCompleted: false,
		isSharedWithMe,
		canBeShared: calendar.isShareable(),
	}
}

/**
 * Maps a dav collection to the sharee array
 *
 * @param {object} sharee The sharee object from the cdav library shares
 * @return {object}
 */
export function mapDavShareeToSharee(sharee) {
	const id = sharee.href.split('/').slice(-1)[0]
	let name = sharee['common-name']
		? sharee['common-name']
		: sharee.href

	if (sharee.href.startsWith('principal:principals/groups/') && name === sharee.href) {
		name = sharee.href.substr(28)
	}

	return {
		displayName: name,
		id,
		writeable: sharee.access[0].endsWith('read-write'),
		isGroup: sharee.href.startsWith('principal:principals/groups/'),
		isCircle: sharee.href.startsWith('principal:principals/circles/'),
		uri: sharee.href,
	}
}

/**
 * Gets the calendar uri from the url
 *
 * @param {string} url The url to get calendar uri from
 * @return {string}
 */
function getCalendarUriFromUrl(url) {
	if (url.endsWith('/')) {
		url = url.substring(0, url.length - 1)
	}

	return url.substring(url.lastIndexOf('/') + 1)
}

const getters = {

	/**
	 * Returns all calendars supporting VTODOs
	 *
	 * @param {object} state The store data
	 * @return {Array<Calendar>} The calendars supporting tasks
	 */
	getTaskCalendars: state => {
		return state.calendars.filter(calendar => {
			return calendar.supportsTasks
		})
	},

	/**
	 * Returns the calendars sorted alphabetically
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @return {Array<Calendar>} Array of the calendars sorted alphabetically
	 */
	getSortedCalendars: (state, getters) => {
		return getters.getTaskCalendars.sort(function(cal1, cal2) {
			const n1 = cal1.order
			const n2 = cal2.order
			return (n1 < n2) ? -1 : (n1 > n2) ? 1 : 0
		})
	},

	/**
	 * Returns the calendars sorted alphabetically
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @return {Array<Calendar>} Array of the calendars sorted alphabetically
	 */
	getSortedWritableCalendars: (state, getters) => {
		return getters.getTaskCalendars.filter(calendar => {
			return !calendar.readOnly
		})
			.sort(function(cal1, cal2) {
				const n1 = cal1.order
				const n2 = cal2.order
				return (n1 < n2) ? -1 : (n1 > n2) ? 1 : 0
			})
	},

	/**
	 * Returns the calendar with the given calendarId
	 *
	 * @param {object} state The store data
	 * @return {Calendar} The requested calendar
	 */
	getCalendarById: state =>
		/**
		 * @param {string} calendarId The id of the calendar
		 * @return {Calendar} The requested calendar
		 */
		(calendarId) => {
			const calendar = state.calendars.find(search => search.id === calendarId)
			return calendar
		},

	/**
	 * Returns the number of tasks in a calendar
	 *
	 * Tasks have to be
	 * - a root task
	 * - uncompleted
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @param {object} rootState The store root state
	 * @return {number} The number of tasks
	 */
	getCalendarCount: (state, getters, rootState) =>
		/**
		 * @param {string} calendarId The id of the requested calendar
		 * @return {number} The number of tasks
		 */
		(calendarId) => {
			const calendar = getters.getCalendarById(calendarId)
			let tasks = Object.values(calendar.tasks)
				.filter(task => {
					return task.closed === false && (!task.related || !isParentInList(task, calendar.tasks))
				})
			if (rootState.tasks.searchQuery) {
				tasks = tasks.filter(task => {
					if (task.matches(rootState.tasks.searchQuery)) {
						return true
					}
					// We also have to show tasks for which one sub(sub...)task matches.
					return searchSubTasks(task, rootState.tasks.searchQuery)
				})
			}
			return tasks.length
		},

	/**
	 * Returns the count of closed tasks in a calendar
	 *
	 * Tasks have to be
	 * - a root task
	 * - closed
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @return {number} The count of closed tasks in a calendar
	 */
	getCalendarCountClosed: (state, getters) =>
		/**
		 * @param {string} calendarId The id of the calendar in question
		 * @return {number} The count of closed tasks in a calendar
		 */
		(calendarId) => {
			const calendar = getters.getCalendarById(calendarId)
			return Object.values(calendar.tasks)
				.filter(task => {
					return task.closed === true && (!task.related || !isParentInList(task, calendar.tasks))
				}).length
		},

	/**
	 * Returns if a calendar name is already used by an other calendar
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @return {boolean} If a calendar name is already used
	 */
	isCalendarNameUsed: (state, getters) =>
		/**
		 * @param {string} name The name to check
		 * @param {string} id The id of the calendar to exclude
		 * @return {boolean} If a calendar name is already used
		 */
		(name, id) => {
			return getters.getTaskCalendars.some(calendar => {
				return (calendar.displayName === name && calendar.id !== id)
			})
		},

	/**
	 * Returns the current calendar
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @param {object} rootState The store root state
	 * @return {Calendar} The calendar by route
	 */
	getCalendarByRoute: (state, getters, rootState) => {
		if (rootState.route.params.collectionId) {
			return getters.getDefaultCalendar
		}
		return getters.getCalendarById(rootState.route.params.calendarId)
	},

	/**
	 * Returns the default calendar
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @param {object} rootState The store root state
	 * @return {Calendar} The default calendar
	 */
	getDefaultCalendar: (state, getters, rootState) => {
		const defaultCalendar = getters.getCalendarById(rootState.settings.settings.defaultCalendarId)
		// If the default calendar is read only we return the first calendar that is writable
		if (!defaultCalendar || defaultCalendar.readOnly) {
			return getters.getSortedCalendars.find(calendar => !calendar.readOnly) || getters.getSortedCalendars[0]
		}
		return defaultCalendar
	},

	hasTrashBin: (state) => {
		return state.trashBin !== undefined && state.trashBin.retentionDuration !== 0
	},

	trashBin: (state) => {
		return state.trashBin
	},

	/**
	 * List of deleted sorted calendars
	 *
	 * @param {object} state the store data
	 * @return {Array}
	 */
	sortedDeletedCalendars(state) {
		console.debug(state.deletedCalendars)
		return state.deletedCalendars
			.sort((a, b) => a.deletedAt - b.deletedAt)
	},

	/**
	 * List of deleted calendars objects
	 *
	 * @param {object} state the store data
	 * @return {Array}
	 */
	deletedCalendarObjects(state) {
		const calendarUriMap = {}
		state.calendars.forEach(calendar => {
			const withoutTrail = calendar.url.replace(/\/$/, '')
			const uri = withoutTrail.substr(withoutTrail.lastIndexOf('/') + 1)
			calendarUriMap[uri] = calendar
		})

		return state.deletedCalendarObjects.map(obj => ({
			calendar: calendarUriMap[obj.dav._props['{http://nextcloud.com/ns}calendar-uri']],
			...obj,
		}))
	},
}

const mutations = {

	/**
	 * Adds a calendar to the state
	 *
	 * @param {object} state The store data
	 * @param {Calendar} calendar The calendar to add
	 */
	addCalendar(state, calendar) {
		// extend the calendar to the default model
		calendar = Object.assign({}, calendarModel, calendar)
		// Only add the calendar if it is not already present
		if (state.calendars.some(cal => {
			return cal.id === calendar.id
		})) {
			return
		}
		state.calendars.push(calendar)
	},

	/**
	 * Delete calendar
	 *
	 * @param {object} state The store data
	 * @param {Calendar} calendar The calendar to delete
	 */
	deleteCalendar(state, calendar) {
		state.calendars.splice(state.calendars.indexOf(calendar), 1)
	},

	addTrashBin(state, { trashBin }) {
		state.trashBin = trashBin
	},

	/**
	 * Adds deleted calendar into state
	 *
	 * @param {object} state the store data
	 * @param {object} data destructuring object
	 * @param {object} data.calendar calendar the calendar to add
	 */
	addDeletedCalendar(state, { calendar }) {
		if (state.deletedCalendars.some(c => c.url === calendar.url)) {
			// This calendar is already known
			return
		}
		state.deletedCalendars.push(calendar)
	},

	/**
	 * Removes a deleted calendar
	 *
	 * @param {object} state the store data
	 * @param {object} data destructuring object
	 * @param {object} data.calendar the deleted calendar to remove
	 */
	removeDeletedCalendar(state, { calendar }) {
		state.deletedCalendars = state.deletedCalendars.filter(c => c !== calendar)
	},

	/**
	 * Removes a deleted calendar object
	 *
	 * @param {object} state the store data
	 * @param {object} data destructuring object
	 * @param {object} data.vobject the deleted calendar object to remove
	 */
	removeDeletedCalendarObject(state, { vobject }) {
		state.deletedCalendarObjects = state.deletedCalendarObjects.filter(vo => vo.id !== vobject.id)
	},

	/**
	 * Adds a deleted vobject into state
	 *
	 * @param {object} state the store data
	 * @param {object} data destructuring object
	 * @param {object} data.vobject the calendar vobject to add
	 */
	addDeletedCalendarObject(state, { vobject }) {
		if (state.deletedCalendarObjects.some(c => c.uri === vobject.uri)) {
			// This vobject is already known
			return
		}
		state.deletedCalendarObjects.push(vobject)
	},

	/**
	 * Toggles whether a calendar is enabled
	 *
	 * @param {object} context The store mutations
	 * @param {Calendar} calendar The calendar to toggle
	 */
	toggleCalendarEnabled(context, calendar) {
		calendar.enabled = !calendar.enabled
	},

	/**
	 * Changes the name and the color of a calendar
	 *
	 * @param {object} context The store mutations
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar to change
	 * @param {string} data.newName The new name of the calendar
	 * @param {string} data.newColor The new color of the calendar
	 */
	renameCalendar(context, { calendar, newName, newColor }) {
		calendar.displayName = newName
		calendar.color = newColor
	},

	/**
	 * Appends a list of tasks to a calendar
	 * and removes duplicates
	 *
	 * @param {object} state The store data
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar to add the tasks to
	 * @param {Task[]} data.tasks Array of tasks to append
	 */
	appendTasksToCalendar(state, { calendar, tasks }) {
		// Convert list into an array and remove duplicate
		calendar.tasks = tasks.reduce((list, task) => {
			if (list[task.uid]) {
				console.debug('Duplicate task overridden', list[task.uid], task)
			}
			Vue.set(list, task.uid, task)
			return list
		}, calendar.tasks)

	},

	/**
	 * Adds a task to a calendar and overwrites if duplicate uid
	 *
	 * @param {object} state The store data
	 * @param {Task} task The task to add
	 */
	addTaskToCalendar(state, task) {
		Vue.set(task.calendar.tasks, task.uid, task)
	},

	/**
	 * Deletes a task from its calendar
	 *
	 * @param {object} state The store data
	 * @param {Task} task The task to delete
	 */
	deleteTaskFromCalendar(state, task) {
		Vue.delete(task.calendar.tasks, task.uid)
	},

	/**
	 * Shares a calendar with a user or group
	 *
	 * @param {object} state The store data
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar
	 * @param {string} data.user The userId
	 * @param {string} data.displayName The displayName
	 * @param {string} data.uri The sharing principalScheme uri
	 * @param {boolean} data.isGroup Is this a group ?
	 * @param {boolean} data.isCircle Is this a circle?
	 */
	shareCalendar(state, { calendar, user, displayName, uri, isGroup, isCircle }) {
		calendar = state.calendars.find(search => search.id === calendar.id)
		const newSharee = {
			displayName,
			id: user,
			writeable: false,
			isGroup,
			isCircle,
			uri,
		}
		if (!calendar.shares.some((share) => share.uri === uri)) {
			calendar.shares.push(newSharee)
		}
	},

	/**
	 * Removes a sharee from calendar shares list
	 *
	 * @param {object} state The store data
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar
	 * @param {string} data.uri The sharee uri
	 */
	removeSharee(state, { calendar, uri }) {
		calendar = state.calendars.find(search => search.id === calendar.id)
		const shareIndex = calendar.shares.findIndex(sharee => sharee.uri === uri)
		calendar.shares.splice(shareIndex, 1)
	},

	/**
	 * Toggles sharee's writable permission
	 *
	 * @param {object} state The store data
	 * @param {object} data Destructuring object
	 * @param {object} data.calendar The calendar
	 * @param {string} data.uri The sharee uri
	 */
	updateShareeWritable(state, { calendar, uri }) {
		calendar = state.calendars.find(search => search.id === calendar.id)
		const sharee = calendar.shares.find(sharee => sharee.uri === uri)
		sharee.writeable = !sharee.writeable
	},

	/**
	 * Sets the sort order of a calendar
	 *
	 * @param {object} state The store data
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar
	 * @param {number} data.order The sort order
	 */
	setCalendarOrder(state, { calendar, order }) {
		Vue.set(calendar, 'order', order)
	},
}

const actions = {
	/**
	 * Retrieves and commits calendars
	 *
	 * @param {object} context The store object
	 * @param {object} context.commit The store mutations
	 * @param {object} context.state The store state
	 * @param {object} context.getters The store getters
	 * @return {Promise<Array>} The calendars
	 */
	async getCalendarsAndTrashBin({ commit, state, getters }) {
		let { calendars, trashBins } = await client.calendarHomes[0].findAllCalDAVCollectionsGrouped()
		calendars = calendars.map(calendar => {
			return Calendar(calendar, getters.getCurrentUserPrincipal)
		})

		calendars.forEach(calendar => {
			commit('addCalendar', calendar)
		})

		if (trashBins.length) {
			commit('addTrashBin', { trashBin: trashBins[0] })
		}

		return {
			calendars: state.calendars,
			trashBin: state.trashBin,
		}
	},

	/**
	 * Retrieve and commit deleted calendars
	 *
	 * @param {object} context The store object
	 * @param {object} context.commit The store mutations
	 * @return {Promise<Array>} the calendars
	 */
	async loadDeletedCalendars({ commit }) {
		const calendars = await client.calendarHomes[0].findAllDeletedCalendars()

		calendars.forEach(calendar => commit('addDeletedCalendar', { calendar }))
	},

	/**
	 * Retrieve and commit deleted calendar objects
	 *
	 * @param {object} context The store object
	 * @param {object} context.commit The store mutations
	 * @param {object} context.state The store state
	 */
	async loadDeletedCalendarObjects({ commit, state }) {
		const vobjects = await state.trashBin.findDeletedObjects()
		console.info('vobjects loaded', { vobjects })

		vobjects.forEach(vobject => {
			try {
				const calendarObject = mapCDavObjectToCalendarObject(vobject, undefined)
				commit('addDeletedCalendarObject', { vobject: calendarObject })
			} catch (error) {
				console.error('could not convert calendar object', vobject, error)
			}
		})
	},

	/**
	 * Appends a new calendar to array of existing calendars
	 *
	 * @param {object} context The store mutations
	 * @param {Calendar} calendar The calendar to append
	 * @return {Promise}
	 */
	async appendCalendar(context, calendar) {
		return client.calendarHomes[0].createCalendarCollection(calendar.displayName, calendar.color, ['VTODO'])
			.then((response) => {
				calendar = Calendar(response, context.getters.getCurrentUserPrincipal)
				context.commit('addCalendar', calendar)
				// Open the calendar
				router.push({ name: 'calendars', params: { calendarId: calendar.id } })
			})
			.catch((error) => { throw error })
	},

	/**
	 * Delete calendar
	 *
	 * @param {object} context The store mutations Current context
	 * @param {Calendar} calendar The calendar to delete
	 * @return {Promise}
	 */
	async deleteCalendar(context, calendar) {
		return calendar.dav.delete()
			.then((response) => {
				// Delete all the tasks from the store that belong to this calendar
				Object.values(calendar.tasks)
					.forEach(task => context.commit('deleteTask', task))
				// Then delete the calendar
				context.commit('deleteCalendar', calendar)
			})
			.catch((error) => { throw error })
	},

	/**
	 * Delete a calendar in the trash bin
	 *
	 * @param {object} context the store mutations Current context
	 * @param {object} data destructuring object
	 * @param {object} data.calendar the calendar to delete
	 * @return {Promise}
	 */
	async deleteCalendarPermanently(context, { calendar }) {
		await calendar.delete({
			'X-NC-CalDAV-No-Trashbin': 1,
		})

		context.commit('removeDeletedCalendar', { calendar })
	},

	async restoreCalendar({ commit, state }, { calendar }) {
		await state.trashBin.restore(calendar.url)

		commit('removeDeletedCalendar', { calendar })
	},

	async restoreCalendarObject({ commit, state, dispatch }, { vobject }) {

		// Restore the direct ancestor(s)
		await dispatch('restoreCalendarObjectAncestor', { vobject })

		// Restore the object itself
		await state.trashBin.restore(vobject.uri)

		// Clean up the data locally
		commit('removeDeletedCalendarObject', { vobject })

		// Restore all children
		await dispatch('restoreCalendarObjectChildren', { vobject })

		if (vobject.isTodo) {
			dispatch('getTasksFromCalendar', { calendar: vobject.calendar })
		}
	},

	async restoreCalendarObjectAncestor({ dispatch, commit }, { vobject }) {
		// Find the direct ancestor(s)
		const ancestors = state.deletedCalendarObjects.filter(v => {
			return v.uid === vobject.parent
		})
		// Restore the ancestor(s)
		await Promise.all(ancestors.map(async ancestor => {
			await dispatch('restoreCalendarObjectAncestor', { vobject: ancestor })

			// Restore the ancestor
			await state.trashBin.restore(ancestor.uri)
			// Clean up the data locally
			commit('removeDeletedCalendarObject', { vobject: ancestor })
		}))
	},

	async restoreCalendarObjectChildren({ state, dispatch, commit }, { vobject }) {
		// Find the children
		const children = state.deletedCalendarObjects.filter(v => {
			return v.parent === vobject.uid
		})
		// Restore the children
		await Promise.all(children.map(async child => {
			// Restore the child
			await state.trashBin.restore(child.uri)
			// Clean up the data locally
			commit('removeDeletedCalendarObject', { vobject: child })

			return await dispatch('restoreCalendarObjectChildren', { vobject: child })
		}))
	},

	/**
	 * Deletes a calendar-object permanently
	 *
	 * @param {object} context the store mutations
	 * @param {object} data destructuring object
	 * @param {object} data.vobject Calendar-object to delete
	 * @return {Promise<void>}
	 */
	async deleteCalendarObjectPermanently(context, { vobject }) {
		await vobject.dav.delete({
			'X-NC-CalDAV-No-Trashbin': 1,
		})

		context.commit('removeDeletedCalendarObject', { vobject })
	},

	/**
	 * Toggles whether a calendar is enabled
	 *
	 * @param {object} context The store mutations current context
	 * @param {Calendar} calendar The calendar to toggle
	 * @return {Promise}
	 */
	async toggleCalendarEnabled(context, calendar) {
		calendar.dav.enabled = !calendar.dav.enabled
		return calendar.dav.update()
			.then((response) => context.commit('toggleCalendarEnabled', calendar))
			.catch((error) => { throw error })
	},

	/**
	 * Changes the name and the color of a calendar
	 *
	 * @param {object} context The store mutations Current context
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar to change
	 * @param {string} data.newName The new name of the calendar
	 * @param {string} data.newColor The new color of the calendar
	 * @return {Promise}
	 */
	async changeCalendar(context, { calendar, newName, newColor }) {
		calendar.dav.displayname = newName
		calendar.dav.color = newColor
		return calendar.dav.update()
			.then((response) => context.commit('renameCalendar', { calendar, newName, newColor }))
			.catch((error) => { throw error })
	},

	/**
	 * Retrieves the tasks of the specified calendar
	 * and commits the results
	 *
	 * @param {object} context The store mutations
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar
	 * @param {string} data.completed Are the requested tasks completed
	 * @param {string} data.related The uid of the parent task
	 * @return {Promise}
	 */
	async getTasksFromCalendar(context, { calendar, completed = false, related = null }) {
		try {
			const response = await findVTODObyState(calendar, completed, related)
			if (response) {
				// If we loaded completed tasks, note that.
				if (completed) {
					calendar.loadedCompleted = true
				}
				// We don't want to lose the url information
				// so we need to parse one by one
				const tasks = response.map(item => {
					const task = new Task(item.data, calendar)
					Vue.set(task, 'dav', item)
					return task
				})

				// Initialize subtasks so we don't have to search for them on every change.
				// We do have to manually adjust this list when a task is added, deleted or moved.
				tasks.forEach(
					parent => {
						const subTasks = tasks.filter(task => {
							return task.related === parent.uid
						})

						// Convert list into an array and remove duplicate
						parent.subTasks = subTasks.reduce((list, task) => {
							if (list[task.uid]) {
								console.debug('Duplicate task overridden', list[task.uid], task)
							}
							Vue.set(list, task.uid, task)
							return list
						}, parent.subTasks)

						// In case we already have subtasks of this task in the store, add them as well.
						const subTasksInStore = context.getters.getTasksByParent(parent)
						subTasksInStore.forEach(
							subTask => {
								context.commit('addTaskToParent', { task: subTask, parent })
							}
						)

						// If necessary, add the tasks as subtasks to parent tasks already present in the store.
						if (!related) {
							const parentParent = context.getters.getTaskByUid(parent.related)
							context.commit('addTaskToParent', { task: parent, parent: parentParent })
						}
					}
				)

				// If the requested tasks are related to a task, add the tasks as subtasks
				if (related) {
					const parent = Object.values(calendar.tasks).find(search => search.uid === related)
					if (parent) {
						parent.loadedCompleted = true
						tasks.map(task => Vue.set(parent.subTasks, task.uid, task))
					}
				}

				context.commit('appendTasksToCalendar', { calendar, tasks })
				context.commit('appendTasks', tasks)
				return tasks
			}
		} catch (error) {
			// unrecoverable error, if no tasks were loaded,
			// remove the calendar
			// TODO: create a failed calendar state and show that there was an issue?
			context.commit('deleteCalendar', calendar)
			console.error(error)
		}
	},

	/**
	 * Removes a sharee from a calendar
	 *
	 * @param {object} context The store mutations Current context
	 * @param {object} data Destructuring object
	 * @param {object} data.calendar The calendar
	 * @param {string} data.uri The sharee uri
	 */
	async removeSharee(context, { calendar, uri }) {
		await calendar.dav.unshare(uri)
		context.commit('removeSharee', { calendar, uri })
	},

	/**
	 * Toggles permissions of calendar sharees writeable rights
	 *
	 * @param {object} context The store mutations Current context
	 * @param {object} data Destructuring object
	 * @param {object} data.calendar The calendar
	 * @param {string} data.uri The sharee uri
	 * @param {boolean} data.writeable The sharee permission
	 */
	async toggleShareeWritable(context, { calendar, uri, writeable }) {
		await calendar.dav.share(uri, writeable)
		context.commit('updateShareeWritable', { calendar, uri, writeable })
	},

	/**
	 * Shares a calendar with a user or a group
	 *
	 * @param {object} context The store mutations Current context
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar
	 * @param {string} data.user The userId
	 * @param {string} data.displayName The displayName
	 * @param {string} data.uri The sharing principalScheme uri
	 * @param {boolean} data.isGroup Is this a group ?
	 * @param {boolean} data.isCircle Is this a circle?
	 */
	async shareCalendar(context, { calendar, user, displayName, uri, isGroup, isCircle }) {
		// Share calendar with entered group or user
		await calendar.dav.share(uri)
		context.commit('shareCalendar', { calendar, user, displayName, uri, isGroup, isCircle })
	},

	/**
	 * Sets the sort order of a calendar
	 *
	 * @param {object} context The store context
	 * @param {object} data Destructuring object
	 * @param {Calendar} data.calendar The calendar to update
	 * @param {number} data.order The sort order
	 */
	async setCalendarOrder(context, { calendar, order }) {
		if (calendar.order === order) {
			return
		}

		context.commit('setCalendarOrder', { calendar, order })
		calendar.dav.order = order
		await calendar.dav.update()
	},
}

export default { state, getters, mutations, actions }