2020-05-13 12:07:44 +00:00
|
|
|
package alpscaldav
|
2020-02-05 17:08:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-05-13 17:42:41 +00:00
|
|
|
"path"
|
2020-05-20 15:08:24 +00:00
|
|
|
"strings"
|
2020-02-05 17:08:00 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
"git.sr.ht/~emersion/alps"
|
2020-05-13 17:42:41 +00:00
|
|
|
"github.com/emersion/go-ical"
|
2020-02-05 17:08:00 +00:00
|
|
|
"github.com/emersion/go-webdav/caldav"
|
2020-05-13 17:42:41 +00:00
|
|
|
"github.com/google/uuid"
|
2020-05-13 14:48:03 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2020-02-05 17:08:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CalendarRenderData struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
alps.BaseRenderData
|
2020-02-05 17:57:16 +00:00
|
|
|
Time time.Time
|
2020-05-20 16:39:37 +00:00
|
|
|
Now time.Time
|
|
|
|
Dates [7 * 6]time.Time
|
2020-02-05 17:57:16 +00:00
|
|
|
Calendar *caldav.Calendar
|
2020-05-13 15:10:21 +00:00
|
|
|
Events []CalendarObject
|
2020-02-05 17:57:16 +00:00
|
|
|
PrevPage, NextPage string
|
2020-05-20 16:39:37 +00:00
|
|
|
PrevTime, NextTime time.Time
|
|
|
|
|
|
|
|
EventsForDate func(time.Time) []CalendarObject
|
|
|
|
DaySuffix func(n int) string
|
|
|
|
Sub func(a, b int) int
|
2020-02-05 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 17:39:53 +00:00
|
|
|
type EventRenderData struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
alps.BaseRenderData
|
2020-02-05 17:39:53 +00:00
|
|
|
Calendar *caldav.Calendar
|
2020-05-13 15:10:21 +00:00
|
|
|
Event CalendarObject
|
2020-02-05 17:39:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:42:41 +00:00
|
|
|
type UpdateEventRenderData struct {
|
|
|
|
alps.BaseRenderData
|
2020-05-20 17:40:52 +00:00
|
|
|
Calendar *caldav.Calendar
|
2020-05-13 17:42:41 +00:00
|
|
|
CalendarObject *caldav.CalendarObject // nil if creating a new contact
|
|
|
|
Event *ical.Event
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:57:16 +00:00
|
|
|
var monthPageLayout = "2006-01"
|
|
|
|
|
2020-05-13 14:48:03 +00:00
|
|
|
func parseObjectPath(s string) (string, error) {
|
|
|
|
p, err := url.PathUnescape(s)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to parse path: %v", err)
|
|
|
|
return "", echo.NewHTTPError(http.StatusBadRequest, err)
|
|
|
|
}
|
|
|
|
return string(p), nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:32:53 +00:00
|
|
|
func parseTime(dateStr, timeStr string) (time.Time, error) {
|
|
|
|
layout := inputDateLayout
|
|
|
|
s := dateStr
|
|
|
|
if timeStr != "" {
|
|
|
|
layout = inputDateLayout + "T" + inputTimeLayout
|
|
|
|
s = dateStr + "T" + timeStr
|
|
|
|
}
|
|
|
|
t, err := time.Parse(layout, s)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("malformed date: %v", err)
|
|
|
|
return time.Time{}, echo.NewHTTPError(http.StatusBadRequest, err)
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
func registerRoutes(p *alps.GoPlugin, u *url.URL) {
|
|
|
|
p.GET("/calendar", func(ctx *alps.Context) error {
|
2020-02-05 17:57:16 +00:00
|
|
|
var start time.Time
|
|
|
|
if s := ctx.QueryParam("month"); s != "" {
|
|
|
|
var err error
|
|
|
|
start, err = time.Parse(monthPageLayout, s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse month: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
now := time.Now()
|
|
|
|
start = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
|
|
}
|
|
|
|
end := start.AddDate(0, 1, 0)
|
|
|
|
|
2020-02-05 17:08:00 +00:00
|
|
|
// TODO: multi-calendar support
|
|
|
|
c, calendar, err := getCalendar(u, ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := caldav.CalendarQuery{
|
|
|
|
CompRequest: caldav.CalendarCompRequest{
|
|
|
|
Name: "VCALENDAR",
|
|
|
|
Props: []string{"VERSION"},
|
|
|
|
Comps: []caldav.CalendarCompRequest{{
|
|
|
|
Name: "VEVENT",
|
|
|
|
Props: []string{
|
|
|
|
"SUMMARY",
|
|
|
|
"UID",
|
|
|
|
"DTSTART",
|
|
|
|
"DTEND",
|
|
|
|
"DURATION",
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
CompFilter: caldav.CompFilter{
|
|
|
|
Name: "VCALENDAR",
|
|
|
|
Comps: []caldav.CompFilter{{
|
|
|
|
Name: "VEVENT",
|
|
|
|
Start: start,
|
|
|
|
End: end,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
events, err := c.QueryCalendar(calendar.Path, &query)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to query calendar: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:39:37 +00:00
|
|
|
// TODO: Time zones are hard
|
|
|
|
var dates [7 * 6]time.Time
|
|
|
|
initialDate := start.UTC()
|
|
|
|
initialDate = initialDate.AddDate(0, 0, -int(initialDate.Weekday()))
|
|
|
|
for i := 0; i < len(dates); i += 1 {
|
|
|
|
dates[i] = initialDate
|
|
|
|
initialDate = initialDate.AddDate(0, 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventMap := make(map[time.Time][]CalendarObject)
|
|
|
|
for _, ev := range events {
|
|
|
|
ev := ev // make a copy
|
|
|
|
// TODO: include event on each date for which it is active
|
|
|
|
co := ev.Data.Events()[0]
|
|
|
|
startTime, _ := co.DateTimeStart(nil)
|
|
|
|
startTime = startTime.UTC().Truncate(time.Hour * 24)
|
|
|
|
eventMap[startTime] = append(eventMap[startTime], CalendarObject{&ev})
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:08:00 +00:00
|
|
|
return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{
|
2020-05-20 17:47:54 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx).
|
|
|
|
WithTitle(calendar.Name + " Calendar: " + start.Format("January 2006")),
|
2020-02-05 17:57:16 +00:00
|
|
|
Time: start,
|
2020-05-20 16:39:37 +00:00
|
|
|
Now: time.Now(), // TODO: Use client time zone
|
2020-02-05 17:08:00 +00:00
|
|
|
Calendar: calendar,
|
2020-05-20 16:39:37 +00:00
|
|
|
Dates: dates,
|
2020-05-13 15:10:21 +00:00
|
|
|
Events: newCalendarObjectList(events),
|
2020-02-05 17:57:16 +00:00
|
|
|
PrevPage: start.AddDate(0, -1, 0).Format(monthPageLayout),
|
|
|
|
NextPage: start.AddDate(0, 1, 0).Format(monthPageLayout),
|
2020-05-20 16:39:37 +00:00
|
|
|
PrevTime: start.AddDate(0, -1, 0),
|
|
|
|
NextTime: start.AddDate(0, 1, 0),
|
|
|
|
|
|
|
|
EventsForDate: func(when time.Time) []CalendarObject {
|
2020-05-20 17:08:06 +00:00
|
|
|
if events, ok := eventMap[when.Truncate(time.Hour*24)]; ok {
|
2020-05-20 16:39:37 +00:00
|
|
|
return events
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
|
|
|
|
DaySuffix: func(n int) string {
|
2020-05-20 17:08:06 +00:00
|
|
|
if n%100 >= 11 && n%100 <= 13 {
|
2020-05-20 16:39:37 +00:00
|
|
|
return "th"
|
|
|
|
}
|
|
|
|
return map[int]string{
|
|
|
|
0: "th",
|
|
|
|
1: "st",
|
|
|
|
2: "nd",
|
|
|
|
3: "rd",
|
|
|
|
4: "th",
|
|
|
|
5: "th",
|
|
|
|
6: "th",
|
|
|
|
7: "th",
|
|
|
|
8: "th",
|
|
|
|
9: "th",
|
2020-05-20 17:08:06 +00:00
|
|
|
}[n%10]
|
2020-05-20 16:39:37 +00:00
|
|
|
},
|
|
|
|
|
2020-05-20 17:08:06 +00:00
|
|
|
Sub: func(a, b int) int {
|
2020-05-20 16:39:37 +00:00
|
|
|
// Why isn't this built-in, come on Go
|
|
|
|
return a - b
|
|
|
|
},
|
2020-02-05 17:08:00 +00:00
|
|
|
})
|
|
|
|
})
|
2020-02-05 17:39:53 +00:00
|
|
|
|
2020-05-13 14:48:03 +00:00
|
|
|
p.GET("/calendar/:path", func(ctx *alps.Context) error {
|
|
|
|
path, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:39:53 +00:00
|
|
|
c, calendar, err := getCalendar(u, ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:48:03 +00:00
|
|
|
multiGet := caldav.CalendarMultiGet{
|
2020-02-05 17:39:53 +00:00
|
|
|
CompRequest: caldav.CalendarCompRequest{
|
|
|
|
Name: "VCALENDAR",
|
|
|
|
Props: []string{"VERSION"},
|
|
|
|
Comps: []caldav.CalendarCompRequest{{
|
|
|
|
Name: "VEVENT",
|
|
|
|
Props: []string{
|
|
|
|
"SUMMARY",
|
|
|
|
"DESCRIPTION",
|
|
|
|
"UID",
|
|
|
|
"DTSTART",
|
|
|
|
"DTEND",
|
|
|
|
"DURATION",
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
2020-05-13 14:48:03 +00:00
|
|
|
|
|
|
|
events, err := c.MultiGetCalendar(path, &multiGet)
|
2020-02-05 17:39:53 +00:00
|
|
|
if err != nil {
|
2020-05-13 14:48:03 +00:00
|
|
|
return fmt.Errorf("failed to multi-get calendar: %v", err)
|
2020-02-05 17:39:53 +00:00
|
|
|
}
|
|
|
|
if len(events) != 1 {
|
2020-05-13 14:48:03 +00:00
|
|
|
return fmt.Errorf("expected exactly one calendar object with path %q, got %v", path, len(events))
|
2020-02-05 17:39:53 +00:00
|
|
|
}
|
|
|
|
event := &events[0]
|
2020-05-20 17:47:54 +00:00
|
|
|
summary, _ := event.Data.Events()[0].Props.Text("SUMMARY")
|
2020-02-05 17:39:53 +00:00
|
|
|
|
|
|
|
return ctx.Render(http.StatusOK, "event.html", &EventRenderData{
|
2020-05-20 17:47:54 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx).WithTitle(summary),
|
2020-02-05 17:39:53 +00:00
|
|
|
Calendar: calendar,
|
2020-05-13 15:10:21 +00:00
|
|
|
Event: CalendarObject{event},
|
2020-02-05 17:39:53 +00:00
|
|
|
})
|
|
|
|
})
|
2020-05-13 17:42:41 +00:00
|
|
|
|
|
|
|
updateEvent := func(ctx *alps.Context) error {
|
|
|
|
calendarObjectPath, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, calendar, err := getCalendar(u, ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var co *caldav.CalendarObject
|
|
|
|
var event *ical.Event
|
|
|
|
if calendarObjectPath != "" {
|
2020-05-20 15:45:32 +00:00
|
|
|
co, err = c.GetCalendarObject(calendarObjectPath)
|
2020-05-13 17:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get CalDAV event: %v", err)
|
|
|
|
}
|
|
|
|
events := co.Data.Events()
|
|
|
|
if len(events) != 1 {
|
|
|
|
return fmt.Errorf("expected exactly one event, got %d", len(events))
|
|
|
|
}
|
|
|
|
event = &events[0]
|
|
|
|
} else {
|
|
|
|
event = ical.NewEvent()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Request().Method == "POST" {
|
|
|
|
summary := ctx.FormValue("summary")
|
2020-05-20 15:08:24 +00:00
|
|
|
description := ctx.FormValue("description")
|
|
|
|
|
2020-05-20 17:32:53 +00:00
|
|
|
// TODO: whole-day events
|
|
|
|
start, err := parseTime(ctx.FormValue("start-date"), ctx.FormValue("start-time"))
|
2020-05-13 17:42:41 +00:00
|
|
|
if err != nil {
|
2020-05-20 17:32:53 +00:00
|
|
|
return err
|
2020-05-13 17:42:41 +00:00
|
|
|
}
|
2020-05-20 17:32:53 +00:00
|
|
|
end, err := parseTime(ctx.FormValue("end-date"), ctx.FormValue("end-time"))
|
2020-05-13 17:42:41 +00:00
|
|
|
if err != nil {
|
2020-05-20 17:32:53 +00:00
|
|
|
return err
|
2020-05-13 17:42:41 +00:00
|
|
|
}
|
|
|
|
if start.After(end) {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "event start is after its end")
|
|
|
|
}
|
|
|
|
|
|
|
|
if start == end {
|
|
|
|
end = start.Add(24 * time.Hour)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.Props.SetDateTime(ical.PropDateTimeStamp, time.Now())
|
|
|
|
event.Props.SetText(ical.PropSummary, summary)
|
|
|
|
event.Props.SetDateTime(ical.PropDateTimeStart, start)
|
|
|
|
event.Props.SetDateTime(ical.PropDateTimeEnd, end)
|
|
|
|
event.Props.Del(ical.PropDuration)
|
|
|
|
|
2020-05-20 15:08:24 +00:00
|
|
|
if description != "" {
|
|
|
|
description = strings.ReplaceAll(description, "\r", "")
|
|
|
|
event.Props.SetText(ical.PropDescription, description)
|
|
|
|
} else {
|
|
|
|
event.Props.Del(ical.PropDescription)
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:42:41 +00:00
|
|
|
newID := uuid.New()
|
|
|
|
if prop := event.Props.Get(ical.PropUID); prop == nil {
|
|
|
|
event.Props.SetText(ical.PropUID, newID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
cal := ical.NewCalendar()
|
|
|
|
cal.Props.SetText(ical.PropProductID, "-//emersion.fr//alps//EN")
|
|
|
|
cal.Props.SetText(ical.PropVersion, "2.0")
|
|
|
|
cal.Children = append(cal.Children, event.Component)
|
|
|
|
|
|
|
|
var p string
|
|
|
|
if co != nil {
|
|
|
|
p = co.Path
|
|
|
|
} else {
|
|
|
|
p = path.Join(calendar.Path, newID.String()+".ics")
|
|
|
|
}
|
|
|
|
co, err = c.PutCalendarObject(p, cal)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to put calendar object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Redirect(http.StatusFound, CalendarObject{co}.URL())
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:47:54 +00:00
|
|
|
summary, _ := event.Props.Text("SUMMARY")
|
|
|
|
|
2020-05-13 17:42:41 +00:00
|
|
|
return ctx.Render(http.StatusOK, "update-event.html", &UpdateEventRenderData{
|
2020-05-20 17:47:54 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx).WithTitle("Update " + summary),
|
2020-05-20 17:40:52 +00:00
|
|
|
Calendar: calendar,
|
2020-05-13 17:42:41 +00:00
|
|
|
CalendarObject: co,
|
|
|
|
Event: event,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
p.GET("/calendar/create", updateEvent)
|
|
|
|
p.POST("/calendar/create", updateEvent)
|
|
|
|
|
|
|
|
p.GET("/calendar/:path/update", updateEvent)
|
|
|
|
p.POST("/calendar/:path/update", updateEvent)
|
2020-05-20 14:54:33 +00:00
|
|
|
|
|
|
|
p.POST("/calendar/:path/delete", func(ctx *alps.Context) error {
|
|
|
|
path, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, _, err := getCalendar(u, ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.RemoveAll(path); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete calendar object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Redirect(http.StatusFound, "/calendar")
|
|
|
|
})
|
2020-02-05 17:08:00 +00:00
|
|
|
}
|