Skip to content

Commit ac357b4

Browse files
several updates
only send messages with private-api if necessary add refresh-contacts bot command contacts cache no longer updates automatically to reduce load on bb
1 parent 91a6ebf commit ac357b4

File tree

7 files changed

+55
-36
lines changed

7 files changed

+55
-36
lines changed

commands.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
var (
28-
HelpSectionCreatingPortals = commands.HelpSection{Name: "Creating portals", Order: 15}
28+
HelpSectionManagingPortals = commands.HelpSection{Name: "Managing portals", Order: 15}
2929
)
3030

3131
type WrappedCommandEvent struct {
@@ -40,6 +40,7 @@ func (br *IMBridge) RegisterCommands() {
4040
proc.AddHandlers(
4141
cmdPM,
4242
cmdSearchContacts,
43+
cmdRefreshContacts,
4344
)
4445
}
4546

@@ -59,7 +60,7 @@ var cmdPM = &commands.FullHandler{
5960
Func: wrapCommand(fnPM),
6061
Name: "pm",
6162
Help: commands.HelpMeta{
62-
Section: HelpSectionCreatingPortals,
63+
Section: HelpSectionManagingPortals,
6364
Description: "Creates a new PM with the specified number or address.",
6465
},
6566
RequiresPortal: false,
@@ -91,8 +92,8 @@ var cmdSearchContacts = &commands.FullHandler{
9192
Func: wrapCommand(fnSearchContacts),
9293
Name: "search-contacts",
9394
Help: commands.HelpMeta{
94-
Section: HelpSectionCreatingPortals,
95-
Description: "Searches contacts based on name, phone, and email.",
95+
Section: HelpSectionManagingPortals,
96+
Description: "Searches contacts based on name, phone, and email (only for BlueBubbles mode).",
9697
},
9798
RequiresPortal: false,
9899
RequiresLogin: false,
@@ -152,5 +153,24 @@ func buildContactString(contact *imessage.Contact) string {
152153
return contactInfo
153154
}
154155

155-
// TODO: potentially add the following commands
156-
// start-group-chat
156+
var cmdRefreshContacts = &commands.FullHandler{
157+
Func: wrapCommand(fnRefreshContacts),
158+
Name: "refresh-contacts",
159+
Help: commands.HelpMeta{
160+
Section: HelpSectionManagingPortals,
161+
Description: "Request that the bridge reload cached contacts (only for BlueBubbles mode).",
162+
},
163+
RequiresPortal: false,
164+
RequiresLogin: false,
165+
}
166+
167+
func fnRefreshContacts(ce *WrappedCommandEvent) {
168+
ce.Bridge.ZLog.Trace().Interface("args", ce.Args).Str("cmd", ce.Command).Msg("fnSearchContacts")
169+
170+
err := ce.Bridge.IM.RefreshContactList()
171+
if err != nil {
172+
ce.Reply("Failed to search contacts: %s", err)
173+
} else {
174+
ce.Reply("Contacts List updated!")
175+
}
176+
}

imessage/bluebubbles/api.go

+8-22
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ func (bb *blueBubbles) Start(readyCallback func()) error {
9393

9494
go bb.PollForWebsocketMessages()
9595

96+
// Preload some caches
9697
bb.usingPrivateApi = bb.isPrivateApi()
98+
bb.RefreshContactList()
9799

100+
// Notify main this API is fully loaded
98101
readyCallback()
99102

100103
return nil
@@ -709,11 +712,9 @@ func (bb *blueBubbles) matchHandleToContact(address string) *Contact {
709712

710713
var contact *Contact
711714

712-
contacts := bb.getContactList()
713-
714715
numericAddress := numericOnly(address)
715716

716-
for _, c := range contacts {
717+
for _, c := range bb.contacts {
717718
// extract only the numbers of every phone (removes `-` and `+`)
718719
var numericPhones []string
719720
for _, e := range c.PhoneNumbers {
@@ -754,9 +755,7 @@ func (bb *blueBubbles) SearchContactList(input string) ([]*imessage.Contact, err
754755

755756
var matchedContacts []*imessage.Contact
756757

757-
contacts := bb.getContactList()
758-
759-
for _, contact := range contacts {
758+
for _, contact := range bb.contacts {
760759

761760
contactFields := []string{
762761
strings.ToLower(contact.FirstName + " " + contact.LastName),
@@ -804,28 +803,15 @@ func (bb *blueBubbles) GetContactInfo(identifier string) (resp *imessage.Contact
804803
func (bb *blueBubbles) GetContactList() (resp []*imessage.Contact, err error) {
805804
bb.log.Trace().Msg("GetContactList")
806805

807-
contactResponse := bb.getContactList()
808-
809-
for _, contact := range contactResponse {
806+
for _, contact := range bb.contacts {
810807
imessageContact, _ := bb.convertBBContactToiMessageContact(&contact)
811808
resp = append(resp, imessageContact)
812809
}
813810

814811
return resp, nil
815812
}
816813

817-
// Updates the cache if necessary, and returns the list
818-
func (bb *blueBubbles) getContactList() []Contact {
819-
820-
if bb.contacts == nil ||
821-
bb.contactsLastRefresh.Add(1*time.Hour).Compare(time.Now()) < 0 {
822-
bb.refreshContactsList()
823-
}
824-
825-
return bb.contacts
826-
}
827-
828-
func (bb *blueBubbles) refreshContactsList() error {
814+
func (bb *blueBubbles) RefreshContactList() error {
829815
bb.log.Trace().Msg("refreshContactsList")
830816

831817
var contactResponse ContactResponse
@@ -945,7 +931,7 @@ func (bb *blueBubbles) SendMessage(chatID, text string, replyTo string, replyToP
945931
bb.log.Trace().Str("chatID", chatID).Str("text", text).Str("replyTo", replyTo).Int("replyToPart", replyToPart).Any("richLink", richLink).Interface("metadata", metadata).Msg("SendMessage")
946932

947933
var method string
948-
if replyTo != "" || bb.usingPrivateApi {
934+
if replyTo != "" {
949935
method = "private-api"
950936
} else {
951937
// we have to use apple-script and send a second message

imessage/bluebubbles/interface.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ type Handle struct {
231231
}
232232

233233
type SendTextRequest struct {
234-
ChatGUID string `json:"chatGuid"`
235-
Method string `json:"method"`
236-
Message string `json:"message"`
237-
EffectId interface{} `json:"effectId"`
238-
Subject string `json:"subject"`
239-
TempGuid string `json:"tempGuid"`
240-
SelectedMessageGuid string `json:"selectedMessageGuid"`
241-
PartIndex int `json:"partIndex"`
234+
ChatGUID string `json:"chatGuid"`
235+
TempGuid string `json:"tempGuid"`
236+
Method string `json:"method"`
237+
Message string `json:"message"`
238+
EffectId string `json:"effectId,omitempty"`
239+
Subject string `json:"subject,omitempty"`
240+
SelectedMessageGuid string `json:"selectedMessageGuid,omitempty"`
241+
PartIndex int `json:"partIndex,omitempty"`
242242
}
243243

244244
type SendTextResponse struct {

imessage/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ContactAPI interface {
3939
GetContactInfo(identifier string) (*Contact, error)
4040
GetContactList() ([]*Contact, error)
4141
SearchContactList(searchTerms string) ([]*Contact, error)
42+
RefreshContactList() error
4243
}
4344

4445
type ChatInfoAPI interface {

imessage/ios/ipc.go

+4
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ func (ios *iOSConnector) SearchContactList(searchTerms string) ([]*imessage.Cont
481481
return nil, errors.New("not implemented")
482482
}
483483

484+
func (ios *iOSConnector) RefreshContactList() error {
485+
return errors.New("not implemented")
486+
}
487+
484488
func (ios *iOSConnector) GetChatInfo(chatID, threadID string) (*imessage.ChatInfo, error) {
485489
var resp imessage.ChatInfo
486490
err := ios.IPC.Request(context.Background(), ReqGetChat, &GetChatRequest{ChatGUID: chatID, ThreadID: threadID}, &resp)

imessage/mac-nosip/nosip.go

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (n NoopContacts) SearchContactList(searchTerms string) ([]*imessage.Contact
7171
return nil, errors.New("not implemented")
7272
}
7373

74+
func (n NoopContacts) RefreshContactList() error {
75+
return errors.New("not implemented")
76+
}
77+
7478
func NewMacNoSIPConnector(bridge imessage.Bridge) (imessage.API, error) {
7579
logger := bridge.GetLog().Sub("iMessage").Sub("Mac-noSIP")
7680
processLogger := bridge.GetLog().Sub("iMessage").Sub("Barcelona")

imessage/mac/contacts.go

+4
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,7 @@ func (cs *ContactStore) GetContactList() ([]*imessage.Contact, error) {
163163
func (cs ContactStore) SearchContactList(searchTerms string) ([]*imessage.Contact, error) {
164164
return nil, errors.New("not implemented")
165165
}
166+
167+
func (cs ContactStore) RefreshContactList() error {
168+
return errors.New("not implemented")
169+
}

0 commit comments

Comments
 (0)