aboutsummaryrefslogtreecommitdiff
path: root/plugins/carddav/routes.go
blob: 2f338520fb2350fe96f40d96ff76616ff029f4e4 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package koushincarddav

import (
	"fmt"
	"net/http"

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

type AddressBookRenderData struct {
	koushin.BaseRenderData
	AddressBook    *carddav.AddressBook
	AddressObjects []carddav.AddressObject
	Query          string
}

type AddressObjectRenderData struct {
	koushin.BaseRenderData
	AddressObject *carddav.AddressObject
}

func registerRoutes(p *plugin) {
	p.GET("/contacts", func(ctx *koushin.Context) error {
		queryText := ctx.QueryParam("query")

		c, addressBook, err := p.clientWithAddressBook(ctx.Session)
		if err != nil {
			return err
		}

		query := carddav.AddressBookQuery{
			DataRequest: carddav.AddressDataRequest{
				Props: []string{
					vcard.FieldFormattedName,
					vcard.FieldEmail,
					vcard.FieldUID,
				},
			},
		}

		if queryText != "" {
			query.PropFilters = []carddav.PropFilter{
				{
					Name:        vcard.FieldFormattedName,
					TextMatches: []carddav.TextMatch{{Text: queryText}},
				},
				{
					Name:        vcard.FieldEmail,
					TextMatches: []carddav.TextMatch{{Text: queryText}},
				},
			}
		}

		addrs, err := c.QueryAddressBook(addressBook.Path, &query)
		if err != nil {
			return fmt.Errorf("failed to query CardDAV addresses: %v", err)
		}

		return ctx.Render(http.StatusOK, "address-book.html", &AddressBookRenderData{
			BaseRenderData: *koushin.NewBaseRenderData(ctx),
			AddressBook:    addressBook,
			AddressObjects: addrs,
			Query:          queryText,
		})
	})

	p.GET("/contacts/:uid", func(ctx *koushin.Context) error {
		uid := ctx.Param("uid")

		c, addressBook, err := p.clientWithAddressBook(ctx.Session)
		if err != nil {
			return err
		}

		query := carddav.AddressBookQuery{
			DataRequest: carddav.AddressDataRequest{
				Props: []string{
					vcard.FieldFormattedName,
					vcard.FieldEmail,
					vcard.FieldUID,
				},
			},
			PropFilters: []carddav.PropFilter{{
				Name: vcard.FieldUID,
				TextMatches: []carddav.TextMatch{{
					Text:      uid,
					MatchType: carddav.MatchEquals,
				}},
			}},
		}
		addrs, err := c.QueryAddressBook(addressBook.Path, &query)
		if err != nil {
			return fmt.Errorf("failed to query CardDAV address: %v", err)
		}
		if len(addrs) != 1 {
			return fmt.Errorf("expected exactly one address object with UID %q, got %v", uid, len(addrs))
		}
		addr := &addrs[0]

		return ctx.Render(http.StatusOK, "address-object.html", &AddressObjectRenderData{
			BaseRenderData: *koushin.NewBaseRenderData(ctx),
			AddressObject:  addr,
		})
	})
}