From e679b5f47b4f8dfef8b6d1c762359c50c16e70a5 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 28 Apr 2021 15:13:43 -0700 Subject: [PATCH] Accept/Reject Group Invite --- go.mod | 2 +- go.sum | 4 ++++ lib.go | 50 ++++++++++++++++++++++++++++++++----------- utils/eventHandler.go | 4 +--- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 538ccdd..0191de6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.openprivacy.ca/flutter/libcwtch-go go 1.15 require ( - cwtch.im/cwtch v0.6.7 + cwtch.im/cwtch v0.6.9 git.openprivacy.ca/openprivacy/connectivity v1.4.2 git.openprivacy.ca/openprivacy/log v1.0.2 golang.org/x/mobile v0.0.0-20210220033013-bdb1ca9a1e08 // indirect diff --git a/go.sum b/go.sum index 1c4d5c8..6e0d143 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,10 @@ cwtch.im/cwtch v0.6.6 h1:GlEoAYfastcimhYsctLSbqql0RNz9t1jphAxTcCvu0M= cwtch.im/cwtch v0.6.6/go.mod h1:KDy4lWWxcYAjeKclwVFkoTQ2dWnZcM0k3Xck+zEuBmE= cwtch.im/cwtch v0.6.7 h1:HerkAptEtiDNeaeebgcOHAbkqpGnHo8mbmBtbgjc8PU= cwtch.im/cwtch v0.6.7/go.mod h1:KDy4lWWxcYAjeKclwVFkoTQ2dWnZcM0k3Xck+zEuBmE= +cwtch.im/cwtch v0.6.8 h1:xwPrhqwc9S2+XIEPVL1JDrfJ0Ct7AtW5+K46g+mymdc= +cwtch.im/cwtch v0.6.8/go.mod h1:KDy4lWWxcYAjeKclwVFkoTQ2dWnZcM0k3Xck+zEuBmE= +cwtch.im/cwtch v0.6.9 h1:R4UgKd8ucw8qGZ0K0RrYB+tZrgCXJ83HsH/MrNfsqps= +cwtch.im/cwtch v0.6.9/go.mod h1:KDy4lWWxcYAjeKclwVFkoTQ2dWnZcM0k3Xck+zEuBmE= cwtch.im/tapir v0.2.1 h1:t1YJB9q5sV1A9xwiiwL6WVfw3dwQWLoecunuzT1PQtw= cwtch.im/tapir v0.2.1/go.mod h1:xzzZ28adyUXNkYL1YodcHsAiTt3IJ8Loc29YVn9mIEQ= git.openprivacy.ca/cwtch.im/tapir v0.3.2 h1:thLWqqY1LkirWFcy9Tg6NgWeYbvo9xBm+s2XVnCIvpY= diff --git a/lib.go b/lib.go index 74ccd8e..9857613 100644 --- a/lib.go +++ b/lib.go @@ -30,7 +30,9 @@ import ( ) const ( - profileOnion = event.Field("profileOnion") + // ProfileOnion is an event field that contains the handle for a given profile. + // todo: this should probably be moved back into Cwtch, and renamed ProfileHandle (onions are too tor-specific) + ProfileOnion = event.Field("ProfileOnion") ) var application app.Application @@ -353,16 +355,40 @@ func c_AcceptContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, ha AcceptContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen)) } -func AcceptContact(profile, handle string) { - err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthApproved) - if err == nil { - eventHandler.Push(event.NewEvent(event.PeerStateChange, map[event.Field]string{ - profileOnion: profile, - event.RemotePeer: handle, - "authorization": string(model.AuthApproved), - })) +// AcceptContact takes in a profileOnion and a handle to either a group or a peer and authorizes the handle +// for further action (e.g. messaging / connecting to the server / joining the group etc.) +func AcceptContact(profileOnion string, handle string) { + profile := application.GetPeer(profileOnion) + profileHandler := utils.NewPeerHelper(profile) + if profileHandler.IsGroup(handle) { + profile.AcceptInvite(handle) } else { - log.Errorf("error accepting contact: %s", err.Error()) + err := profile.SetContactAuthorization(handle, model.AuthApproved) + if err == nil { + eventHandler.Push(event.NewEvent(event.PeerStateChange, map[event.Field]string{ + ProfileOnion: profileOnion, + event.RemotePeer: handle, + "authorization": string(model.AuthApproved), + })) + } else { + log.Errorf("error accepting contact: %s", err.Error()) + } + } +} + +//export c_RejectInvite +func c_RejectInvite(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) { + RejectInvite(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen)) +} + +// RejectInvite rejects a group invite +func RejectInvite(profileOnion string, handle string) { + log.Debugf("rejecting invite %v for %v", handle, profileOnion) + profile := application.GetPeer(profileOnion) + profileHandler := utils.NewPeerHelper(profile) + if profileHandler.IsGroup(handle) { + profile.RejectInvite(handle) + log.Debugf("successfully rejected invite %v for %v", handle, profileOnion) } } @@ -375,7 +401,7 @@ func BlockContact(profile, handle string) { err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthBlocked) if err == nil { eventHandler.Push(event.NewEvent(event.PeerStateChange, map[event.Field]string{ - profileOnion: profile, + ProfileOnion: profile, event.RemotePeer: handle, "authorization": string(model.AuthBlocked), })) @@ -393,7 +419,7 @@ func DebugResetContact(profile, handle string) { err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthUnknown) if err == nil { application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{ - profileOnion: profile, + ProfileOnion: profile, event.RemotePeer: handle, "authorization": string(model.AuthUnknown), })) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 433f36a..6c8e183 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -162,9 +162,6 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { ph := NewPeerHelper(profile) cpicPath := ph.GetProfilePic(groupId) - // todo hack for now, auto accept groups - profile.AcceptInvite(groupId) - authorization := model.AuthUnknown if group.Accepted { authorization = model.AuthApproved @@ -344,6 +341,7 @@ func (eh *EventHandler) startHandlingPeer(onion string) { eventBus.Subscribe(event.PeerAcknowledgement, q) eventBus.Subscribe(event.NewMessageFromGroup, q) eventBus.Subscribe(event.NewGroupInvite, q) + eventBus.Subscribe(event.AcceptGroupInvite, q) eventBus.Subscribe(event.SendMessageToGroupError, q) eventBus.Subscribe(event.SendMessageToPeerError, q) eventBus.Subscribe(event.ServerStateChange, q)