diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index a7678d295..3c283a29b 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -2890,7 +2890,17 @@ func (m *Manager) GetByIDString(idString string) (*Community, error) { return m.GetByID(id) } -func (m *Manager) RequestToJoin(requester *ecdsa.PublicKey, request *requests.RequestToJoinCommunity) (*Community, *RequestToJoin, error) { +func (m *Manager) SaveRequestToJoinAndCommunity(requestToJoin *RequestToJoin, community *Community) (*Community, *RequestToJoin, error) { + if err := m.persistence.SaveRequestToJoin(requestToJoin); err != nil { + return nil, nil, err + } + community.config.RequestedToJoinAt = uint64(time.Now().Unix()) + community.AddRequestToJoin(requestToJoin) + + return community, requestToJoin, nil +} + +func (m *Manager) CreateRequestToJoin(requester *ecdsa.PublicKey, request *requests.RequestToJoinCommunity) (*Community, *RequestToJoin, error) { community, err := m.persistence.GetByID(&m.identity.PublicKey, request.CommunityID) if err != nil { return nil, nil, err @@ -2914,12 +2924,6 @@ func (m *Manager) RequestToJoin(requester *ecdsa.PublicKey, request *requests.Re requestToJoin.CalculateID() - if err := m.persistence.SaveRequestToJoin(requestToJoin); err != nil { - return nil, nil, err - } - community.config.RequestedToJoinAt = uint64(time.Now().Unix()) - community.AddRequestToJoin(requestToJoin) - return community, requestToJoin, nil } diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 8adfdadb0..7530d9986 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -3123,7 +3123,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityBanUserRequesToJoin() { request := &requests.RequestToJoinCommunity{CommunityID: community.ID()} // We try to join the org - _, rtj, err := s.alice.communitiesManager.RequestToJoin(&s.alice.identity.PublicKey, request) + _, rtj, err := s.alice.communitiesManager.CreateRequestToJoin(&s.alice.identity.PublicKey, request) s.Require().NoError(err) diff --git a/protocol/communities_messenger_token_permissions_test.go b/protocol/communities_messenger_token_permissions_test.go index 984664a3c..7376eb929 100644 --- a/protocol/communities_messenger_token_permissions_test.go +++ b/protocol/communities_messenger_token_permissions_test.go @@ -578,6 +578,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions( err = <-waitOnCommunityEncryptionErrCh s.Require().NoError(err) + _, err = WaitOnMessengerResponse( + s.bob, + func(r *MessengerResponse) bool { + return len(r.Communities()) > 0 + }, + "no community", + ) + s.Require().NoError(err) + // bob should be kicked from the community, // because he doesn't meet the criteria community, err = s.owner.communitiesManager.GetByID(community.ID()) @@ -603,6 +612,18 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions( s.Require().Error(err) s.Require().ErrorContains(err, "no messages") + // bob tries to join, but he doesn't satisfy so the request isn't sent + passwdHash := types.EncodeHex(crypto.Keccak256([]byte(bobPassword))) + request := &requests.RequestToJoinCommunity{CommunityID: community.ID(), Password: passwdHash, AddressesToReveal: []string{bobAddress}} + _, err = s.bob.RequestToJoinCommunity(request) + s.Require().Error(err) + s.Require().ErrorContains(err, "permission to join not satisfied") + + // make sure bob does not have a pending request to join + requests, err := s.bob.MyPendingRequestsToJoin() + s.Require().NoError(err) + s.Require().Len(requests, 0) + // make bob satisfy the criteria s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequest.TokenCriteria[0]) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index d285606c5..18cb7e1d5 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -743,11 +743,7 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun return nil, err } - community, requestToJoin, err := m.communitiesManager.RequestToJoin(&m.identity.PublicKey, request) - if err != nil { - return nil, err - } - err = m.syncCommunity(context.Background(), community, m.dispatchMessage) + community, requestToJoin, err := m.communitiesManager.CreateRequestToJoin(&m.identity.PublicKey, request) if err != nil { return nil, err } @@ -777,6 +773,9 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun if err != nil { return nil, err } + if !response.Satisfied { + return nil, errors.New("permission to join not satisfied") + } for _, accountAndChainIDs := range response.ValidCombinations { revealedAccounts[accountAndChainIDs.Address].ChainIds = accountAndChainIDs.ChainIDs @@ -787,6 +786,15 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun } } + community, _, err = m.communitiesManager.SaveRequestToJoinAndCommunity(requestToJoin, community) + if err != nil { + return nil, err + } + err = m.syncCommunity(context.Background(), community, m.dispatchMessage) + if err != nil { + return nil, err + } + payload, err := proto.Marshal(requestToJoinProto) if err != nil { return nil, err