aboutsummaryrefslogtreecommitdiff
path: root/plugins/caldav/caldav.go
blob: 05d57fe25bbf89f291fccf8ba9b464f6fed0dfa4 (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
67
68
69
70
71
72
73
74
75
76
77
package alpscaldav

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

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

var errNoCalendar = fmt.Errorf("caldav: no calendar found")

type authRoundTripper struct {
	upstream http.RoundTripper
	session  *alps.Session
}

func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	rt.session.SetHTTPBasicAuth(req)
	return rt.upstream.RoundTrip(req)
}

func newClient(u *url.URL, session *alps.Session) (*caldav.Client, error) {
	rt := authRoundTripper{
		upstream: http.DefaultTransport,
		session:  session,
	}
	c, err := caldav.NewClient(&http.Client{Transport: &rt}, u.String())
	if err != nil {
		return nil, fmt.Errorf("failed to create CalDAV client: %v", err)
	}

	return c, nil
}

func getCalendar(u *url.URL, session *alps.Session) (*caldav.Client, *caldav.Calendar, error) {
	c, err := newClient(u, session)
	if err != nil {
		return nil, nil, err
	}

	principal, err := c.FindCurrentUserPrincipal()
	if err != nil {
		return nil, nil, fmt.Errorf("failed to query CalDAV principal: %v", err)
	}

	calendarHomeSet, err := c.FindCalendarHomeSet(principal)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to query CalDAV calendar home set: %v", err)
	}

	calendars, err := c.FindCalendars(calendarHomeSet)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to find calendars: %v", err)
	}
	if len(calendars) == 0 {
		return nil, nil, errNoCalendar
	}
	return c, &calendars[0], nil
}

type CalendarObject struct {
	*caldav.CalendarObject
}

func newCalendarObjectList(cos []caldav.CalendarObject) []CalendarObject {
	l := make([]CalendarObject, len(cos))
	for i := range cos {
		l[i] = CalendarObject{&cos[i]}
	}
	return l
}

func (ao CalendarObject) URL() string {
	return "/calendar/" + url.PathEscape(ao.Path)
}