Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(community): getting kicked out of a community should still spectate #4217

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions protocol/communities/community_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ type CommunityChanges struct {
// automatically
ShouldMemberJoin bool `json:"memberAdded"`

// ShouldMemberJoin indicates whether the user should leave this community
// automatically
ShouldMemberLeave bool `json:"memberRemoved"`
// MemberKicked indicates whether the user has been kicked out
MemberKicked bool `json:"memberRemoved"`
}

func EmptyCommunityChanges() *CommunityChanges {
Expand Down
25 changes: 23 additions & 2 deletions protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,8 @@ func (m *Manager) handleCommunityDescriptionMessageCommon(community *Community,
}

if changes.HasMemberLeft(pkString) {
// If we joined previously the community, we should leave it
changes.ShouldMemberLeave = community.Joined()
// If we joined previously the community, that means we have been kicked
changes.MemberKicked = community.Joined()
}
}

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

// Same as LeaveCommunity, but we want to stay spectating
func (m *Manager) KickedOutOfCommunity(id types.HexBytes) (*Community, error) {
community, err := m.GetByID(id)
if err != nil {
return nil, err
}
if community == nil {
return nil, ErrOrgNotFound
}

community.RemoveOurselvesFromOrg(&m.identity.PublicKey)
community.Leave()
community.Spectate()

if err = m.persistence.SaveCommunity(community); err != nil {
return nil, err
}

return community, nil
}

func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecdsa.PublicKey) (*Community, error) {
community, err := m.GetByID(communityID)
if err != nil {
Expand Down
34 changes: 20 additions & 14 deletions protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ func (m *Messenger) LeaveCommunity(communityID types.HexBytes) (*MessengerRespon
return nil, err
}

mr, err := m.leaveCommunity(communityID, true)
mr, err := m.leaveCommunity(communityID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1753,7 +1753,7 @@ func (m *Messenger) LeaveCommunity(communityID types.HexBytes) (*MessengerRespon
return mr, nil
}

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

community, err := m.communitiesManager.LeaveCommunity(communityID)
Expand All @@ -1776,11 +1776,21 @@ func (m *Messenger) leaveCommunity(communityID types.HexBytes, unsubsribeFromCom
}
}

if unsubsribeFromCommunity {
_, err = m.transport.RemoveFilterByChatID(communityID.String())
if err != nil {
return nil, err
}
_, err = m.transport.RemoveFilterByChatID(communityID.String())
if err != nil {
return nil, err
}

response.AddCommunity(community)
return response, nil
}

func (m *Messenger) kickedOutOfCommunity(communityID types.HexBytes) (*MessengerResponse, error) {
response := &MessengerResponse{}

community, err := m.communitiesManager.KickedOutOfCommunity(communityID)
if err != nil {
return nil, err
}

response.AddCommunity(community)
Expand Down Expand Up @@ -3230,7 +3240,7 @@ func (m *Messenger) handleSyncInstallationCommunity(messageState *ReceivedMessag
return err
}
} else {
mr, err = m.leaveCommunity(syncCommunity.Id, true)
mr, err = m.leaveCommunity(syncCommunity.Id)
if err != nil {
logger.Debug("m.leaveCommunity error", zap.Error(err))
return err
Expand Down Expand Up @@ -5877,12 +5887,8 @@ func (m *Messenger) processCommunityChanges(messageState *ReceivedMessageState)
continue
}

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