carddav: add displayname and addressbook-description to server

This commit is contained in:
Simon Ser 2020-01-19 14:53:58 +01:00
parent 797b2f8fc5
commit edfc2804b5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
5 changed files with 28 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
type AddressBook struct {
Href string
Name string
Description string
}

View File

@ -76,7 +76,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
l = append(l, AddressBook{
Href: href,
Description: descProp.Data,
Description: descProp.Description,
})
}

View File

@ -26,7 +26,7 @@ type addressbookHomeSet struct {
type addressbookDescription struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-description"`
Data string `xml:",chardata"`
Description string `xml:",chardata"`
}
// https://tools.ietf.org/html/rfc6352#section-10.3

View File

@ -1,6 +1,7 @@
package carddav
import (
"bytes"
"encoding/xml"
"net/http"
@ -11,6 +12,7 @@ import (
// TODO: add support for multiple address books
type Backend interface {
AddressBook() (*AddressBook, error)
GetAddressObject(href string) (*AddressObject, error)
ListAddressObjects() ([]AddressObject, error)
QueryAddressObjects(query *AddressBookQuery) ([]AddressObject, error)
@ -157,7 +159,12 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
var resps []internal.Response
if r.URL.Path == "/" {
resp, err := b.propfindAddressBook(propfind)
ab, err := b.Backend.AddressBook()
if err != nil {
return nil, err
}
resp, err := b.propfindAddressBook(propfind, ab)
if err != nil {
return nil, err
}
@ -193,12 +200,18 @@ func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth i
return internal.NewMultistatus(resps...), nil
}
func (b *backend) propfindAddressBook(propfind *internal.Propfind) (*internal.Response, error) {
func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBook) (*internal.Response, error) {
props := map[xml.Name]internal.PropfindFunc{
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return internal.NewResourceType(internal.CollectionName, addressBookName), nil
},
// TODO: displayname, addressbook-description, addressbook-supported-address-data, addressbook-max-resource-size, addressbook-home-set
internal.DisplayNameName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.DisplayName{Name: ab.Name}, nil
},
addressBookDescriptionName: func(*internal.RawXMLValue) (interface{}, error) {
return &addressbookDescription{Description: ab.Description}, nil
},
// TODO: addressbook-supported-address-data, addressbook-max-resource-size, addressbook-home-set
}
return internal.NewPropfindResponse("/", propfind, props)
@ -209,7 +222,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.GetContentType{Type: vcard.MIMEType}, nil
},
addressBookDataName: func(*internal.RawXMLValue) (interface{}, error) {
addressDataName: func(*internal.RawXMLValue) (interface{}, error) {
var buf bytes.Buffer
if err := vcard.NewEncoder(&buf).Encode(ao.Card); err != nil {
return nil, err

View File

@ -303,6 +303,7 @@ type GetLastModified struct {
var (
ResourceTypeName = xml.Name{"DAV:", "resourcetype"}
DisplayNameName = xml.Name{"DAV:", "displayname"}
GetContentLengthName = xml.Name{"DAV:", "getcontentlength"}
GetContentTypeName = xml.Name{"DAV:", "getcontenttype"}
GetLastModifiedName = xml.Name{"DAV:", "getlastmodified"}
@ -314,3 +315,9 @@ type Error struct {
XMLName xml.Name `xml:"DAV: error"`
Raw []RawXMLValue `xml:",any"`
}
// https://tools.ietf.org/html/rfc4918#section-15.2
type DisplayName struct {
XMLName xml.Name `xml:"DAV: displayname"`
Name string `xml:",chardata"`
}