tempus/caldav.go

202 lines
4.9 KiB
Go
Raw Normal View History

2024-06-19 19:27:13 +00:00
package main
import (
"context"
// "fmt"
webdav "github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/caldav"
"strings"
2024-06-21 18:38:41 +00:00
"time"
2024-06-19 19:27:13 +00:00
)
type TODO struct {
2024-06-21 18:38:41 +00:00
Name string
Desc string
Time string
2024-06-19 19:27:13 +00:00
// Priority int //TODO
// Subtasks []TODO //TODO
// Repeat //TODO
// Alarm //TODO
}
var clientWebDAV *webdav.Client
var client *caldav.Client // clientCalDAV
// var calendarObjects []caldav.CalendarObject
var ctx = context.Background()
2024-06-21 18:38:41 +00:00
2024-06-19 19:27:13 +00:00
// var authSession caldav.Client // clientCalDAV
func (options *Options) InitDAVclients() error {
2024-06-21 18:38:41 +00:00
2024-06-19 19:27:13 +00:00
var err error
authSession := webdav.HTTPClientWithBasicAuth(nil, options.User, options.Password)
clientWebDAV, err = webdav.NewClient(authSession, options.URL)
if err != nil {
// Handle error
return err
}
client, err = caldav.NewClient(authSession, options.URL)
if err != nil {
// Handle error
return err
}
return nil
}
2024-06-21 18:38:41 +00:00
func GetCalendars() ([]caldav.Calendar, error) {
2024-06-19 19:27:13 +00:00
principal, err := clientWebDAV.FindCurrentUserPrincipal(ctx)
if err != nil {
// Handle error
2024-06-21 18:38:41 +00:00
return nil, err
2024-06-19 19:27:13 +00:00
}
// fmt.Println("principal: ",principal) TODO log
// Find the calendar home set
calendarHomeSet, err := client.FindCalendarHomeSet(ctx, principal)
if err != nil {
2024-06-21 18:38:41 +00:00
return nil, err
2024-06-19 19:27:13 +00:00
}
// Find calendars in the calendar home set
calendars, err := client.FindCalendars(ctx, calendarHomeSet)
if err != nil {
2024-06-21 18:38:41 +00:00
return nil, err
2024-06-19 19:27:13 +00:00
}
return calendars, nil
}
2024-06-21 18:38:41 +00:00
func GetTODOs(calendarPath string) (calendarObjects []caldav.CalendarObject, err error) {
2024-06-19 19:27:13 +00:00
date := time.Now()
2024-06-21 18:38:41 +00:00
dateStart := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location()).AddDate(0, 0, -1) //TODO too complex - time.Now().Add(-92 * time.Hour),
2024-06-19 19:27:13 +00:00
// dateEnd:= time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location()) //date +1 day
2024-06-21 18:38:41 +00:00
dateEnd := date.AddDate(0, 1, 0) //TODO we hard limit pulling events only for this month, to prevent getting tooo much events
2024-06-19 19:27:13 +00:00
calQuery := caldav.CalendarQuery{
CompRequest: caldav.CalendarCompRequest{
2024-06-21 18:38:41 +00:00
Name: "VCALENDAR",
Comps: []caldav.CalendarCompRequest{{
Name: "VTODO",
Props: []string{
"UID",
"SUMMARY",
"COMPLETED",
"DESCRIPTION",
"DUE",
"PRIORITY",
"RELATED-TO",
},
},
},
},
2024-06-19 19:27:13 +00:00
CompFilter: caldav.CompFilter{
2024-06-21 18:38:41 +00:00
Name: "VCALENDAR",
Comps: []caldav.CompFilter{
{
Name: "VTODO",
Start: dateStart,
End: dateEnd,
// FYI: server-side filtering is not supported for Nextcloud
2024-06-19 19:27:13 +00:00
},
2024-06-21 18:38:41 +00:00
},
},
2024-06-19 19:27:13 +00:00
}
2024-06-21 18:38:41 +00:00
calendarObjects, err = client.QueryCalendar(ctx, calendarPath, &calQuery)
2024-06-19 19:27:13 +00:00
if err != nil {
2024-06-21 18:38:41 +00:00
return nil, err
2024-06-19 19:27:13 +00:00
}
2024-06-21 18:38:41 +00:00
return calendarObjects, nil
2024-06-19 19:27:13 +00:00
}
2024-06-21 18:38:41 +00:00
func ParseDueDateTODOs(calObjs []caldav.CalendarObject, date time.Time) ([]TODO, error) {
var output []TODO
2024-06-19 19:27:13 +00:00
2024-06-21 18:38:41 +00:00
for _, calObj := range calObjs {
2024-06-19 19:27:13 +00:00
// fmt.Println((*(*calObj.Data).Children[0]).Name)
// TODO STATUS map[] COMPLETED
2024-06-21 18:38:41 +00:00
for _, event := range (*calObj.Data).Children {
2024-06-19 19:27:13 +00:00
// if (*event).Name == "VTODO" {
2024-06-21 18:38:41 +00:00
var notCompletedTODO, withDate, fromToday bool
//TODO we can optimize there if we encounter wrong state to forcefully stop next analysis
// notCompletedTODO
if (*event).Props["COMPLETED"] == nil {
if (*event).Props["STATUS"] == nil {
notCompletedTODO = true
} else {
if (*event).Props["STATUS"][0].Value != "COMPLETED" {
notCompletedTODO = true
2024-06-19 19:27:13 +00:00
}
}
2024-06-21 18:38:41 +00:00
}
// withTodayDate
if (*event).Props["DUE"] != nil {
withDate = true
// fromToday
if strings.HasPrefix((*event).Props["DUE"][0].Value, date.Format("20060102")) {
fromToday = true
}
}
if notCompletedTODO && withDate && fromToday {
2024-06-19 19:27:13 +00:00
2024-06-21 18:38:41 +00:00
var tmpTODO TODO
if (*event).Props["SUMMARY"] != nil {
name := (*event).Props["SUMMARY"][0].Value
tmpTODO.Name = name
} else {
tmpTODO.Name = "<EMPTY>"
2024-06-19 19:27:13 +00:00
}
2024-06-21 18:38:41 +00:00
if (*event).Props["DESCRIPTION"] != nil {
description := (*event).Props["DESCRIPTION"][0].Value
tmpTODO.Desc = description
}
var todoTime string
due := (*event).Props["DUE"][0].Value
index := strings.Index(due, "T")
if index != -1 {
str := due[index+1:]
todoTime = str[:2] + ":" + str[2:4]
2024-06-19 19:27:13 +00:00
2024-06-21 18:38:41 +00:00
tmpTODO.Time = todoTime
2024-06-19 19:27:13 +00:00
}
2024-06-21 18:38:41 +00:00
output = append(output, tmpTODO)
}
2024-06-19 19:27:13 +00:00
// }
}
}
2024-06-21 18:38:41 +00:00
// //TODO sort: time, priority (if no time)
// // it means, put DUE at first, other DUE;VALUE=DATE in priority order
// //TODO color: priority, add time icon if have time
// //TODO UID:7ed30f40-fce1-422c-be3b-0486dcfe8943
// //TODO RELATED-TO:7ed30f40-fce1-422c-be3b-0486dcfe8943 # subtask
// //TODO PRIORITY:1 #1-high, 5-mid, 9-low
//TODO repeat function???
2024-06-19 19:27:13 +00:00
2024-06-21 18:38:41 +00:00
return output, nil
2024-06-19 19:27:13 +00:00
}
//TODO on complete -repeat function
// RRULE:FREQ=WEEKLY;INTERVAL=1
//TODO if no repeat - mark as complted
// STATUS:COMPLETED
// COMPLETED:20240421T065323Z
// PERCENT-COMPLETE:100
//TODO support notifcations/alarms???
// BEGIN:VTODO
// ...
// BEGIN:VALARM
// TRIGGER;RELATED=END:-PT15M
// ACTION:DISPLAY
// DESCRIPTION:Default Tasks.org description
// END:VALARM
// END:VTODO