plugins/carddav: cache addressbook home set path

This commit is contained in:
Simon Ser 2020-02-11 19:14:05 +01:00
parent 13f5414691
commit 3a3887b358
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 50 additions and 31 deletions

View file

@ -21,32 +21,10 @@ func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
return rt.upstream.RoundTrip(req)
}
func getAddressBook(u *url.URL, session *koushin.Session) (*carddav.Client, *carddav.AddressBook, error) {
func newClient(u *url.URL, session *koushin.Session) (*carddav.Client, error) {
rt := authRoundTripper{
upstream: http.DefaultTransport,
session: session,
}
c, err := carddav.NewClient(&http.Client{Transport: &rt}, u.String())
if err != nil {
return nil, nil, fmt.Errorf("failed to create CardDAV client: %v", err)
}
principal, err := c.FindCurrentUserPrincipal()
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV principal: %v", err)
}
addressBookHomeSet, err := c.FindAddressBookHomeSet(principal)
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV address book home set: %v", err)
}
addressBooks, err := c.FindAddressBooks(addressBookHomeSet)
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV address books: %v", err)
}
if len(addressBooks) == 0 {
return nil, nil, errNoAddressBook
}
return c, &addressBooks[0], nil
return carddav.NewClient(&http.Client{Transport: &rt}, u.String())
}

View file

@ -30,6 +30,44 @@ func sanityCheckURL(u *url.URL) error {
return nil
}
type plugin struct {
koushin.GoPlugin
url *url.URL
homeSetCache map[string]string
}
func (p *plugin) clientWithAddressBook(session *koushin.Session) (*carddav.Client, *carddav.AddressBook, error) {
c, err := newClient(p.url, session)
if err != nil {
return nil, nil, fmt.Errorf("failed to create CardDAV client: %v", err)
}
homeSet, ok := p.homeSetCache[session.Username()]
if !ok {
principal, err := c.FindCurrentUserPrincipal()
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV principal: %v", err)
}
homeSet, err = c.FindAddressBookHomeSet(principal)
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV address book home set: %v", err)
}
p.homeSetCache[session.Username()] = homeSet
// TODO: evict entries from the cache if it's getting too big
}
addressBooks, err := c.FindAddressBooks(homeSet)
if err != nil {
return nil, nil, fmt.Errorf("failed to query CardDAV address books: %v", err)
}
if len(addressBooks) == 0 {
return nil, nil, errNoAddressBook
}
return c, &addressBooks[0], nil
}
func newPlugin(srv *koushin.Server) (koushin.Plugin, error) {
u, err := srv.Upstream("carddavs", "carddav+insecure", "https", "http+insecure")
if _, ok := err.(*koushin.NoUpstreamError); ok {
@ -61,14 +99,18 @@ func newPlugin(srv *koushin.Server) (koushin.Plugin, error) {
srv.Logger().Printf("Configured upstream CardDAV server: %v", u)
p := koushin.GoPlugin{Name: "carddav"}
p := &plugin{
GoPlugin: koushin.GoPlugin{Name: "carddav"},
url: u,
homeSetCache: make(map[string]string),
}
registerRoutes(&p, u)
registerRoutes(p)
p.Inject("compose.html", func(ctx *koushin.Context, _data koushin.RenderData) error {
data := _data.(*koushinbase.ComposeRenderData)
c, addressBook, err := getAddressBook(u, ctx.Session)
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
if err == errNoAddressBook {
return nil
} else if err != nil {

View file

@ -3,7 +3,6 @@ package koushincarddav
import (
"fmt"
"net/http"
"net/url"
"git.sr.ht/~emersion/koushin"
"github.com/emersion/go-vcard"
@ -22,11 +21,11 @@ type AddressObjectRenderData struct {
AddressObject *carddav.AddressObject
}
func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
func registerRoutes(p *plugin) {
p.GET("/contacts", func(ctx *koushin.Context) error {
queryText := ctx.QueryParam("query")
c, addressBook, err := getAddressBook(u, ctx.Session)
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
if err != nil {
return err
}
@ -70,7 +69,7 @@ func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
p.GET("/contacts/:uid", func(ctx *koushin.Context) error {
uid := ctx.Param("uid")
c, addressBook, err := getAddressBook(u, ctx.Session)
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
if err != nil {
return err
}