carddav: implement AddressBook.CreateAddressObject
This commit is contained in:
parent
5cc5a2f843
commit
49ff810a48
|
@ -1,6 +1,8 @@
|
||||||
package carddav
|
package carddav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -53,6 +55,10 @@ func (ao *addressObject) ID() string {
|
||||||
return ao.contact.ID
|
return ao.contact.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ao *addressObject) Stat() (os.FileInfo, error) {
|
||||||
|
return &addressFileInfo{ao.contact}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ao *addressObject) Card() (vcard.Card, error) {
|
func (ao *addressObject) Card() (vcard.Card, error) {
|
||||||
card := make(vcard.Card)
|
card := make(vcard.Card)
|
||||||
|
|
||||||
|
@ -80,8 +86,8 @@ func (ao *addressObject) Card() (vcard.Card, error) {
|
||||||
return card, nil
|
return card, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *addressObject) Stat() (os.FileInfo, error) {
|
func (ao *addressObject) SetCard(card vcard.Card) error {
|
||||||
return &addressFileInfo{ao.contact}, nil
|
return errors.New("hydroxide/carddav: not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
type addressBook struct {
|
type addressBook struct {
|
||||||
|
@ -207,6 +213,42 @@ func (ab *addressBook) GetAddressObject(id string) (carddav.AddressObject, error
|
||||||
return ao, nil
|
return ao, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
if len(resps) != 1 {
|
||||||
|
return nil, errors.New("hydroxide/carddav: expected exactly one response when creating contact")
|
||||||
|
}
|
||||||
|
resp := resps[0]
|
||||||
|
if err := resp.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
contact := resp.Response.Contact
|
||||||
|
contact.Cards = contactImport.Cards // Not returned by the server
|
||||||
|
|
||||||
|
ao := &addressObject{
|
||||||
|
c: ab.c,
|
||||||
|
contact: contact,
|
||||||
|
}
|
||||||
|
ab.cacheAddressObject(ao)
|
||||||
|
return ao, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ab *addressBook) receiveEvents(events <-chan *protonmail.Event) {
|
func (ab *addressBook) receiveEvents(events <-chan *protonmail.Event) {
|
||||||
for event := range events {
|
for event := range events {
|
||||||
ab.locker.Lock()
|
ab.locker.Lock()
|
||||||
|
|
|
@ -73,6 +73,10 @@ type ContactExport struct {
|
||||||
Cards []*ContactCard
|
Cards []*ContactCard
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ContactImport struct {
|
||||||
|
Cards []*ContactCard
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) ListContacts(page, pageSize int) (total int, contacts []*Contact, err error) {
|
func (c *Client) ListContacts(page, pageSize int) (total int, contacts []*Contact, err error) {
|
||||||
v := url.Values{}
|
v := url.Values{}
|
||||||
v.Set("Page", strconv.Itoa(page))
|
v.Set("Page", strconv.Itoa(page))
|
||||||
|
@ -163,7 +167,7 @@ func (c *Client) GetContact(id string) (*Contact, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateContactResp struct {
|
type CreateContactResp struct {
|
||||||
Input *Contact
|
Index int
|
||||||
Response struct {
|
Response struct {
|
||||||
resp
|
resp
|
||||||
Contact *Contact
|
Contact *Contact
|
||||||
|
@ -174,10 +178,11 @@ func (resp *CreateContactResp) Err() error {
|
||||||
return resp.Response.Err()
|
return resp.Response.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateContacts(contacts []*Contact) ([]*CreateContactResp, error) {
|
func (c *Client) CreateContacts(contacts []*ContactImport) ([]*CreateContactResp, error) {
|
||||||
reqData := struct {
|
reqData := struct {
|
||||||
Contacts []*Contact
|
Contacts []*ContactImport
|
||||||
}{contacts}
|
Overwrite, Groups, Labels int
|
||||||
|
}{contacts, 0, 0, 0}
|
||||||
req, err := c.newJSONRequest(http.MethodPost, "/contacts", &reqData)
|
req, err := c.newJSONRequest(http.MethodPost, "/contacts", &reqData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue