carddav: implement AddressObject.SetCard

This commit is contained in:
emersion 2017-09-13 10:11:14 +02:00
parent 49ff810a48
commit 0447370b16
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 32 additions and 16 deletions

View File

@ -18,6 +18,21 @@ type contextKey string
const ClientContextKey = contextKey("client")
func formatCard(card vcard.Card) (*protonmail.ContactImport, error) {
// TODO: sign/encrypt stuff
var b bytes.Buffer
if err := vcard.NewEncoder(&b).Encode(card); err != nil {
return nil, err
}
return &protonmail.ContactImport{
Cards: []*protonmail.ContactCard{
{Data: b.String()},
},
}, nil
}
type addressFileInfo struct {
contact *protonmail.Contact
}
@ -87,7 +102,19 @@ func (ao *addressObject) Card() (vcard.Card, error) {
}
func (ao *addressObject) SetCard(card vcard.Card) error {
return errors.New("hydroxide/carddav: not yet implemented")
contactImport, err := formatCard(card)
if err != nil {
return err
}
contact, err := ao.c.UpdateContact(ao.contact.ID, contactImport)
if err != nil {
return err
}
contact.Cards = contactImport.Cards // Not returned by the server
ao.contact = contact
return nil
}
type addressBook struct {
@ -214,19 +241,11 @@ func (ab *addressBook) GetAddressObject(id string) (carddav.AddressObject, error
}
func (ab *addressBook) CreateAddressObject(card vcard.Card) (carddav.AddressObject, error) {
// TODO: sign/encrypt stuff
var b bytes.Buffer
if err := vcard.NewEncoder(&b).Encode(card); err != nil {
contactImport, err := formatCard(card)
if err != nil {
return nil, err
}
contactImport := &protonmail.ContactImport{
Cards: []*protonmail.ContactCard{
{Data: b.String()},
},
}
resps, err := ab.c.CreateContacts([]*protonmail.ContactImport{contactImport})
if err != nil {
return nil, err

View File

@ -199,11 +199,8 @@ func (c *Client) CreateContacts(contacts []*ContactImport) ([]*CreateContactResp
return respData.Responses, nil
}
func (c *Client) UpdateContact(id string, cards []*ContactCard) (*Contact, error) {
reqData := struct {
Cards []*ContactCard
}{cards}
req, err := c.newJSONRequest(http.MethodPut, "/contacts/"+id, &reqData)
func (c *Client) UpdateContact(id string, contact *ContactImport) (*Contact, error) {
req, err := c.newJSONRequest(http.MethodPut, "/contacts/"+id, contact)
if err != nil {
return nil, err
}