Skip to content

Commit 0cac2af

Browse files
authored
fix(community): getting kicked out of a community should still spectate (#4217)
Fixes status-im/status-desktop#12558 When getting kicked out of a community, before we used to leave the community completely, but just keep the filters on. That created a problem when reopening the app, because the community disappeared and could even create a problem in desktop where it tried to open the last opened community but it's no longer there. The fix now is that when getting kicked out, we instead just remove ourselves from the community and set Joined to false, but we keep the community spectated.
1 parent e304fe3 commit 0cac2af

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

protocol/communities/community_changes.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ type CommunityChanges struct {
4242
// automatically
4343
ShouldMemberJoin bool `json:"memberAdded"`
4444

45-
// ShouldMemberJoin indicates whether the user should leave this community
46-
// automatically
47-
ShouldMemberLeave bool `json:"memberRemoved"`
45+
// MemberKicked indicates whether the user has been kicked out
46+
MemberKicked bool `json:"memberRemoved"`
4847
}
4948

5049
func EmptyCommunityChanges() *CommunityChanges {

protocol/communities/manager.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,8 @@ func (m *Manager) handleCommunityDescriptionMessageCommon(community *Community,
16841684
}
16851685

16861686
if changes.HasMemberLeft(pkString) {
1687-
// If we joined previously the community, we should leave it
1688-
changes.ShouldMemberLeave = community.Joined()
1687+
// If we joined previously the community, that means we have been kicked
1688+
changes.MemberKicked = community.Joined()
16891689
}
16901690
}
16911691

@@ -2964,6 +2964,27 @@ func (m *Manager) LeaveCommunity(id types.HexBytes) (*Community, error) {
29642964
return community, nil
29652965
}
29662966

2967+
// Same as LeaveCommunity, but we want to stay spectating
2968+
func (m *Manager) KickedOutOfCommunity(id types.HexBytes) (*Community, error) {
2969+
community, err := m.GetByID(id)
2970+
if err != nil {
2971+
return nil, err
2972+
}
2973+
if community == nil {
2974+
return nil, ErrOrgNotFound
2975+
}
2976+
2977+
community.RemoveOurselvesFromOrg(&m.identity.PublicKey)
2978+
community.Leave()
2979+
community.Spectate()
2980+
2981+
if err = m.persistence.SaveCommunity(community); err != nil {
2982+
return nil, err
2983+
}
2984+
2985+
return community, nil
2986+
}
2987+
29672988
func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecdsa.PublicKey) (*Community, error) {
29682989
community, err := m.GetByID(communityID)
29692990
if err != nil {

protocol/messenger_communities.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ func (m *Messenger) LeaveCommunity(communityID types.HexBytes) (*MessengerRespon
17021702
return nil, err
17031703
}
17041704

1705-
mr, err := m.leaveCommunity(communityID, true)
1705+
mr, err := m.leaveCommunity(communityID)
17061706
if err != nil {
17071707
return nil, err
17081708
}
@@ -1756,7 +1756,7 @@ func (m *Messenger) LeaveCommunity(communityID types.HexBytes) (*MessengerRespon
17561756
return mr, nil
17571757
}
17581758

1759-
func (m *Messenger) leaveCommunity(communityID types.HexBytes, unsubsribeFromCommunity bool) (*MessengerResponse, error) {
1759+
func (m *Messenger) leaveCommunity(communityID types.HexBytes) (*MessengerResponse, error) {
17601760
response := &MessengerResponse{}
17611761

17621762
community, err := m.communitiesManager.LeaveCommunity(communityID)
@@ -1779,11 +1779,21 @@ func (m *Messenger) leaveCommunity(communityID types.HexBytes, unsubsribeFromCom
17791779
}
17801780
}
17811781

1782-
if unsubsribeFromCommunity {
1783-
_, err = m.transport.RemoveFilterByChatID(communityID.String())
1784-
if err != nil {
1785-
return nil, err
1786-
}
1782+
_, err = m.transport.RemoveFilterByChatID(communityID.String())
1783+
if err != nil {
1784+
return nil, err
1785+
}
1786+
1787+
response.AddCommunity(community)
1788+
return response, nil
1789+
}
1790+
1791+
func (m *Messenger) kickedOutOfCommunity(communityID types.HexBytes) (*MessengerResponse, error) {
1792+
response := &MessengerResponse{}
1793+
1794+
community, err := m.communitiesManager.KickedOutOfCommunity(communityID)
1795+
if err != nil {
1796+
return nil, err
17871797
}
17881798

17891799
response.AddCommunity(community)
@@ -3230,7 +3240,7 @@ func (m *Messenger) handleSyncInstallationCommunity(messageState *ReceivedMessag
32303240
return err
32313241
}
32323242
} else {
3233-
mr, err = m.leaveCommunity(syncCommunity.Id, true)
3243+
mr, err = m.leaveCommunity(syncCommunity.Id)
32343244
if err != nil {
32353245
logger.Debug("m.leaveCommunity error", zap.Error(err))
32363246
return err
@@ -5865,12 +5875,8 @@ func (m *Messenger) processCommunityChanges(messageState *ReceivedMessageState)
58655875
continue
58665876
}
58675877

5868-
} else if changes.ShouldMemberLeave {
5869-
// this means we've been kicked by the community owner/admin,
5870-
// in this case we don't want to unsubscribe from community updates
5871-
// so we still get notified accordingly when something changes,
5872-
// hence, we're setting `unsubscribeFromCommunity` to `false` here
5873-
response, err := m.leaveCommunity(changes.Community.ID(), false)
5878+
} else if changes.MemberKicked {
5879+
response, err := m.kickedOutOfCommunity(changes.Community.ID())
58745880
if err != nil {
58755881
m.logger.Error("cannot leave community", zap.Error(err))
58765882
continue

0 commit comments

Comments
 (0)