From 00dc2e60e5317133d40748f132c5bf44c2142ab9 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sat, 8 May 2021 12:03:09 -0700 Subject: [PATCH] Wire up SendMessageToGroupError (Also makes this flow much more efficient by including groupId in the round trip) --- model/profile.go | 13 ++++++------- peer/cwtch_peer.go | 7 ++++--- protocol/connections/engine.go | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/model/profile.go b/model/profile.go index 99ceada..fceac0a 100644 --- a/model/profile.go +++ b/model/profile.go @@ -216,13 +216,12 @@ func (p *Profile) AckSentMessageToPeer(onion string, eventID string) int { } // AddGroupSentMessageError searches matching groups for the message by sig and marks it as an error -func (p *Profile) AddGroupSentMessageError(groupServer string, signature string, error string) { - for _, group := range p.Groups { - if group.GroupServer == groupServer { - if group.ErrorSentMessage([]byte(signature), error) { - break - } - } +func (p *Profile) AddGroupSentMessageError(groupID string, signature []byte, error string) { + p.lock.Lock() + defer p.lock.Unlock() + group, exists := p.Groups[groupID] + if exists { + group.ErrorSentMessage(signature, error) } } diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index e5abba4..d9f83a6 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -482,10 +482,10 @@ func (cp *cwtchPeer) SendMessageToGroupTracked(groupid string, message string) ( ct, sig, err := cp.Profile.EncryptMessageToGroup(message, groupid) if err == nil { - cp.eventBus.Publish(event.NewEvent(event.SendMessageToGroup, map[event.Field]string{event.GroupServer: group.GroupServer, event.Ciphertext: base64.StdEncoding.EncodeToString(ct), event.Signature: base64.StdEncoding.EncodeToString(sig)})) + cp.eventBus.Publish(event.NewEvent(event.SendMessageToGroup, map[event.Field]string{event.GroupID: groupid, event.GroupServer: group.GroupServer, event.Ciphertext: base64.StdEncoding.EncodeToString(ct), event.Signature: base64.StdEncoding.EncodeToString(sig)})) } - return string(sig), err + return base64.StdEncoding.EncodeToString(sig), err } func (cp *cwtchPeer) SendMessageToPeer(onion string, message string) string { @@ -702,7 +702,8 @@ func (cp *cwtchPeer) eventHandler() { case event.SendMessageToGroupError: cp.mutex.Lock() - cp.Profile.AddGroupSentMessageError(ev.Data[event.GroupServer], ev.Data[event.Signature], ev.Data[event.Error]) + signature, _ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) + cp.Profile.AddGroupSentMessageError(ev.Data[event.GroupID], signature, ev.Data[event.Error]) cp.mutex.Unlock() case event.SendMessageToPeerError: diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 15b43c6..dbeaf70 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -54,6 +54,8 @@ type engine struct { // Engine (ProtocolEngine) encapsulates the logic necessary to make and receive Cwtch connections. // Note: ProtocolEngine doesn't have access to any information necessary to encrypt or decrypt GroupMessages +// Protocol Engine *can* associate Group Identifiers with Group Servers, although we don't currently make use of this fact +// other than to route errors back to the UI. type Engine interface { ACN() connectivity.ACN EventManager() event.Manager @@ -153,7 +155,7 @@ func (e *engine) eventHandler() { signature, _ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) err := e.sendMessageToGroup(ev.Data[event.GroupServer], ciphertext, signature) if err != nil { - e.eventManager.Publish(event.NewEvent(event.SendMessageToGroupError, map[event.Field]string{event.GroupServer: ev.Data[event.GroupServer], event.EventID: ev.EventID, event.Error: err.Error()})) + e.eventManager.Publish(event.NewEvent(event.SendMessageToGroupError, map[event.Field]string{event.GroupID: ev.Data[event.GroupID], event.GroupServer: ev.Data[event.GroupServer], event.EventID: ev.EventID, event.Error: err.Error(), event.Signature: ev.Data[event.Signature]})) } case event.SendMessageToPeer: // TODO: remove this passthrough once the UI is integrated.