plugins/carddav: add view to edit contacts

This commit is contained in:
Simon Ser 2020-02-12 21:35:18 +01:00
parent c4ff33e645
commit 778e6f9c89
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
4 changed files with 62 additions and 12 deletions

2
go.mod
View file

@ -26,3 +26,5 @@ require (
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect
layeh.com/gopher-luar v1.0.7
)
replace github.com/emersion/go-webdav => ../go-webdav

View file

@ -10,6 +10,12 @@
<h2>Contact: {{$fn}}</h2>
<p>
<a href="/contacts/{{.AddressObject.Card.Value "UID" | pathescape}}/edit">
Edit
</a>
</p>
<ul>
<li><strong>Name</strong>: {{$fn}}</li>
{{range .AddressObject.Card.Values "EMAIL"}}

View file

@ -117,8 +117,39 @@ func registerRoutes(p *plugin) {
})
})
createContact := func(ctx *koushin.Context) error {
card := make(vcard.Card)
updateContact := func(ctx *koushin.Context) error {
uid := ctx.Param("uid")
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
if err != nil {
return err
}
var ao *carddav.AddressObject
var card vcard.Card
if uid != "" {
query := carddav.AddressBookQuery{
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 {
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
} else {
card = make(vcard.Card)
}
if ctx.Request().Method == "POST" {
fn := ctx.FormValue("fn")
@ -152,12 +183,12 @@ func registerRoutes(p *plugin) {
card.SetValue(vcard.FieldUID, id.URN())
}
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
if err != nil {
return err
var p string
if ao != nil {
p = ao.Path
} else {
p = path.Join(addressBook.Path, id.String()+".vcf")
}
p := path.Join(addressBook.Path, id.String() + ".vcf")
_, err = c.PutAddressObject(p, card)
if err != nil {
return fmt.Errorf("failed to put address object: %v", err)
@ -165,13 +196,19 @@ func registerRoutes(p *plugin) {
// 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, "/contacts/"+card.Value(vcard.FieldUID))
}
return ctx.Render(http.StatusOK, "update-address-object.html", &UpdateAddressObjectRenderData{
BaseRenderData: *koushin.NewBaseRenderData(ctx),
AddressObject: ao,
Card: card,
})
}
p.GET("/contacts/create", createContact)
p.POST("/contacts/create", createContact)
p.GET("/contacts/create", updateContact)
p.POST("/contacts/create", updateContact)
p.GET("/contacts/:uid/edit", updateContact)
p.POST("/contacts/:uid/edit", updateContact)
}

View file

@ -9,7 +9,12 @@
<h2>{{$fn}}</h2>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">View contact</a>
<a class="nav-link active" href="#">View</a>
</li>
<li class="nav-item">
<a class="nav-link"
href="/contacts/{{.AddressObject.Card.Value "UID" | pathescape}}/edit"
>Edit</a>
</li>
<li class="mr-auto d-none d-sm-flex"></li>
<li class="nav-item">