From 60caa08868668d66f37360668b84a95f6af1a966 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 30 Jul 2022 16:05:39 -0700 Subject: [PATCH] readd deletecontact and wire in --- event/common.go | 3 +++ peer/cwtch_peer.go | 7 ++++++- protocol/connections/engine.go | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/event/common.go b/event/common.go index 8829752..34e0bf9 100644 --- a/event/common.go +++ b/event/common.go @@ -129,6 +129,9 @@ const ( // Data [serialized *model.Group] GroupCreated = Type("GroupCreated") + // RemotePeer + DeleteContact = Type("DeleteContact") + // PeerStateChange servers as a new incoming connection message as well, and can/is consumed by frontends to alert of new p2p connections // RemotePeer // ConnectionState diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 99c4ffa..a0d5498 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -614,7 +614,12 @@ func (cp *cwtchPeer) FetchConversationInfo(handle string) (*model.Conversation, func (cp *cwtchPeer) DeleteConversation(id int) error { cp.mutex.Lock() defer cp.mutex.Unlock() - return cp.storage.DeleteConversation(id) + ci, err := cp.storage.GetConversation(id) + if err == nil && ci != nil { + cp.eventBus.Publish(event.NewEventList(event.DeleteContact, event.RemotePeer, ci.Handle)) + return cp.storage.DeleteConversation(id) + } + return fmt.Errorf("could not delete conversation, did not exist") } // SetConversationAttribute sets the conversation attribute at path to value diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 3b1e413..44d6e4e 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -101,6 +101,7 @@ func NewProtocolEngine(identity primitives.Identity, privateKey ed25519.PrivateK engine.eventManager.Subscribe(event.SendMessageToPeer, engine.queue) engine.eventManager.Subscribe(event.SendGetValMessageToPeer, engine.queue) engine.eventManager.Subscribe(event.SendRetValMessageToPeer, engine.queue) + engine.eventManager.Subscribe(event.DeleteContact, engine.queue) engine.eventManager.Subscribe(event.UpdateConversationAuthorization, engine.queue) engine.eventManager.Subscribe(event.BlockUnknownPeers, engine.queue) @@ -159,6 +160,11 @@ func (e *engine) eventHandler() { go e.peerWithTokenServer(ev.Data[event.GroupServer], ev.Data[event.ServerTokenOnion], ev.Data[event.ServerTokenY], signature) case event.LeaveServer: e.leaveServer(ev.Data[event.GroupServer]) + case event.DeleteContact: + onion := ev.Data[event.RemotePeer] + // We remove this peer from out blocklist which will prevent them from contacting us if we have "block unknown peers" turned on. + e.authorizations.Delete(ev.Data[event.RemotePeer]) + e.deleteConnection(onion) case event.SendMessageToGroup: ciphertext, _ := base64.StdEncoding.DecodeString(ev.Data[event.Ciphertext]) signature, _ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) @@ -525,6 +531,13 @@ func (e *engine) sendRetValToPeer(eventID, onion, val, existsStr string) error { return e.sendPeerMessage(onion, pmodel.PeerMessage{ID: eventID, Context: event.ContextRetVal, Data: message}) } +func (e *engine) deleteConnection(id string) { + conn, err := e.service.GetConnection(id) + if err == nil { + conn.Close() + } +} + // receiveGroupMessage is a callback function that processes GroupMessages from a given server func (e *engine) receiveGroupMessage(server string, gm *groups.EncryptedGroupMessage) { // Publish Event so that a Profile Engine can deal with it.