diff --git a/protocol/communities/community_changes.go b/protocol/communities/community_changes.go index 6409d001f5..0cd40dde29 100644 --- a/protocol/communities/community_changes.go +++ b/protocol/communities/community_changes.go @@ -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 { diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index eff5597c74..697967e67e 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -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() } } @@ -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 { diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 296062bab1..77275e34c1 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -1702,7 +1702,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 } @@ -1756,7 +1756,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) @@ -1779,11 +1779,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) @@ -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 @@ -5865,12 +5875,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