vikunja-api/pkg/routes/caldav/parsing.go

126 lines
3.3 KiB
Go
Raw Normal View History

2020-02-07 17:27:45 +01:00
// Vikunja is a to-do list application to facilitate your life.
2020-01-09 18:33:22 +01:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2019-05-22 19:48:48 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2019-05-22 19:48:48 +02:00
//
// 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 General Public License for more details.
2019-05-22 19:48:48 +02:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-05-22 19:48:48 +02:00
package caldav
import (
"code.vikunja.io/api/pkg/caldav"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/timeutil"
2019-05-24 19:45:33 +02:00
"github.com/laurent22/ical-go"
2019-05-22 19:48:48 +02:00
"strconv"
"time"
)
func getCaldavTodosForTasks(list *models.List) string {
// Make caldav todos from Vikunja todos
var caldavtodos []*caldav.Todo
for _, t := range list.Tasks {
duration := t.EndDate.ToTime().Sub(t.StartDate.ToTime())
2019-05-22 19:48:48 +02:00
caldavtodos = append(caldavtodos, &caldav.Todo{
Timestamp: t.Updated,
UID: t.UID,
Summary: t.Title,
Description: t.Description,
Completed: t.DoneAt,
2019-05-22 19:48:48 +02:00
// Organizer: &t.CreatedBy, // Disabled until we figure out how this works
Priority: t.Priority,
Start: t.StartDate,
End: t.EndDate,
Created: t.Created,
Updated: t.Updated,
DueDate: t.DueDate,
Duration: duration,
2019-05-22 19:48:48 +02:00
})
}
caldavConfig := &caldav.Config{
Name: list.Title,
ProdID: "Vikunja Todo App",
}
return caldav.ParseTodos(caldavConfig, caldavtodos)
}
2019-08-14 22:19:04 +02:00
func parseTaskFromVTODO(content string) (vTask *models.Task, err error) {
2019-05-22 19:48:48 +02:00
parsed, err := ical.ParseCalendar(content)
if err != nil {
return nil, err
}
// We put the task details in a map to be able to handle them more easily
task := make(map[string]string)
for _, c := range parsed.Children {
if c.Name == "VTODO" {
for _, entry := range c.Children {
task[entry.Name] = entry.Value
}
// Breaking, to only process the first task
break
}
}
// Parse the UID
var priority int64
if _, ok := task["PRIORITY"]; ok {
priority, err = strconv.ParseInt(task["PRIORITY"], 10, 64)
if err != nil {
return nil, err
}
}
// Parse the enddate
duration, _ := time.ParseDuration(task["DURATION"])
2019-08-14 22:19:04 +02:00
vTask = &models.Task{
UID: task["UID"],
Title: task["SUMMARY"],
Description: task["DESCRIPTION"],
Priority: priority,
DueDate: caldavTimeToTimestamp(task["DUE"]),
Updated: caldavTimeToTimestamp(task["DTSTAMP"]),
StartDate: caldavTimeToTimestamp(task["DTSTART"]),
DoneAt: caldavTimeToTimestamp(task["COMPLETED"]),
2019-05-22 19:48:48 +02:00
}
if task["STATUS"] == "COMPLETED" {
vTask.Done = true
}
if duration > 0 && vTask.StartDate > 0 {
vTask.EndDate = timeutil.FromTime(vTask.StartDate.ToTime().Add(duration))
2019-05-22 19:48:48 +02:00
}
return
}
func caldavTimeToTimestamp(tstring string) timeutil.TimeStamp {
2019-05-22 19:48:48 +02:00
if tstring == "" {
return 0
}
t, err := time.Parse(caldav.DateFormat, tstring)
if err != nil {
log.Warningf("Error while parsing caldav time %s to TimeStamp: %s", tstring, err)
2019-05-22 19:48:48 +02:00
return 0
}
return timeutil.FromTime(t)
2019-05-22 19:48:48 +02:00
}