Skip to content

Commit

Permalink
GetContactList + New API Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed Jan 15, 2024
1 parent e65e5ed commit e85718d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
55 changes: 4 additions & 51 deletions imessage/bluebubbles/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,64 +350,17 @@ func (bb *blueBubbles) GetContactInfo(identifier string) (*imessage.Contact, err
return nil, ErrNotImplemented
}

type Contact struct {
PhoneNumbers []PhoneNumber `json:"phoneNumbers,omitempty"`
Emails []Email `json:"emails,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Nickname string `json:"nickname,omitempty"`
Birthday string `json:"birthday,omitempty"`
Avatar string `json:"avatar,omitempty"`
SourceType string `json:"sourceType,omitempty"`
ID string `json:"id,omitempty"`
}

type PhoneNumber struct {
Address string `json:"address,omitempty"`
ID interface{} `json:"id,omitempty"`
}

type Email struct {
Address string `json:"address,omitempty"`
ID interface{} `json:"id,omitempty"`
}

func (bb *blueBubbles) GetContactList() (resp []*imessage.Contact, err error) {
bb.log.Trace().Msg("GetContactList")
var contactResponse ContactResponse

url := bb.bridge.GetConnectorConfig().BlueBubblesURL + "/api/v1/contact?password=" + bb.bridge.GetConnectorConfig().BlueBubblesPassword
method := "GET"

client := &http.Client{}
req, err := http.NewRequest(method, url, nil)

if err != nil {
fmt.Println(err)
return nil, err
}
res, err := client.Do(req)
err = bb.apiPost("/api/v1/contact", nil, &contactResponse)
if err != nil {
fmt.Println(err)
return nil, err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return nil, err
}

var contactList []Contact

err = json.Unmarshal(body, &contactList)
if err != nil {
fmt.Println(err)
return nil, err
}

// Convert to imessage.Contact type
for _, contact := range contactList {
for _, contact := range contactResponse.Data {
imessageContact := &imessage.Contact{
FirstName: contact.FirstName,
LastName: contact.LastName,
Expand Down
29 changes: 29 additions & 0 deletions imessage/bluebubbles/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,32 @@ type Chat struct {
ChatIdentifier string `json:"chatIdentifier"`
GroupId string `json:"groupId"`
}

type ContactResponse struct {
Status int64 `json:"status"`
Message string `json:"message"`
Data []Contact `json:"data"`
}

type Contact struct {
PhoneNumbers []PhoneNumber `json:"phoneNumbers,omitempty"`
Emails []Email `json:"emails,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Nickname string `json:"nickname,omitempty"`
Birthday string `json:"birthday,omitempty"`
Avatar string `json:"avatar,omitempty"`
SourceType string `json:"sourceType,omitempty"`
ID string `json:"id,omitempty"`
}

type PhoneNumber struct {
Address string `json:"address,omitempty"`
ID interface{} `json:"id,omitempty"`
}

type Email struct {
Address string `json:"address,omitempty"`
ID interface{} `json:"id,omitempty"`
}

0 comments on commit e85718d

Please sign in to comment.