-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathtasks.js
1440 lines (1342 loc) · 40.7 KB
/
tasks.js
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Nextcloud - Tasks
*
* @author Raimund Schlüßler
*
* @copyright 2018 Raimund Schlüßler <[email protected]>
*
* 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 { Calendar } from './calendars.js'
import { findVTODObyUid } from './cdav-requests.js'
import { isParentInList, momentToICALTime } from './storeHelper.js'
import SyncStatus from '../models/syncStatus.js'
import Task from '../models/task.js'
import router from '../router.js'
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { translate as t } from '@nextcloud/l10n'
import moment from '@nextcloud/moment'
import ICAL from 'ical.js'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
tasks: {},
searchQuery: '',
deletedTasks: {},
deleteInterval: null,
}
const getters = {
/**
* Returns all tasks corresponding to the calendar
*
* @param {object} state The store data
* @param {object} getters The store getters
* @param {object} rootState The store root state
* @return {Array<Task>} The tasks
*/
getTasksByCalendarId: (state, getters, rootState) =>
/**
* @param {string} calendarId The Id of the calendar in question
* @return {Array<Task>} The tasks
*/
(calendarId) => {
const calendar = getters.getCalendarById(calendarId)
if (calendar) {
return Object.values(calendar.tasks)
}
},
/**
* Returns all tasks corresponding to current route value
*
* @param {object} state The store data
* @param {object} getters The store getters
* @param {object} rootState The store root state
* @return {Array<Task>} The tasks
*/
getTasksByRoute: (state, getters, rootState) => {
return getters.getTasksByCalendarId(rootState.route.params.calendarId)
},
/**
* Returns all tasks which are direct children of the current task
*
* @param {object} state The store data
* @param {object} getters The store getters
* @param {object} rootState The store root state
* @return {Array<Task>} The sub-tasks of the current task
*/
getTasksByParent: (state, getters, rootState) =>
/**
* @param {object} parent The parent task
* @return {Array<Task>} The sub-tasks of the current task
*/
(parent) => {
return getters.getTasksByCalendarId(parent.calendar.id)
.filter(task => {
return task.related === parent.uid
})
},
/**
* Returns all tasks of all calendars
*
* @param {object} state The store data
* @param {object} getters The store getters
* @return {Array} All tasks in store
*/
getAllTasks: (state, getters) => {
let tasks = []
getters.getTaskCalendars.forEach(calendar => {
tasks = tasks.concat(Object.values(calendar.tasks))
})
return tasks
},
/**
* Returns the task currently opened by route
*
* @param {object} state The store data
* @param {object} getters The store getters
* @param {object} rootState The store root state
* @return {Task} The task
*/
getTaskByRoute: (state, getters, rootState) => {
// If a calendar is given, only search in that calendar.
if (rootState.route.params.calendarId) {
const calendar = getters.getCalendarById(rootState.route.params.calendarId)
if (!calendar) {
return null
}
return Object.values(calendar.tasks).find(task => {
return task.uri === rootState.route.params.taskId
})
}
// Else, we have to search all calendars
return getters.getTaskByUri(rootState.route.params.taskId)
},
/**
* Returns the task by Uri
*
* @param {object} state The store data
* @param {object} getters The store getters
* @return {Task} The task
*/
getTaskByUri: (state, getters) =>
/**
* @param {string} taskUri The Uri of the task in question
* @return {Task} The task
*/
(taskUri) => {
// We have to search in all calendars
let task
for (const calendar of getters.getTaskCalendars) {
task = Object.values(calendar.tasks).find(task => {
return task.uri === taskUri
})
if (task) return task
}
return null
},
/**
* Returns the task by Uri
*
* @param {object} state The store data
* @param {object} getters The store getters
* @return {Task} The task
*/
getTaskByUid: (state, getters) =>
/**
* @param {string} taskUid The Uid of the task in question
* @return {Task} The task
*/
(taskUid) => {
// We have to search in all calendars
let task
for (const calendar of getters.getTaskCalendars) {
task = Object.values(calendar.tasks).find(task => {
return task.uid === taskUid
})
if (task) return task
}
return null
},
/**
* Returns the root tasks from a given object
*
* @return {Array<Task>}
*/
findRootTasks: () =>
/**
* @param {object} tasks The tasks to search in
* @return {Array<Task>}
*/
(tasks) => {
return Object.values(tasks).filter(task => {
/**
* Check if the task has the related field set.
* If it has, then check if the parent task is available
* (otherwise it might happen, that this task is not shown at all)
*/
return !task.related || !isParentInList(task, tasks)
})
},
/**
* Returns the closed root tasks from a given object
*
* @return {Array<Task>}
*/
findClosedRootTasks: () =>
/**
* @param {object} tasks The tasks to search in
* @return {Array<Task>}
*/
(tasks) => {
return Object.values(tasks).filter(task => {
/**
* Check if the task has the related field set.
* If it has, then check if the parent task is available
* (otherwise it might happen, that this task is not shown at all)
*/
return (!task.related || !isParentInList(task, tasks)) && task.closed
})
},
/**
* Returns the not closed root tasks from a given object
*
* @return {Array<Task>}
*/
findOpenRootTasks: () =>
/**
* @param {object} tasks The tasks to search in
* @return {Array<Task>}
*/
(tasks) => {
return Object.values(tasks).filter(task => {
/**
* Check if the task has the related field set.
* If it has, then check if the parent task is available
* (otherwise it might happen, that this task is not shown at all)
*/
return (!task.related || !isParentInList(task, tasks)) && !task.closed
})
},
/**
* Returns the parent task of a given task
*
* @return {Task} The parent task
*/
getParentTask: () =>
/**
* @param {Task} task The task of which to find the parent
* @return {Task} The parent task
*/
(task) => {
const tasks = task.calendar.tasks
return Object.values(tasks).find(search => search.uid === task.related) || null
},
/**
* Returns the current search query
*
* @param {object} state The store data
* @param {object} getters The store getters
* @param {object} rootState The store root state
* @return {string} The current search query
*/
searchQuery: (state, getters, rootState) => {
return state.searchQuery
},
/**
* Returns all tags of all tasks
*
* @param {object} state The store data
* @param {object} getters The store getters
* @return {Array<string>} All tags
*/
tags: (state, getters) => {
const tasks = getters.getAllTasks
return tasks.reduce((tags, task) => {
// Add each tag to the tags array if it's not present yet
task.tags.forEach((tag) => {
if (!tags.includes(tag)) {
tags.push(tag)
}
})
return tags
}, [])
},
}
const mutations = {
/**
* Stores tasks into state
*
* @param {object} state Default state
* @param {Array<Task>} tasks Tasks
*/
appendTasks(state, tasks = []) {
state.tasks = tasks.reduce(function(list, task) {
if (task instanceof Task) {
Vue.set(list, task.key, task)
} else {
console.error('Wrong task object', task)
}
return list
}, state.tasks)
},
/**
* Stores task into state
*
* @param {object} state Default state
* @param {Task} task The task to append
*/
appendTask(state, task) {
Vue.set(state.tasks, task.key, task)
},
/**
* Deletes a task from state
*
* @param {object} state Default state
* @param {Task} task The task to delete
*/
deleteTask(state, task) {
if (state.tasks[task.key] && task instanceof Task) {
Vue.delete(state.tasks, task.key)
}
},
/**
* Deletes a task from the parent
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task to delete from the parents subtask list
* @param {Task} data.parent The parent task
*/
deleteTaskFromParent(state, { task, parent }) {
if (task instanceof Task) {
// Remove task from parents subTask list if necessary
if (task.related && parent) {
Vue.delete(parent.subTasks, task.uid)
}
}
},
/**
* Adds a task to parent task as subtask
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task to add to the parents subtask list
* @param {Task} data.parent The parent task
*/
addTaskToParent(state, { task, parent }) {
if (task.related && parent) {
Vue.set(parent.subTasks, task.uid, task)
}
},
/**
* Toggles the completed state of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {number} data.complete The complete value
*/
setComplete(state, { task, complete }) {
Vue.set(task, 'complete', complete)
},
/**
* Toggles the starred state of a task
*
* @param {object} state The store data
* @param {Task} task The task
*/
toggleStarred(state, task) {
if (+task.priority < 1 || +task.priority > 4) {
Vue.set(task, 'priority', 1)
} else {
Vue.set(task, 'priority', 0)
}
},
/**
* Toggles the pinned state of a task
*
* @param {object} state The store data
* @param {Task} task The task
*/
togglePinned(state, task) {
Vue.set(task, 'pinned', !task.pinned)
},
/**
* Toggles the visibility of the subtasks
*
* @param {object} state The store data
* @param {Task} task The task
*/
toggleSubtasksVisibility(state, task) {
Vue.set(task, 'hideSubtasks', !task.hideSubtasks)
},
/**
* Toggles the visibility of the completed subtasks
*
* @param {object} state The store data
* @param {Task} task The task
*/
toggleCompletedSubtasksVisibility(state, task) {
Vue.set(task, 'hideCompletedSubtasks', !task.hideCompletedSubtasks)
},
/**
* Sets the summary of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.summary The summary
*/
setSummary(state, { task, summary }) {
Vue.set(task, 'summary', summary)
},
/**
* Sets the note of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.note The note
*/
setNote(state, { task, note }) {
Vue.set(task, 'note', note)
},
/**
* Sets the tags of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {Array} data.tags The array of tags
*/
setTags(state, { task, tags }) {
Vue.set(task, 'tags', tags)
},
/**
* Adds a tag to a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.tag The tag to add
*/
addTag(state, { task, tag }) {
Vue.set(task, 'tags', task.tags.concat([tag]))
},
/**
* Sets the priority of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.priority The priority
*/
setPriority(state, { task, priority }) {
Vue.set(task, 'priority', priority)
},
/**
* Sets the classification of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.classification The classification
*/
setClassification(state, { task, classification }) {
Vue.set(task, 'class', classification)
},
/**
* Sets the status of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.status The status
*/
setStatus(state, { task, status }) {
Vue.set(task, 'status', status)
},
/**
* Sets the sort order of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {number} data.order The sort order
*/
setSortOrder(state, { task, order }) {
Vue.set(task, 'sortOrder', order)
},
/**
* Sets the due date of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {moment} data.due The due date moment
* @param {boolean} data.allDay Whether the date is all-day
*/
setDue(state, { task, due, allDay }) {
if (due === null) {
// If the date is null, just set (remove) it.
Vue.set(task, 'due', due)
} else {
// Check, that the due date is after the start date.
// If it is not, shift the start date to keep the difference between start and due equal.
let start = task.startMoment
if (start.isValid() && due.isBefore(start)) {
const currentdue = task.dueMoment
if (currentdue.isValid()) {
start.subtract(currentdue.diff(due), 'ms')
} else {
start = due.clone()
}
Vue.set(task, 'start', momentToICALTime(start, allDay))
}
// Set the due date, convert it to ICALTime first.
Vue.set(task, 'due', momentToICALTime(due, allDay))
}
},
/**
* Sets the start date of a task
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {moment} data.start The start date moment
* @param {boolean} data.allDay Whether the date is all-day
*/
setStart(state, { task, start, allDay }) {
if (start === null) {
// If the date is null, just set (remove) it.
Vue.set(task, 'start', start)
} else {
// Check, that the start date is before the due date.
// If it is not, shift the due date to keep the difference between start and due equal.
let due = task.dueMoment
if (due.isValid() && start.isAfter(due)) {
const currentstart = task.startMoment
if (currentstart.isValid()) {
due.add(start.diff(currentstart), 'ms')
} else {
due = start.clone()
}
Vue.set(task, 'due', momentToICALTime(due, allDay))
}
// Set the due date, convert it to ICALTime first.
Vue.set(task, 'start', momentToICALTime(start, allDay))
}
},
/**
* Toggles if the start and due dates of a task are all day
*
* @param {object} state The store data
* @param {Task} task The task
*/
toggleAllDay(state, task) {
Vue.set(task, 'allDay', !task.allDay)
},
/**
* Move task to a different calendar
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {Calendar} data.calendar The calendar to move the task to
*/
setTaskCalendar(state, { task, calendar }) {
Vue.set(task, 'calendar', calendar)
},
/**
* Move task to a different calendar
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {string} data.related The uid of the related task
*/
setTaskParent(state, { task, related }) {
Vue.set(task, 'related', related)
},
/**
* Update a task etag
*
* @param {object} state The store object
* @param {object} data Destructuring object
* @param {Task} data.task The task to update
*/
updateTaskEtag(state, { task }) {
if (state.tasks[task.key] && task instanceof Task) {
// replace task object data
state.tasks[task.key].dav.etag = task.conflict
} else {
console.error('Error while replacing the etag of following task ', task)
}
},
/**
* Resets the sync status
*
* @param {object} state The store object
* @param {object} data Destructuring object
* @param {Task} data.task The task to update
*/
resetStatus(state, { task }) {
if (state.tasks[task.key] && task instanceof Task) {
// replace task object data
state.tasks[task.key].syncStatus = null
}
},
/**
* Update a task
*
* @param {object} state The store data
* @param {Task} task The task to update
*/
updateTask(state, task) {
if (state.tasks[task.key] && task instanceof Task) {
// replace task object data
state.tasks[task.key].updateTask(task.jCal)
} else {
console.error('Error while replacing the following task ', task)
}
},
/**
* Sets the search query
*
* @param {object} state The store data
* @param {string} searchQuery The search query
*/
setSearchQuery(state, searchQuery) {
state.searchQuery = searchQuery
},
addTaskForDeletion(state, { task }) {
Vue.set(state.deletedTasks, task.key, task)
},
clearTaskFromDeletion(state, { task }) {
if (state.deletedTasks[task.key] && task instanceof Task) {
Vue.delete(state.deletedTasks, task.key)
}
},
/**
* Sets the delete countdown value
*
* @param {object} state The store data
* @param {object} data Destructuring object
* @param {Task} data.task The task
* @param {number} data.countdown The countdown value
*/
setTaskDeleteCountdown(state, { task, countdown }) {
Vue.set(task, 'deleteCountdown', countdown)
},
}
const actions = {
/**
* Creates a new task
*
* @param {object} context The store mutations
* @param {object} taskData The data of the new task
* @return {Promise}
*/
async createTask(context, taskData) {
if (!taskData.calendar) {
taskData.calendar = context.getters.getDefaultCalendar
}
// Don't try to create tasks in read-only calendars
if (taskData.calendar.readOnly) {
return
}
const task = new Task('BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Nextcloud Tasks v' + this._vm.$appVersion + '\nEND:VCALENDAR', taskData.calendar)
task.created = ICAL.Time.now()
task.summary = taskData.summary
task.hidesubtasks = 0
if (taskData.priority) {
task.priority = taskData.priority
}
if (taskData.complete) {
task.complete = taskData.complete
}
if (taskData.note) {
task.note = taskData.note
}
if (taskData.due) {
task.due = taskData.due
}
if (taskData.start) {
task.start = taskData.start
}
if (taskData.allDay) {
task.allDay = taskData.allDay
}
if (taskData.related) {
task.related = taskData.related
// Check that parent task is not completed, uncomplete if necessary.
if (task.complete !== 100) {
const parent = context.getters.getParentTask(task)
if (parent && parent.completed) {
await context.dispatch('setPercentComplete', { task: parent, complete: 0 })
}
}
}
const vData = ICAL.stringify(task.jCal)
if (!task.dav) {
const response = await task.calendar.dav.createVObject(vData)
Vue.set(task, 'dav', response)
task.syncStatus = new SyncStatus('success', t('tasks', 'Successfully created the task.'))
context.commit('appendTask', task)
context.commit('addTaskToCalendar', task)
const parent = context.getters.getTaskByUid(task.related)
context.commit('addTaskToParent', { task, parent })
// In case the task is created in Talk, we don't have a route
// Only open the details view if there is enough space or if it is already open.
if (context.rootState.route !== undefined && (document.documentElement.clientWidth >= 768 || context.rootState.route?.params.taskId !== undefined)) {
// Open the details view for the new task
const calendarId = context.rootState.route.params.calendarId
const collectionId = context.rootState.route.params.collectionId
if (calendarId) {
router.push({ name: 'calendarsTask', params: { calendarId, taskId: task.uri } })
} else if (collectionId) {
if (collectionId === 'week') {
router.push({
name: 'collectionsParamTask',
params: { collectionId, taskId: task.uri, collectionParam: '0' },
})
} else {
router.push({ name: 'collectionsTask', params: { collectionId, taskId: task.uri } })
}
}
}
return task
}
},
/**
* Deletes a task
*
* @param {object} context The store mutations
* @param {object} data Destructuring object
* @param {Task} data.task The task to delete
* @param {boolean} [data.dav = true] Trigger a dav deletion
*/
async deleteTask(context, { task, dav = true }) {
// Don't try to delete tasks in read-only calendars
if (task.calendar.readOnly) {
return
}
// Don't delete tasks in shared calendars with access class not PUBLIC
if (task.calendar.isSharedWithMe && task.class !== 'PUBLIC') {
return
}
// Clear task from deletion array
context.dispatch('clearTaskDeletion', task)
/**
* Deletes a task from the store
*/
function deleteTaskFromStore() {
context.commit('deleteTask', task)
const parent = context.getters.getTaskByUid(task.related)
context.commit('deleteTaskFromParent', { task, parent })
context.commit('deleteTaskFromCalendar', task)
// If the task is open in the sidebar, close the sidebar
if (context.rootState.route.params.taskId === task.uri) {
emit('tasks:close-appsidebar')
}
// Stop the delete timeout if no tasks are scheduled for deletion anymore
if (Object.values(context.state.deletedTasks).length < 1) {
clearInterval(context.state.deleteInterval)
context.state.deleteInterval = null
}
}
// Delete all subtasks first
await Promise.all(Object.values(task.subTasks).map(async (subTask) => {
await context.dispatch('deleteTask', { task: subTask, dav: true })
}))
// Only local delete if the task does not exist on the server
if (task.dav && dav) {
await task.dav.delete()
.then(() => {
deleteTaskFromStore()
})
.catch((error) => {
console.debug(error)
task.syncStatus = new SyncStatus('error', t('tasks', 'Could not delete the task.'))
})
} else {
deleteTaskFromStore()
}
},
/**
* Schedules a task for deletion
*
* @param {object} context The store context
* @param {Task} task The task to delete
* @return {Promise}
*/
async scheduleTaskDeletion(context, task) {
// Don't try to delete tasks in read-only calendars
if (task.calendar.readOnly) {
return
}
// Don't delete tasks in shared calendars with access class not PUBLIC
if (task.calendar.isSharedWithMe && task.class !== 'PUBLIC') {
return
}
context.commit('addTaskForDeletion', { task })
context.commit('setTaskDeleteCountdown', { task, countdown: 7 })
// Start the delete timeout if it is not running
if (context.state.deleteInterval === null) {
context.state.deleteInterval = setInterval(async () => {
Object.values(context.state.deletedTasks).forEach(task => {
context.commit('setTaskDeleteCountdown', { task, countdown: --task.deleteCountdown })
if (task.deleteCountdown <= 0) {
context.dispatch('deleteTask', { task, dav: true })
}
})
}, 1000)
}
},
/**
* Cancels a scheduled task deletion
*
* @param {object} context The store context
* @param {Task} task The task to not delete
* @return {Promise}
*/
async clearTaskDeletion(context, task) {
context.commit('clearTaskFromDeletion', { task })
context.commit('setTaskDeleteCountdown', { task, countdown: null })
// Stop the delete timeout if no tasks scheduled for deletion are left
if (Object.values(context.state.deletedTasks).length === 0) {
clearInterval(context.state.deleteInterval)
context.state.deleteInterval = null
}
},
/**
* Updates a task
*
* @param {object} context The store context
* @param {Task} task The task to update
* @return {Promise}
*/
async updateTask(context, task) {
// If an update is currently running, we schedule another one an return
if (task.updateRunning) {
task.updateScheduled = true
return
}
task.updateRunning = true
task.updateScheduled = false
// Don't try to update tasks in read-only calendars
if (task.calendar.readOnly) {
return
}
// Don't edit tasks in shared calendars with access class not PUBLIC
if (task.calendar.isSharedWithMe && task.class !== 'PUBLIC') {
return
}
const vCalendar = ICAL.stringify(task.jCal)
if (!task.conflict) {
task.dav.data = vCalendar
task.syncStatus = new SyncStatus('sync', t('tasks', 'Synchronizing to the server.'))
try {
await task.dav.update()
task.syncStatus = new SyncStatus('success', t('tasks', 'Task successfully saved to server.'))
} catch (error) {
// Wrong etag, we most likely have a conflict
if (error && error.status === 412) {
// Saving the new etag so that the user can manually
// trigger a fetchCompleteData without any further errors
task.conflict = error.xhr.getResponseHeader('etag')
task.syncStatus = new SyncStatus('conflict', t('tasks', 'Could not update the task because it was changed on the server. Please click to refresh it, local changes will be discarded.'))
} else {
task.syncStatus = new SyncStatus('error', t('tasks', 'Could not update the task.'))
}
}
} else {
task.syncStatus = new SyncStatus('conflict', t('tasks', 'Could not update the task because it was changed on the server. Please click to refresh it, local changes will be discarded.'))
}
task.updateRunning = false
// We have to run again if an update was scheduled in the meantime.
if (task.updateScheduled) {
await context.dispatch('updateTask', task)
}
},
/**
* Retrieves the task with the given uri from the given calendar
* and commits the result
*
* @param {object} context The store mutations
* @param {object} data Destructuring object
* @param {Calendar} data.calendar The calendar
* @param {string} data.taskUri The uri of the requested task
* @return {Task}
*/
async getTaskByUri(context, { calendar, taskUri }) {
const response = await calendar.dav.find(taskUri)
if (response) {
const task = new Task(response.data, calendar)
Vue.set(task, 'dav', response)
if (task.related) {
let parent = context.getters.getTaskByUid(task.related)
// If the parent is not found locally, we try to get it from the server.
if (!parent) {
parent = await context.dispatch('getTaskByUid', { calendar, taskUid: task.related })
}
context.commit('addTaskToParent', { task, parent })
}
// In case we already have subtasks of this task in the store, add them as well.
const subTasksInStore = context.getters.getTasksByParent(task)
subTasksInStore.forEach(
subTask => {
context.commit('addTaskToParent', { task: subTask, parent: task })
}
)
context.commit('appendTasksToCalendar', { calendar, tasks: [task] })
context.commit('appendTasks', [task])
return task
} else {
return null
}
},
/**
* Retrieves the task with the given uid from the given calendar
* and commits the result
*
* @param {object} context The store mutations
* @param {object} data Destructuring object
* @param {Calendar} data.calendar The calendar
* @param {string} data.taskUid The uid of the requested task
* @return {Task}
*/
async getTaskByUid(context, { calendar, taskUid }) {
const response = await findVTODObyUid(calendar, taskUid)
// We expect to only get zero or one task when we query by UID.
if (response.length) {
const task = new Task(response[0].data, calendar)
Vue.set(task, 'dav', response[0])
if (task.related) {