From 717e0f9ba57bdf588f586ae6a811940f021acab1 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 22 Sep 2023 12:31:35 +0200 Subject: [PATCH] Optimized counter increment logic for 1-1 chat messages (#3889) * Optimized counter increment logic for 1-1 chat messages * Updated * Updated condition --- protocol/common/message.go | 11 +++++++++++ protocol/messenger_handler.go | 25 ++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/protocol/common/message.go b/protocol/common/message.go index 69e1ef35c..429bfcb3a 100644 --- a/protocol/common/message.go +++ b/protocol/common/message.go @@ -632,6 +632,17 @@ func (m *Message) PrepareContent(identity string) error { return m.parseAudio() } +func (m *Message) IsSystemMessage() bool { + return m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_CONTENT_PRIVATE_GROUP || + m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_GAP || + m.ContentType == protobuf.ChatMessage_CONTACT_REQUEST || + m.ContentType == protobuf.ChatMessage_IDENTITY_VERIFICATION || + m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_PINNED_MESSAGE || + m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_SENT || + m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_ACCEPTED || + m.ContentType == protobuf.ChatMessage_SYSTEM_MESSAGE_MUTUAL_EVENT_REMOVED +} + // GetSimplifiedText returns a the text stripped of all the markdown and with mentions // replaced by canonical names func (m *Message) GetSimplifiedText(identity string, canonicalNames map[string]string) (string, error) { diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 9b1267ba6..4127ededd 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -40,6 +40,13 @@ const ( requestAddressForTransactionDeclinedMessage = "Request address for transaction declined" ) +const ( + // IncreaseUnviewedMessagesCountTimeout + // this timeout indicates how long the time between received messages should be + // for a new message to increase the unviewed messages counter + IncreaseUnviewedMessagesCountTimeout = 1000 * 60 * 2 +) + var ( ErrMessageNotAllowed = errors.New("message from a non-contact") ErrMessageForWrongChatType = errors.New("message for the wrong chat type") @@ -413,7 +420,7 @@ func (m *Messenger) handleCommandMessage(state *ReceivedMessageState, message *c // Increase unviewed count if !common.IsPubKeyEqual(message.SigPubKey, &m.identity.PublicKey) { - m.updateUnviewedCounts(chat, message.Mentioned || message.Replied) + m.updateUnviewedCounts(chat, message) message.OutgoingStatus = "" } else { // Our own message, mark as sent @@ -2120,7 +2127,7 @@ func (m *Messenger) handleChatMessage(state *ReceivedMessageState, forceSeen boo } } if !skipUpdateUnviewedCountForAlbums { - m.updateUnviewedCounts(chat, receivedMessage.Mentioned || receivedMessage.Replied) + m.updateUnviewedCounts(chat, receivedMessage) } } @@ -3075,9 +3082,17 @@ func (m *Messenger) isMessageAllowedFrom(publicKey string, chat *Chat) (bool, er return contact.added(), nil } -func (m *Messenger) updateUnviewedCounts(chat *Chat, mentionedOrReplied bool) { - chat.UnviewedMessagesCount++ - if mentionedOrReplied { +func (m *Messenger) updateUnviewedCounts(chat *Chat, message *common.Message) { + if chat == nil { + return + } + if chat.LastMessage == nil || + chat.LastMessage.IsSystemMessage() || + !chat.LastMessage.New || + message.Timestamp > chat.LastMessage.Timestamp+IncreaseUnviewedMessagesCountTimeout { + chat.UnviewedMessagesCount++ + } + if message.Mentioned || message.Replied { chat.UnviewedMentionsCount++ } }