plugins/carddav: use paths instead of UIDs in URLs
This commit is contained in:
parent
62853a933e
commit
89149b38c8
8 changed files with 65 additions and 52 deletions
|
@ -28,3 +28,19 @@ func newClient(u *url.URL, session *koushin.Session) (*carddav.Client, error) {
|
||||||
}
|
}
|
||||||
return carddav.NewClient(&http.Client{Transport: &rt}, u.String())
|
return carddav.NewClient(&http.Client{Transport: &rt}, u.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AddressObject struct {
|
||||||
|
*carddav.AddressObject
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAddressObjectList(aos []carddav.AddressObject) []AddressObject {
|
||||||
|
l := make([]AddressObject, len(aos))
|
||||||
|
for i := range aos {
|
||||||
|
l[i] = AddressObject{&aos[i]}
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao AddressObject) URL() string {
|
||||||
|
return "/contacts/" + url.PathEscape(ao.Path)
|
||||||
|
}
|
||||||
|
|
|
@ -37,6 +37,10 @@ type plugin struct {
|
||||||
homeSetCache map[string]string
|
homeSetCache map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *plugin) client(session *koushin.Session) (*carddav.Client, error) {
|
||||||
|
return newClient(p.url, session)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *plugin) clientWithAddressBook(session *koushin.Session) (*carddav.Client, *carddav.AddressBook, error) {
|
func (p *plugin) clientWithAddressBook(session *koushin.Session) (*carddav.Client, *carddav.AddressBook, error) {
|
||||||
c, err := newClient(p.url, session)
|
c, err := newClient(p.url, session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
<ul>
|
<ul>
|
||||||
{{range .AddressObjects}}
|
{{range .AddressObjects}}
|
||||||
<li>
|
<li>
|
||||||
<a href="/contacts/{{.Card.Value "UID" | pathescape}}">
|
<a href="{{.URL}}">
|
||||||
{{.Card.Value "FN"}}
|
{{.Card.Value "FN"}}
|
||||||
</a>
|
</a>
|
||||||
{{$email := .Card.PreferredValue "EMAIL"}}
|
{{$email := .Card.PreferredValue "EMAIL"}}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
<h2>Contact: {{$fn}}</h2>
|
<h2>Contact: {{$fn}}</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a href="/contacts/{{.AddressObject.Card.Value "UID" | pathescape}}/edit">
|
<a href="{{.AddressObject.URL}}/edit">
|
||||||
Edit
|
Edit
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -6,7 +6,9 @@
|
||||||
<a href="/contacts">Back</a>
|
<a href="/contacts">Back</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h2>Edit contact</h2>
|
<h2>
|
||||||
|
{{if .Card}}Edit{{else}}Create{{end}} contact
|
||||||
|
</h2>
|
||||||
|
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<label for="fn">Name:</label>
|
<label for="fn">Name:</label>
|
||||||
|
|
|
@ -3,6 +3,7 @@ package koushincarddav
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -10,18 +11,19 @@ import (
|
||||||
"github.com/emersion/go-vcard"
|
"github.com/emersion/go-vcard"
|
||||||
"github.com/emersion/go-webdav/carddav"
|
"github.com/emersion/go-webdav/carddav"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddressBookRenderData struct {
|
type AddressBookRenderData struct {
|
||||||
koushin.BaseRenderData
|
koushin.BaseRenderData
|
||||||
AddressBook *carddav.AddressBook
|
AddressBook *carddav.AddressBook
|
||||||
AddressObjects []carddav.AddressObject
|
AddressObjects []AddressObject
|
||||||
Query string
|
Query string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddressObjectRenderData struct {
|
type AddressObjectRenderData struct {
|
||||||
koushin.BaseRenderData
|
koushin.BaseRenderData
|
||||||
AddressObject *carddav.AddressObject
|
AddressObject AddressObject
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateAddressObjectRenderData struct {
|
type UpdateAddressObjectRenderData struct {
|
||||||
|
@ -30,6 +32,15 @@ type UpdateAddressObjectRenderData struct {
|
||||||
Card vcard.Card
|
Card vcard.Card
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func registerRoutes(p *plugin) {
|
func registerRoutes(p *plugin) {
|
||||||
p.GET("/contacts", func(ctx *koushin.Context) error {
|
p.GET("/contacts", func(ctx *koushin.Context) error {
|
||||||
queryText := ctx.QueryParam("query")
|
queryText := ctx.QueryParam("query")
|
||||||
|
@ -65,7 +76,7 @@ func registerRoutes(p *plugin) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addrs, err := c.QueryAddressBook(addressBook.Path, &query)
|
aos, err := c.QueryAddressBook(addressBook.Path, &query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to query CardDAV addresses: %v", err)
|
return fmt.Errorf("failed to query CardDAV addresses: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -73,20 +84,23 @@ func registerRoutes(p *plugin) {
|
||||||
return ctx.Render(http.StatusOK, "address-book.html", &AddressBookRenderData{
|
return ctx.Render(http.StatusOK, "address-book.html", &AddressBookRenderData{
|
||||||
BaseRenderData: *koushin.NewBaseRenderData(ctx),
|
BaseRenderData: *koushin.NewBaseRenderData(ctx),
|
||||||
AddressBook: addressBook,
|
AddressBook: addressBook,
|
||||||
AddressObjects: addrs,
|
AddressObjects: newAddressObjectList(aos),
|
||||||
Query: queryText,
|
Query: queryText,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
p.GET("/contacts/:uid", func(ctx *koushin.Context) error {
|
p.GET("/contacts/:path", func(ctx *koushin.Context) error {
|
||||||
uid := ctx.Param("uid")
|
path, err := parseObjectPath(ctx.Param("path"))
|
||||||
|
|
||||||
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
query := carddav.AddressBookQuery{
|
c, err := p.client(ctx.Session)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
multiGet := carddav.AddressBookMultiGet{
|
||||||
DataRequest: carddav.AddressDataRequest{
|
DataRequest: carddav.AddressDataRequest{
|
||||||
Props: []string{
|
Props: []string{
|
||||||
vcard.FieldFormattedName,
|
vcard.FieldFormattedName,
|
||||||
|
@ -94,31 +108,27 @@ func registerRoutes(p *plugin) {
|
||||||
vcard.FieldUID,
|
vcard.FieldUID,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
PropFilters: []carddav.PropFilter{{
|
|
||||||
Name: vcard.FieldUID,
|
|
||||||
TextMatches: []carddav.TextMatch{{
|
|
||||||
Text: uid,
|
|
||||||
MatchType: carddav.MatchEquals,
|
|
||||||
}},
|
|
||||||
}},
|
|
||||||
}
|
}
|
||||||
addrs, err := c.QueryAddressBook(addressBook.Path, &query)
|
aos, err := c.MultiGetAddressBook(path, &multiGet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
||||||
}
|
}
|
||||||
if len(addrs) != 1 {
|
if len(aos) != 1 {
|
||||||
return fmt.Errorf("expected exactly one address object with UID %q, got %v", uid, len(addrs))
|
return fmt.Errorf("expected exactly one address object with path %q, got %v", path, len(aos))
|
||||||
}
|
}
|
||||||
addr := &addrs[0]
|
ao := &aos[0]
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "address-object.html", &AddressObjectRenderData{
|
return ctx.Render(http.StatusOK, "address-object.html", &AddressObjectRenderData{
|
||||||
BaseRenderData: *koushin.NewBaseRenderData(ctx),
|
BaseRenderData: *koushin.NewBaseRenderData(ctx),
|
||||||
AddressObject: addr,
|
AddressObject: AddressObject{ao},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
updateContact := func(ctx *koushin.Context) error {
|
updateContact := func(ctx *koushin.Context) error {
|
||||||
uid := ctx.Param("uid")
|
addressObjectPath, err := parseObjectPath(ctx.Param("path"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -127,25 +137,11 @@ func registerRoutes(p *plugin) {
|
||||||
|
|
||||||
var ao *carddav.AddressObject
|
var ao *carddav.AddressObject
|
||||||
var card vcard.Card
|
var card vcard.Card
|
||||||
if uid != "" {
|
if addressObjectPath != "" {
|
||||||
query := carddav.AddressBookQuery{
|
ao, err := c.GetAddressObject(addressObjectPath)
|
||||||
DataRequest: carddav.AddressDataRequest{AllProp: true},
|
|
||||||
PropFilters: []carddav.PropFilter{{
|
|
||||||
Name: vcard.FieldUID,
|
|
||||||
TextMatches: []carddav.TextMatch{{
|
|
||||||
Text: uid,
|
|
||||||
MatchType: carddav.MatchEquals,
|
|
||||||
}},
|
|
||||||
}},
|
|
||||||
}
|
|
||||||
aos, err := c.QueryAddressBook(addressBook.Path, &query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
||||||
}
|
}
|
||||||
if len(aos) != 1 {
|
|
||||||
return fmt.Errorf("expected exactly one address object with UID %q, got %v", uid, len(aos))
|
|
||||||
}
|
|
||||||
ao = &aos[0]
|
|
||||||
card = ao.Card
|
card = ao.Card
|
||||||
} else {
|
} else {
|
||||||
card = make(vcard.Card)
|
card = make(vcard.Card)
|
||||||
|
@ -189,14 +185,12 @@ func registerRoutes(p *plugin) {
|
||||||
} else {
|
} else {
|
||||||
p = path.Join(addressBook.Path, id.String()+".vcf")
|
p = path.Join(addressBook.Path, id.String()+".vcf")
|
||||||
}
|
}
|
||||||
_, err = c.PutAddressObject(p, card)
|
ao, err = c.PutAddressObject(p, card)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to put address object: %v", err)
|
return fmt.Errorf("failed to put address object: %v", err)
|
||||||
}
|
}
|
||||||
// TODO: check if the returned AddressObject's path matches, if not
|
|
||||||
// fetch the new UID (the server may mutate it)
|
|
||||||
|
|
||||||
return ctx.Redirect(http.StatusFound, "/contacts/"+card.Value(vcard.FieldUID))
|
return ctx.Redirect(http.StatusFound, AddressObject{ao}.URL())
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "update-address-object.html", &UpdateAddressObjectRenderData{
|
return ctx.Render(http.StatusOK, "update-address-object.html", &UpdateAddressObjectRenderData{
|
||||||
|
@ -209,6 +203,6 @@ func registerRoutes(p *plugin) {
|
||||||
p.GET("/contacts/create", updateContact)
|
p.GET("/contacts/create", updateContact)
|
||||||
p.POST("/contacts/create", updateContact)
|
p.POST("/contacts/create", updateContact)
|
||||||
|
|
||||||
p.GET("/contacts/:uid/edit", updateContact)
|
p.GET("/contacts/:path/edit", updateContact)
|
||||||
p.POST("/contacts/:uid/edit", updateContact)
|
p.POST("/contacts/:path/edit", updateContact)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,10 +37,7 @@
|
||||||
<ul class="nav flex-column">
|
<ul class="nav flex-column">
|
||||||
{{range .AddressObjects}}
|
{{range .AddressObjects}}
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a
|
<a class="nav-link" href="{{.URL}}">{{.Card.Value "FN"}}</a>
|
||||||
class="nav-link"
|
|
||||||
href="/contacts/{{.Card.Value "UID" | pathescape}}"
|
|
||||||
>{{.Card.Value "FN"}}</a>
|
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link"
|
<a class="nav-link"
|
||||||
href="/contacts/{{.AddressObject.Card.Value "UID" | pathescape}}/edit"
|
href="{{.AddressObject.URL}}/edit"
|
||||||
>Edit</a>
|
>Edit</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="mr-auto d-none d-sm-flex"></li>
|
<li class="mr-auto d-none d-sm-flex"></li>
|
||||||
|
|
Loading…
Reference in a new issue