aboutsummaryrefslogtreecommitdiff
path: root/plugins/caldav/routes.go
blob: acc879ff714a70f96a5520c9b488ea20aa5ca877 (plain) (blame)
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
package koushincaldav

import (
	"fmt"
	"net/http"
	"net/url"
	"time"

	"git.sr.ht/~emersion/koushin"
	"github.com/emersion/go-webdav/caldav"
)

type CalendarRenderData struct {
	koushin.BaseRenderData
	Calendar *caldav.Calendar
	Events   []caldav.CalendarObject
}

func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
	p.GET("/calendar", func(ctx *koushin.Context) error {
		// TODO: multi-calendar support
		c, calendar, err := getCalendar(u, ctx.Session)
		if err != nil {
			return err
		}

		now := time.Now()
		start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
		end := start.AddDate(0, 1, 0)

		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)
		}

		return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{
			BaseRenderData: *koushin.NewBaseRenderData(ctx),
			Calendar:       calendar,
			Events:         events,
		})
	})
}