Skip to content

Commit

Permalink
GetChatInfo implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed Jan 15, 2024
1 parent e85718d commit f35d92c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
74 changes: 69 additions & 5 deletions imessage/bluebubbles/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (bb *blueBubbles) GetMessage(guid string) (resp *imessage.Message, err erro
return nil, ErrNotImplemented
}

func (bb *blueBubbles) apiUrl(path string) string {
func (bb *blueBubbles) apiUrl(path string, queryParams *map[string]string) string {
u, err := url.Parse(bb.bridge.GetConnectorConfig().BlueBubblesURL)
if err != nil {
bb.log.Error().Err(err).Msg("Error parsing BlueBubbles URL")
Expand All @@ -273,17 +273,52 @@ func (bb *blueBubbles) apiUrl(path string) string {

q := u.Query()
q.Add("password", bb.bridge.GetConnectorConfig().BlueBubblesPassword)

if queryParams != nil {
for key, value := range *queryParams {
q.Add(key, value)
}
}

u.RawQuery = q.Encode()

url := u.String()

return url
}

func (bb *blueBubbles) apiPost(path string, payload interface{}, target interface{}) (err error) {
url := bb.apiUrl(path)
func (bb *blueBubbles) apiGet(path string, queryParams *map[string]string, target interface{}) (err error) {
url := bb.apiUrl(path, queryParams)

bb.log.Info().Str("url", url).Msg("Making POST request")
// debug log url
bb.log.Debug().Str("url", url).Msg("URL")

response, err := http.Get(url)
if err != nil {
bb.log.Error().Err(err).Msg("Error making GET request")
return err
}
defer response.Body.Close()

responseBody, err := io.ReadAll(response.Body)
if err != nil {
bb.log.Error().Err(err).Msg("Error reading response body")
return err
}

// debug log responseBody
bb.log.Debug().Str("responseBody", string(responseBody)).Msg("Response body")

if err := json.Unmarshal(responseBody, target); err != nil {
bb.log.Error().Err(err).Msg("Error unmarshalling response body")
return err
}

return nil
}

func (bb *blueBubbles) apiPost(path string, payload interface{}, target interface{}) (err error) {
url := bb.apiUrl(path, nil)

payloadJSON, err := json.Marshal(payload)
if err != nil {
Expand Down Expand Up @@ -377,7 +412,36 @@ func (bb *blueBubbles) GetContactList() (resp []*imessage.Contact, err error) {

func (bb *blueBubbles) GetChatInfo(chatID, threadID string) (*imessage.ChatInfo, error) {
bb.log.Trace().Str("chatID", chatID).Str("threadID", threadID).Msg("GetChatInfo")
return nil, ErrNotImplemented

var chatResponse ChatResponse

// DEVNOTE: it doesn't appear we need to URL Encode the chatID... 😬
err := bb.apiGet(fmt.Sprintf("/api/v1/chat/%s", chatID), &map[string]string{
"with": "participants",
}, &chatResponse)
if err != nil {
return nil, err
}

if chatResponse.Data.GroupId != threadID {
return nil, errors.New("threadID does not match")
}

members := make([]string, len(chatResponse.Data.Partipants))

for i, participant := range chatResponse.Data.Partipants {
members[i] = participant.Address
}

chatInfo := &imessage.ChatInfo{
JSONChatGUID: chatResponse.Data.GUID,
Identifier: imessage.ParseIdentifier(chatResponse.Data.GUID),
DisplayName: chatResponse.Data.DisplayName,
Members: members,
ThreadID: chatResponse.Data.GroupId,
}

return chatInfo, nil
}

func (bb *blueBubbles) GetGroupAvatar(chatID string) (*imessage.Attachment, error) {
Expand Down
18 changes: 15 additions & 3 deletions imessage/bluebubbles/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ type ChatQueryResponse struct {
Metadata PageMetadata `json:"metadata"`
}

type ChatResponse struct {
Status int64 `json:"status"`
Message string `json:"message"`
Data *Chat `json:"data,omitempty"`
}

type Chat struct {
// TODO How to get timestamp
GUID string `json:"guid"`
ChatIdentifier string `json:"chatIdentifier"`
GroupId string `json:"groupId"`
GUID string `json:"guid"`
ChatIdentifier string `json:"chatIdentifier"`
GroupId string `json:"groupId,omitempty"`
DisplayName string `json:"displayName"`
Partipants []Participant `json:"participants"`
}

type Participant struct {
Address string `json:"address"`
}

type ContactResponse struct {
Expand Down

0 comments on commit f35d92c

Please sign in to comment.