feat: kicked/banned member should not have spectated mode after the kick/ban (#4806)

This commit is contained in:
Mykhailo Prakhov 2024-02-26 13:33:07 +01:00 committed by GitHub
parent 54ea0981a5
commit 92bc64bb41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 35 deletions

View File

@ -3106,8 +3106,8 @@ func (m *Manager) LeaveCommunity(id types.HexBytes) (*Community, error) {
return community, nil return community, nil
} }
// Same as LeaveCommunity, but we want to stay spectating // Same as LeaveCommunity, but we have an option to stay spectating
func (m *Manager) KickedOutOfCommunity(id types.HexBytes) (*Community, error) { func (m *Manager) KickedOutOfCommunity(id types.HexBytes, spectateMode bool) (*Community, error) {
community, err := m.GetByID(id) community, err := m.GetByID(id)
if err != nil { if err != nil {
return nil, err return nil, err
@ -3115,7 +3115,9 @@ func (m *Manager) KickedOutOfCommunity(id types.HexBytes) (*Community, error) {
community.RemoveOurselvesFromOrg(&m.identity.PublicKey) community.RemoveOurselvesFromOrg(&m.identity.PublicKey)
community.Leave() community.Leave()
community.Spectate() if spectateMode {
community.Spectate()
}
if err = m.persistence.SaveCommunity(community); err != nil { if err = m.persistence.SaveCommunity(community); err != nil {
return nil, err return nil, err

View File

@ -1,20 +1,16 @@
package protocol package protocol
import ( import (
//"bytes"
"context" "context"
"testing" "testing"
"time" "time"
//"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
gethcommon "github.com/ethereum/go-ethereum/common" gethcommon "github.com/ethereum/go-ethereum/common"
hexutil "github.com/ethereum/go-ethereum/common/hexutil" hexutil "github.com/ethereum/go-ethereum/common/hexutil"
//utils "github.com/status-im/status-go/common"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
@ -225,7 +221,9 @@ func (s *MessengerCommunitiesSignersSuite) TestControlNodeUpdateSigner() {
_, err = WaitOnSignaledMessengerResponse( _, err = WaitOnSignaledMessengerResponse(
s.bob, s.bob,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Communities()) > 0 && !r.Communities()[0].HasMember(&s.bob.identity.PublicKey) return len(r.Communities()) > 0 && !r.Communities()[0].HasMember(&s.bob.identity.PublicKey) &&
!r.Communities()[0].Joined() && r.Communities()[0].Spectated() &&
len(r.ActivityCenterNotifications()) == 0
}, },
"Bob was not kicked from the community", "Bob was not kicked from the community",
) )
@ -236,7 +234,8 @@ func (s *MessengerCommunitiesSignersSuite) TestControlNodeUpdateSigner() {
_, err = WaitOnSignaledMessengerResponse( _, err = WaitOnSignaledMessengerResponse(
s.john, s.john,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
wasKicked := len(r.Communities()) > 0 && !r.Communities()[0].HasMember(&s.john.identity.PublicKey) inSpectateMode := len(r.Communities()) > 0 && !r.Communities()[0].HasMember(&s.john.identity.PublicKey) &&
!r.Communities()[0].Joined() && r.Communities()[0].Spectated()
sharedNotificationExist := false sharedNotificationExist := false
for _, acNotification := range r.ActivityCenterNotifications() { for _, acNotification := range r.ActivityCenterNotifications() {
if acNotification.Type == ActivityCenterNotificationTypeShareAccounts { if acNotification.Type == ActivityCenterNotificationTypeShareAccounts {
@ -244,7 +243,7 @@ func (s *MessengerCommunitiesSignersSuite) TestControlNodeUpdateSigner() {
break break
} }
} }
return wasKicked && sharedNotificationExist return inSpectateMode && sharedNotificationExist
}, },
"John was not kicked from the community", "John was not kicked from the community",
) )
@ -278,7 +277,8 @@ func (s *MessengerCommunitiesSignersSuite) TestControlNodeUpdateSigner() {
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
s.bob, s.bob,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&s.bob.identity.PublicKey) return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&s.bob.identity.PublicKey) &&
r.Communities()[0].Joined() && !r.Communities()[0].Spectated()
}, },
"Bob was auto-accepted", "Bob was auto-accepted",
) )

View File

@ -2351,7 +2351,11 @@ func (s *MessengerCommunitiesSuite) TestBanUser() {
return len(r.Communities()) == 1 && return len(r.Communities()) == 1 &&
len(r.Communities()[0].PendingAndBannedMembers()) == 1 && len(r.Communities()[0].PendingAndBannedMembers()) == 1 &&
r.Communities()[0].IsBanned(&s.alice.identity.PublicKey) && r.Communities()[0].IsBanned(&s.alice.identity.PublicKey) &&
len(r.ActivityCenterNotifications()) == 1 && !r.ActivityCenterState().HasSeen len(r.ActivityCenterNotifications()) == 1 &&
!r.ActivityCenterState().HasSeen &&
!r.Communities()[0].Spectated() &&
!r.Communities()[0].Joined()
}, },
"no message about alice ban", "no message about alice ban",
) )
@ -4094,5 +4098,5 @@ func (s *MessengerCommunitiesSuite) TestBanUserAndDeleteAllUserMessages() {
s.Require().True(community.IsBanned(&s.alice.identity.PublicKey)) s.Require().True(community.IsBanned(&s.alice.identity.PublicKey))
s.Require().Len(community.PendingAndBannedMembers(), 1) s.Require().Len(community.PendingAndBannedMembers(), 1)
s.Require().False(community.Joined()) s.Require().False(community.Joined())
s.Require().True(community.Spectated()) s.Require().False(community.Spectated())
} }

View File

@ -1793,17 +1793,19 @@ func (m *Messenger) leaveCommunity(communityID types.HexBytes) (*MessengerRespon
return response, nil return response, nil
} }
func (m *Messenger) kickedOutOfCommunity(communityID types.HexBytes) (*MessengerResponse, error) { func (m *Messenger) kickedOutOfCommunity(communityID types.HexBytes, spectateMode bool) (*MessengerResponse, error) {
response := &MessengerResponse{} response := &MessengerResponse{}
community, err := m.communitiesManager.KickedOutOfCommunity(communityID) community, err := m.communitiesManager.KickedOutOfCommunity(communityID, spectateMode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = m.DeleteProfileShowcaseCommunity(community) if !spectateMode {
if err != nil { err = m.DeleteProfileShowcaseCommunity(community)
return nil, err if err != nil {
return nil, err
}
} }
response.AddCommunity(community) response.AddCommunity(community)
@ -2959,7 +2961,7 @@ func (m *Messenger) HandleCommunityUserKicked(state *ReceivedMessageState, messa
return nil return nil
} }
response, err := m.kickedOutOfCommunity(community.ID()) response, err := m.kickedOutOfCommunity(community.ID(), false)
if err != nil { if err != nil {
m.logger.Error("cannot leave community", zap.Error(err)) m.logger.Error("cannot leave community", zap.Error(err))
return err return err
@ -4205,7 +4207,7 @@ func (m *Messenger) processCommunityChanges(messageState *ReceivedMessageState)
if changes.IsMemberBanned(pkString) { if changes.IsMemberBanned(pkString) {
notificationType = ActivityCenterNotificationTypeCommunityBanned notificationType = ActivityCenterNotificationTypeCommunityBanned
} }
m.leaveCommunityDueToKickOrBan(changes.Community, notificationType, messageState.Response) m.leaveCommunityDueToKickOrBan(changes, notificationType, messageState.Response)
} else if changes.IsMemberUnbanned(pkString) { } else if changes.IsMemberUnbanned(pkString) {
m.AddActivityCenterNotificationToResponse(changes.Community.IDString(), ActivityCenterNotificationTypeCommunityUnbanned, messageState.Response) m.AddActivityCenterNotificationToResponse(changes.Community.IDString(), ActivityCenterNotificationTypeCommunityUnbanned, messageState.Response)
} }
@ -4319,27 +4321,31 @@ func (m *Messenger) AddActivityCenterNotificationToResponse(communityID string,
} }
} }
func (m *Messenger) leaveCommunityDueToKickOrBan(community *communities.Community, acType ActivityCenterType, stateResponse *MessengerResponse) { func (m *Messenger) leaveCommunityDueToKickOrBan(changes *communities.CommunityChanges, acType ActivityCenterType, stateResponse *MessengerResponse) {
response, err := m.kickedOutOfCommunity(community.ID()) // during the ownership change kicked user must stay in the spectate mode
ownerhipChange := changes.ControlNodeChanged != nil
response, err := m.kickedOutOfCommunity(changes.Community.ID(), ownerhipChange)
if err != nil { if err != nil {
m.logger.Error("cannot leave community", zap.Error(err)) m.logger.Error("cannot leave community", zap.Error(err))
return return
} }
// Activity Center notification if !ownerhipChange {
notification := &ActivityCenterNotification{ // Activity Center notification
ID: types.FromHex(uuid.New().String()), notification := &ActivityCenterNotification{
Type: acType, ID: types.FromHex(uuid.New().String()),
Timestamp: m.getTimesource().GetCurrentTime(), Type: acType,
CommunityID: community.IDString(), Timestamp: m.getTimesource().GetCurrentTime(),
Read: false, CommunityID: changes.Community.IDString(),
UpdatedAt: m.GetCurrentTimeInMillis(), Read: false,
} UpdatedAt: m.GetCurrentTimeInMillis(),
}
err = m.addActivityCenterNotification(response, notification, nil) err = m.addActivityCenterNotification(response, notification, nil)
if err != nil { if err != nil {
m.logger.Error("failed to save notification", zap.Error(err)) m.logger.Error("failed to save notification", zap.Error(err))
return return
}
} }
if err := stateResponse.Merge(response); err != nil { if err := stateResponse.Merge(response); err != nil {