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

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 committed Apr 19, 2024
1 parent e691a1b commit f142ea0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
21 changes: 7 additions & 14 deletions protocol/communities/community_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,23 @@ func evaluateCommunityChangesByDescription(origin, modified *protobuf.CommunityD
if _, ok := origin.Chats[chatID]; !ok {
changes.ChatsAdded[chatID] = chat
} else {
if changes.ChatsModified[chatID] == nil {
changes.ChatsModified[chatID] = &CommunityChatChanges{
MembersAdded: make(map[string]*protobuf.CommunityMember),
MembersRemoved: make(map[string]*protobuf.CommunityMember),
}
}

// Check for members added
for pk, member := range modified.Chats[chatID].Members {
if _, ok := origin.Chats[chatID].Members[pk]; !ok {
if changes.ChatsModified[chatID] == nil {
changes.ChatsModified[chatID] = &CommunityChatChanges{
MembersAdded: make(map[string]*protobuf.CommunityMember),
MembersRemoved: make(map[string]*protobuf.CommunityMember),
}
}

changes.ChatsModified[chatID].MembersAdded[pk] = member
}
}

// check for members removed
for pk, member := range origin.Chats[chatID].Members {
if _, ok := modified.Chats[chatID].Members[pk]; !ok {
if changes.ChatsModified[chatID] == nil {
changes.ChatsModified[chatID] = &CommunityChatChanges{
MembersAdded: make(map[string]*protobuf.CommunityMember),
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 @@ -1822,9 +1822,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 @@ -1848,6 +1847,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 @@ -1866,6 +1876,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
15 changes: 15 additions & 0 deletions protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3089,6 +3089,21 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi
}
}

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

if chat, ok := state.AllChats.Load(chatID); ok {
// Reset the chat's message counts
chat.UnviewedMessagesCount = 0
chat.UnviewedMentionsCount = 0
m.saveChat(chat)
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 f142ea0

Please sign in to comment.