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

View File

@ -710,12 +710,12 @@ func (c *Client) subscribeForMessageEvents() {
switch m.Type { switch m.Type {
case common.MessageScheduled: case common.MessageScheduled:
c.config.Logger.Debug("handling message scheduled") 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)) c.config.Logger.Error("failed to handle message", zap.Error(err))
} }
case common.MessageSent: case common.MessageSent:
c.config.Logger.Debug("handling message sent") 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)) c.config.Logger.Error("failed to handle message", zap.Error(err))
} }
default: default:
@ -825,13 +825,19 @@ func (c *Client) queryNotificationInfo(publicKey *ecdsa.PublicKey, force bool) e
} }
// handleMessageSent is called every time a message is sent // 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 // Ignore if we are not sending notifications
if !c.config.SendEnabled { if !c.config.SendEnabled {
return nil 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 { if sentMessage.PublicKey == nil {
return c.handlePublicMessageSent(sentMessage) 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 // 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 { if !message.SendPushNotification {
return nil 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) messageID, err := types.DecodeHex(message.ID)
if err != nil { if err != nil {
return err 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 // 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) { 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 { if len(installationID) == 0 {
return c.persistence.ShouldSendNotificationToAllInstallationIDs(publicKey, messageID) 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 // 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) { 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 // get latest push notification infos
err := c.queryNotificationInfo(publicKey, false) err := c.queryNotificationInfo(publicKey, false)
if err != nil { if err != nil {

View File

@ -190,7 +190,11 @@ func (s *ClientSuite) TestHandleMessageScheduled() {
LocalChatID: chatID, 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() key1, err := crypto.GenerateKey()
s.Require().NoError(err) s.Require().NoError(err)
@ -251,3 +255,13 @@ func (s *ClientSuite) TestShouldRefreshToken() {
// allow from contacts only is enabled // 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)) 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)
}