feat: up mention count for 1-1 messages each 2 min (#4035)

This commit is contained in:
Jonathan Rainville 2023-09-25 11:29:36 -04:00 committed by GitHub
parent ba5cd9c1a4
commit 2afe5a269d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 33 deletions

View File

@ -27,6 +27,13 @@ var chatColors = []string{
"#d37ef4", // purple
}
const (
// IncreaseUnviewedMessagesCountTimeoutMs
// this timeout indicates how long the time between received messages should be
// for a new message to increase the unviewed messages counter
IncreaseUnviewedMessagesCountTimeoutMs = 1000 * 60 * 2
)
type ChatType int
const (
@ -391,6 +398,14 @@ func (c *Chat) UpdateFromMessage(message *common.Message, timesource common.Time
return nil
}
func (c *Chat) ShouldIncreaseMessageCount(message *common.Message) bool {
return c.LastMessage == nil ||
c.LastMessage.IsSystemMessage() ||
!c.LastMessage.New ||
c.LastMessage.Seen ||
message.Timestamp > c.LastMessage.Timestamp+IncreaseUnviewedMessagesCountTimeoutMs
}
func (c *Chat) UpdateFirstMessageTimestamp(timestamp uint32) bool {
if timestamp == c.FirstMessageTimestamp {
return false

View File

@ -635,8 +635,6 @@ func (m *Message) PrepareContent(identity string) error {
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 ||

View File

@ -4108,6 +4108,10 @@ func (m *Messenger) markAllRead(chatID string, clock uint64, shouldBeSynced bool
chat.UnviewedMessagesCount = 0
chat.UnviewedMentionsCount = 0
if chat.LastMessage != nil {
chat.LastMessage.Seen = true
}
// TODO(samyoul) remove storing of an updated reference pointer?
m.allChats.Store(chat.ID, chat)
return m.persistence.SaveChats([]*Chat{chat})

View File

@ -367,10 +367,10 @@ func (s *MessengerEditMessageSuite) TestEditMessageWithMention() {
s.Require().NoError(err)
s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Messages(), 1)
// Make sure there is no mention at first
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1)
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 0)
// Make sure the message is not marked as Mentioned (chat still counts it because it's 1-1)
s.Require().False(response.Messages()[0].Mentioned)
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1)
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 1)
ogMessage := sendResponse.Messages()[0]
@ -407,9 +407,9 @@ func (s *MessengerEditMessageSuite) TestEditMessageWithMention() {
s.Require().NotEmpty(response.Messages()[0].EditedAt)
s.Require().False(response.Messages()[0].New)
// Receiver (us) is now mentioned
s.Require().True(response.Messages()[0].Mentioned)
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1)
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 1)
s.Require().True(response.Messages()[0].Mentioned)
// Edit the message again but remove the mention
editedText = "edited text no mention"
@ -441,9 +441,9 @@ func (s *MessengerEditMessageSuite) TestEditMessageWithMention() {
s.Require().NotEmpty(response.Messages()[0].EditedAt)
s.Require().False(response.Messages()[0].New)
// Receiver (us) is no longer mentioned
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1) // We still have an unread message though
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 0)
s.Require().False(response.Messages()[0].Mentioned)
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1) // We still have an unread message though
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 1)
}
func (s *MessengerEditMessageSuite) TestEditMessageWithLinkPreviews() {

View File

@ -40,13 +40,6 @@ 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")
@ -1728,15 +1721,19 @@ func (m *Messenger) handleEditMessage(state *ReceivedMessageState, editMessage E
editedMessageHasMentions := editedMessage.Mentioned
if editedMessageHasMentions && !originalMessageMentioned && !editedMessage.Seen {
// Increase unviewed count when the edited message has a mention and didn't have one before
chat.UnviewedMentionsCount++
needToSaveChat = true
} else if !editedMessageHasMentions && originalMessageMentioned && !editedMessage.Seen {
// Opposite of above, the message had a mention, but no longer does, so we reduce the count
chat.UnviewedMentionsCount--
needToSaveChat = true
// Messages in OneToOne chats increase the UnviewedMentionsCount whether or not they include a Mention
if !chat.OneToOne() {
if editedMessageHasMentions && !originalMessageMentioned && !editedMessage.Seen {
// Increase unviewed count when the edited message has a mention and didn't have one before
chat.UnviewedMentionsCount++
needToSaveChat = true
} else if !editedMessageHasMentions && originalMessageMentioned && !editedMessage.Seen {
// Opposite of above, the message had a mention, but no longer does, so we reduce the count
chat.UnviewedMentionsCount--
needToSaveChat = true
}
}
if needToSaveChat {
err := m.saveChat(chat)
if err != nil {
@ -3083,17 +3080,11 @@ func (m *Messenger) isMessageAllowedFrom(publicKey string, chat *Chat) (bool, er
}
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 {
if chat.ShouldIncreaseMessageCount(message) {
chat.UnviewedMessagesCount++
}
if message.Mentioned || message.Replied {
chat.UnviewedMentionsCount++
if message.Mentioned || message.Replied || chat.OneToOne() {
chat.UnviewedMentionsCount++
}
}
}