Dont send notification to our own devices

This commit is contained in:
Andrea Maria Piana 2022-07-15 14:19:37 +01:00
parent 530f3c7a3a
commit 897bd0f58f
4 changed files with 48 additions and 10 deletions

View File

@ -1 +1 @@
0.103.0
0.103.1

View File

@ -51,6 +51,7 @@ const (
)
type MessageEvent struct {
Recipient *ecdsa.PublicKey
Type MessageEventType
SentMessage *SentMessage
RawMessage *RawMessage
@ -247,7 +248,7 @@ func (s *MessageSender) sendCommunity(
// Notify before dispatching, otherwise the dispatch subscription might happen
// earlier than the scheduled
s.notifyOnScheduledMessage(rawMessage)
s.notifyOnScheduledMessage(nil, rawMessage)
if rawMessage.CommunityKeyExMsgType != KeyExMsgNone {
keyExMessageSpecs, err := s.protocol.GetKeyExMessageSpecs(rawMessage.CommunityID, s.identity, rawMessage.Recipients, rawMessage.CommunityKeyExMsgType == KeyExMsgRekey)
@ -318,7 +319,7 @@ func (s *MessageSender) sendPrivate(
// Notify before dispatching, otherwise the dispatch subscription might happen
// earlier than the scheduled
s.notifyOnScheduledMessage(rawMessage)
s.notifyOnScheduledMessage(recipient, rawMessage)
if s.featureFlags.Datasync && rawMessage.ResendAutomatically {
// No need to call transport tracking.
@ -508,7 +509,7 @@ func (s *MessageSender) SendPublic(
rawMessage.ID = types.EncodeHex(messageID)
// notify before dispatching
s.notifyOnScheduledMessage(&rawMessage)
s.notifyOnScheduledMessage(nil, &rawMessage)
hash, err := s.transport.SendPublic(ctx, newMessage, chatName)
if err != nil {
@ -830,8 +831,9 @@ func (s *MessageSender) notifyOnSentMessage(sentMessage *SentMessage) {
}
func (s *MessageSender) notifyOnScheduledMessage(message *RawMessage) {
func (s *MessageSender) notifyOnScheduledMessage(recipient *ecdsa.PublicKey, message *RawMessage) {
event := &MessageEvent{
Recipient: recipient,
Type: MessageScheduled,
RawMessage: message,
}

View File

@ -710,12 +710,12 @@ func (c *Client) subscribeForMessageEvents() {
switch m.Type {
case common.MessageScheduled:
c.config.Logger.Debug("handling message scheduled")
if err := c.handleMessageScheduled(m.RawMessage); err != nil {
if err := c.handleMessageScheduled(m); err != nil {
c.config.Logger.Error("failed to handle message", zap.Error(err))
}
case common.MessageSent:
c.config.Logger.Debug("handling message sent")
if err := c.handleMessageSent(m.SentMessage); err != nil {
if err := c.handleMessageSent(m); err != nil {
c.config.Logger.Error("failed to handle message", zap.Error(err))
}
default:
@ -825,13 +825,19 @@ func (c *Client) queryNotificationInfo(publicKey *ecdsa.PublicKey, force bool) e
}
// handleMessageSent is called every time a message is sent
func (c *Client) handleMessageSent(sentMessage *common.SentMessage) error {
func (c *Client) handleMessageSent(e *common.MessageEvent) error {
sentMessage := e.SentMessage
// Ignore if we are not sending notifications
if !c.config.SendEnabled {
return nil
}
// check if it's for one of our devices, do nothing in that case
if e.Recipient != nil && common.IsPubKeyEqual(e.Recipient, &c.config.Identity.PublicKey) {
return nil
}
if sentMessage.PublicKey == nil {
return c.handlePublicMessageSent(sentMessage)
}
@ -1028,10 +1034,17 @@ func (c *Client) handleDirectMessageSent(sentMessage *common.SentMessage) error
}
// handleMessageScheduled keeps track of the message to make sure we notify on it
func (c *Client) handleMessageScheduled(message *common.RawMessage) error {
func (c *Client) handleMessageScheduled(e *common.MessageEvent) error {
message := e.RawMessage
if !message.SendPushNotification {
return nil
}
// check if it's for one of our devices, do nothing in that case
if e.Recipient != nil && common.IsPubKeyEqual(e.Recipient, &c.config.Identity.PublicKey) {
return nil
}
messageID, err := types.DecodeHex(message.ID)
if err != nil {
return err
@ -1041,6 +1054,11 @@ func (c *Client) handleMessageScheduled(message *common.RawMessage) error {
// shouldNotifyOn check whether we should notify a particular public-key/installation-id/message-id combination
func (c *Client) shouldNotifyOn(publicKey *ecdsa.PublicKey, installationID string, messageID []byte) (bool, error) {
if publicKey != nil && common.IsPubKeyEqual(publicKey, &c.config.Identity.PublicKey) {
return false, nil
}
if len(installationID) == 0 {
return c.persistence.ShouldSendNotificationToAllInstallationIDs(publicKey, messageID)
}
@ -1295,6 +1313,10 @@ func (c *Client) registerWithServer(registration *protobuf.PushNotificationRegis
// the notification is sent using an ephemeral key to shield the real identity of the sender
func (c *Client) SendNotification(publicKey *ecdsa.PublicKey, installationIDs []string, messageID []byte, chatID string, notificationType protobuf.PushNotification_PushNotificationType) ([]*PushNotificationInfo, error) {
if common.IsPubKeyEqual(publicKey, &c.config.Identity.PublicKey) {
return nil, nil
}
// get latest push notification infos
err := c.queryNotificationInfo(publicKey, false)
if err != nil {

View File

@ -190,7 +190,11 @@ func (s *ClientSuite) TestHandleMessageScheduled() {
LocalChatID: chatID,
}
s.Require().NoError(s.client.handleMessageScheduled(rawMessage))
event := &common.MessageEvent{
RawMessage: rawMessage,
}
s.Require().NoError(s.client.handleMessageScheduled(event))
key1, err := crypto.GenerateKey()
s.Require().NoError(err)
@ -251,3 +255,13 @@ func (s *ClientSuite) TestShouldRefreshToken() {
// allow from contacts only is enabled
s.Require().True(s.client.shouldRefreshToken([]*ecdsa.PublicKey{&key1.PublicKey, &key2.PublicKey}, []*ecdsa.PublicKey{&key2.PublicKey, &key1.PublicKey}, false, true))
}
func (s *ClientSuite) TestHandleMessageScheduledFromPairedDevice() {
messageID := []byte("message-id")
installationID1 := "1"
// Should return nil
response, err := s.client.shouldNotifyOn(&s.identity.PublicKey, installationID1, messageID)
s.Require().NoError(err)
s.Require().False(response)
}