plugins/carddav: add view to edit contacts
This commit is contained in:
parent
c4ff33e645
commit
778e6f9c89
4 changed files with 62 additions and 12 deletions
2
go.mod
2
go.mod
|
@ -26,3 +26,5 @@ require (
|
||||||
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect
|
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect
|
||||||
layeh.com/gopher-luar v1.0.7
|
layeh.com/gopher-luar v1.0.7
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace github.com/emersion/go-webdav => ../go-webdav
|
||||||
|
|
|
@ -10,6 +10,12 @@
|
||||||
|
|
||||||
<h2>Contact: {{$fn}}</h2>
|
<h2>Contact: {{$fn}}</h2>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<a href="/contacts/{{.AddressObject.Card.Value "UID" | pathescape}}/edit">
|
||||||
|
Edit
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li><strong>Name</strong>: {{$fn}}</li>
|
<li><strong>Name</strong>: {{$fn}}</li>
|
||||||
{{range .AddressObject.Card.Values "EMAIL"}}
|
{{range .AddressObject.Card.Values "EMAIL"}}
|
||||||
|
|
|
@ -27,7 +27,7 @@ type AddressObjectRenderData struct {
|
||||||
type UpdateAddressObjectRenderData struct {
|
type UpdateAddressObjectRenderData struct {
|
||||||
koushin.BaseRenderData
|
koushin.BaseRenderData
|
||||||
AddressObject *carddav.AddressObject // nil if creating a new contact
|
AddressObject *carddav.AddressObject // nil if creating a new contact
|
||||||
Card vcard.Card
|
Card vcard.Card
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerRoutes(p *plugin) {
|
func registerRoutes(p *plugin) {
|
||||||
|
@ -117,8 +117,39 @@ func registerRoutes(p *plugin) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
createContact := func(ctx *koushin.Context) error {
|
updateContact := func(ctx *koushin.Context) error {
|
||||||
card := make(vcard.Card)
|
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" {
|
if ctx.Request().Method == "POST" {
|
||||||
fn := ctx.FormValue("fn")
|
fn := ctx.FormValue("fn")
|
||||||
|
@ -152,12 +183,12 @@ func registerRoutes(p *plugin) {
|
||||||
card.SetValue(vcard.FieldUID, id.URN())
|
card.SetValue(vcard.FieldUID, id.URN())
|
||||||
}
|
}
|
||||||
|
|
||||||
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
var p string
|
||||||
if err != nil {
|
if ao != nil {
|
||||||
return err
|
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)
|
_, 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)
|
||||||
|
@ -165,13 +196,19 @@ func registerRoutes(p *plugin) {
|
||||||
// TODO: check if the returned AddressObject's path matches, if not
|
// TODO: check if the returned AddressObject's path matches, if not
|
||||||
// fetch the new UID (the server may mutate it)
|
// 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{
|
return ctx.Render(http.StatusOK, "update-address-object.html", &UpdateAddressObjectRenderData{
|
||||||
BaseRenderData: *koushin.NewBaseRenderData(ctx),
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,12 @@
|
||||||
<h2>{{$fn}}</h2>
|
<h2>{{$fn}}</h2>
|
||||||
<ul class="nav nav-tabs">
|
<ul class="nav nav-tabs">
|
||||||
<li class="nav-item">
|
<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>
|
||||||
<li class="mr-auto d-none d-sm-flex"></li>
|
<li class="mr-auto d-none d-sm-flex"></li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
|
|
Loading…
Reference in a new issue