feat: kicked/banned member should not have spectated mode after the kick/ban (#4806)
This commit is contained in:
parent
54ea0981a5
commit
92bc64bb41
|
@ -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()
|
||||||
|
if spectateMode {
|
||||||
community.Spectate()
|
community.Spectate()
|
||||||
|
}
|
||||||
|
|
||||||
if err = m.persistence.SaveCommunity(community); err != nil {
|
if err = m.persistence.SaveCommunity(community); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -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",
|
||||||
)
|
)
|
||||||
|
|
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1793,18 +1793,20 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !spectateMode {
|
||||||
err = m.DeleteProfileShowcaseCommunity(community)
|
err = m.DeleteProfileShowcaseCommunity(community)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response.AddCommunity(community)
|
response.AddCommunity(community)
|
||||||
return response, nil
|
return response, nil
|
||||||
|
@ -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,19 +4321,22 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !ownerhipChange {
|
||||||
// Activity Center notification
|
// Activity Center notification
|
||||||
notification := &ActivityCenterNotification{
|
notification := &ActivityCenterNotification{
|
||||||
ID: types.FromHex(uuid.New().String()),
|
ID: types.FromHex(uuid.New().String()),
|
||||||
Type: acType,
|
Type: acType,
|
||||||
Timestamp: m.getTimesource().GetCurrentTime(),
|
Timestamp: m.getTimesource().GetCurrentTime(),
|
||||||
CommunityID: community.IDString(),
|
CommunityID: changes.Community.IDString(),
|
||||||
Read: false,
|
Read: false,
|
||||||
UpdatedAt: m.GetCurrentTimeInMillis(),
|
UpdatedAt: m.GetCurrentTimeInMillis(),
|
||||||
}
|
}
|
||||||
|
@ -4341,6 +4346,7 @@ func (m *Messenger) leaveCommunityDueToKickOrBan(community *communities.Communit
|
||||||
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 {
|
||||||
m.logger.Error("cannot merge leave and notification response", zap.Error(err))
|
m.logger.Error("cannot merge leave and notification response", zap.Error(err))
|
||||||
|
|
Loading…
Reference in New Issue