Skip to content

Commit

Permalink
fix(messenger)_: make sure chats have an unread count of 0 for channe…
Browse files Browse the repository at this point in the history
…ls you can't view (#5062)

Fixes status-im/status-desktop#14421

The problem is that you can receive messages to  a channel, then later, before marking them as read, a permission is added to them, so you no longer have access.
Then, you can't even mark it as read if it's hidden.
Here, I fix it by setting the unread count on Init at 0 if the user doesn't have view access to it. And I make sure we update the counts when we are removed from a channel
  • Loading branch information
jrainville authored May 15, 2024
1 parent bf56cb7 commit 5ca1cb0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 1 addition & 2 deletions protocol/communities/community_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func evaluateCommunityChangesByDescription(origin, modified *protobuf.CommunityD
if _, ok := origin.Chats[chatID]; !ok {
changes.ChatsAdded[chatID] = chat
} else {

// Check for members added
for pk, member := range modified.Chats[chatID].Members {
if _, ok := origin.Chats[chatID].Members[pk]; !ok {
Expand All @@ -192,7 +193,6 @@ func evaluateCommunityChangesByDescription(origin, modified *protobuf.CommunityD
MembersRemoved: make(map[string]*protobuf.CommunityMember),
}
}

changes.ChatsModified[chatID].MembersAdded[pk] = member
}
}
Expand All @@ -206,7 +206,6 @@ func evaluateCommunityChangesByDescription(origin, modified *protobuf.CommunityD
MembersRemoved: make(map[string]*protobuf.CommunityMember),
}
}

changes.ChatsModified[chatID].MembersRemoved[pk] = member
}
}
Expand Down
16 changes: 14 additions & 2 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,9 +1758,8 @@ func (m *Messenger) Init() error {
continue
}

m.allChats.Store(chat.ID, chat)

if !chat.Active || chat.Timeline() {
m.allChats.Store(chat.ID, chat)
continue
}

Expand All @@ -1784,6 +1783,17 @@ func (m *Messenger) Init() error {
communityInfo[chat.CommunityID] = community
}

if chat.UnviewedMessagesCount > 0 || chat.UnviewedMentionsCount > 0 {
// Make sure the unread count is 0 for the channels the user cannot view
// It's possible that the users received messages to a channel before permissions were added
canView := community.CanView(&m.identity.PublicKey, chat.CommunityChatID())

if !canView {
chat.UnviewedMessagesCount = 0
chat.UnviewedMentionsCount = 0
}
}

filtersToInit = append(filtersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic()})
case ChatTypeOneToOne:
pk, err := chat.PublicKey()
Expand All @@ -1802,6 +1812,8 @@ func (m *Messenger) Init() error {
default:
return errors.New("invalid chat type")
}

m.allChats.Store(chat.ID, chat)
}

// Timeline and profile chats are deprecated.
Expand Down
20 changes: 19 additions & 1 deletion protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi

removedChatIDs := make([]string, 0)
for id := range communityResponse.Changes.ChatsRemoved {
chatID := community.IDString() + id
chatID := community.ChatID(id)
_, ok := state.AllChats.Load(chatID)
if ok {
removedChatIDs = append(removedChatIDs, chatID)
Expand All @@ -3185,6 +3185,24 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi
}
}

// Check if we have been removed from a chat (ie no longer have access)
for channelID, changes := range communityResponse.Changes.ChatsModified {
if _, ok := changes.MembersRemoved[common.PubkeyToHex(&m.identity.PublicKey)]; ok {
chatID := community.ChatID(channelID)

if chat, ok := state.AllChats.Load(chatID); ok {
// Reset the chat's message counts
chat.UnviewedMessagesCount = 0
chat.UnviewedMentionsCount = 0
err := m.saveChat(chat)
if err != nil {
return err
}
state.Response.AddChat(chat)
}
}
}

// Update relevant chats names and add new ones
// Currently removal is not supported
chats := CreateCommunityChats(community, state.Timesource)
Expand Down

0 comments on commit 5ca1cb0

Please sign in to comment.