From 99a304686faadd465a90704b1e228f62ef873df2 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 22 Feb 2021 18:12:59 +0200 Subject: [PATCH] Filter message PNs on status go side --- VERSION | 2 +- protocol/message_handler.go | 10 ------- protocol/messenger.go | 53 ++++++++++++++++++++++++++++++------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/VERSION b/VERSION index cbb0974ed..33707196d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.71.6 +0.71.7 diff --git a/protocol/message_handler.go b/protocol/message_handler.go index 411679875..443174a4e 100644 --- a/protocol/message_handler.go +++ b/protocol/message_handler.go @@ -490,16 +490,6 @@ func (m *MessageHandler) HandleChatMessage(state *ReceivedMessageState) error { // Add to response state.Response.Messages = append(state.Response.Messages, receivedMessage) - // Create notification body to be eventually passed to `localnotifications.SendMessageNotifications()` - state.Response.Notifications = append( - state.Response.Notifications, - MessageNotificationBody{ - Message: receivedMessage, - Contact: contact, - Chat: chat, - }, - ) - return nil } diff --git a/protocol/messenger.go b/protocol/messenger.go index a01770327..141addd42 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -2796,7 +2796,7 @@ func (m *Messenger) markDeliveredMessages(acks [][]byte) { // addNewMessageNotification takes a common.Message and generates a new MessageNotificationBody and appends it to the // []Response.Notifications if the message is m.New -func (r *ReceivedMessageState) addNewMessageNotification(m *common.Message) error { +func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicKey, m *common.Message, responseTo *common.Message) error { if !m.New { return nil } @@ -2807,14 +2807,19 @@ func (r *ReceivedMessageState) addNewMessageNotification(m *common.Message) erro } contactID := contactIDFromPublicKey(pubKey) - r.Response.Notifications = append( - r.Response.Notifications, - MessageNotificationBody{ - Message: m, - Contact: r.AllContacts[contactID], - Chat: r.AllChats[m.ChatId], - }, - ) + chat := r.AllChats[m.LocalChatID] + notification := MessageNotificationBody{ + Message: m, + Contact: r.AllContacts[contactID], + Chat: chat, + } + + if showNotification(publicKey, notification, responseTo) { + r.Response.Notifications = append( + r.Response.Notifications, + notification, + ) + } return nil } @@ -3299,6 +3304,10 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte if err != nil { return nil, err } + messagesByID := map[string]*common.Message{} + for _, message := range messagesWithResponses { + messagesByID[message.ID] = message + } messageState.Response.Messages = messagesWithResponses for _, message := range messageState.Response.Messages { @@ -3306,7 +3315,7 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte message.New = true // Create notification body to be eventually passed to `localnotifications.SendMessageNotifications()` - if err = messageState.addNewMessageNotification(message); err != nil { + if err = messageState.addNewMessageNotification(m.identity.PublicKey, message, messagesByID[message.ResponseTo]); err != nil { return nil, err } } @@ -3318,6 +3327,30 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte return messageState.Response, nil } +func showNotification(publicKey ecdsa.PublicKey, n MessageNotificationBody, responseTo *common.Message) bool { + if n.Chat != nil && n.Chat.ChatType == ChatTypeOneToOne { + return true + } + + publicKeyString := common.PubkeyToHex(&publicKey) + mentioned := false + for _, mention := range n.Message.Mentions { + if publicKeyString == mention { + mentioned = true + } + } + + if mentioned { + return true + } + + if responseTo != nil { + return responseTo.From == publicKeyString + } + + return false +} + // SetMailserver sets the currently used mailserver func (m *Messenger) SetMailserver(peer []byte) { m.mailserver = peer