Add BeforeDispatch callback

This commit is contained in:
Andrea Maria Piana 2023-06-20 17:12:59 +01:00
parent 14e3eafaeb
commit efad2ef6a2
4 changed files with 53 additions and 19 deletions

View File

@ -261,6 +261,11 @@ func (s *MessageSender) sendCommunity(
rawMessage.ID = types.EncodeHex(messageID) rawMessage.ID = types.EncodeHex(messageID)
messageIDs := [][]byte{messageID} messageIDs := [][]byte{messageID}
if rawMessage.BeforeDispatch != nil {
if err := rawMessage.BeforeDispatch(rawMessage); err != nil {
return nil, err
}
}
// 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(nil, rawMessage) s.notifyOnScheduledMessage(nil, rawMessage)
@ -351,6 +356,12 @@ func (s *MessageSender) sendPrivate(
messageID := v1protocol.MessageID(&rawMessage.Sender.PublicKey, wrappedMessage) messageID := v1protocol.MessageID(&rawMessage.Sender.PublicKey, wrappedMessage)
rawMessage.ID = types.EncodeHex(messageID) rawMessage.ID = types.EncodeHex(messageID)
if rawMessage.BeforeDispatch != nil {
if err := rawMessage.BeforeDispatch(rawMessage); err != nil {
return nil, err
}
}
// 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(recipient, rawMessage) s.notifyOnScheduledMessage(recipient, rawMessage)
@ -510,6 +521,12 @@ func (s *MessageSender) dispatchCommunityChatMessage(ctx context.Context, rawMes
PowTime: whisperPoWTime, PowTime: whisperPoWTime,
} }
if rawMessage.BeforeDispatch != nil {
if err := rawMessage.BeforeDispatch(rawMessage); err != nil {
return nil, nil, err
}
}
// notify before dispatching // notify before dispatching
s.notifyOnScheduledMessage(nil, rawMessage) s.notifyOnScheduledMessage(nil, rawMessage)
@ -564,6 +581,12 @@ func (s *MessageSender) SendPublic(
messageID := v1protocol.MessageID(&rawMessage.Sender.PublicKey, wrappedMessage) messageID := v1protocol.MessageID(&rawMessage.Sender.PublicKey, wrappedMessage)
rawMessage.ID = types.EncodeHex(messageID) rawMessage.ID = types.EncodeHex(messageID)
if rawMessage.BeforeDispatch != nil {
if err := rawMessage.BeforeDispatch(&rawMessage); err != nil {
return nil, err
}
}
// notify before dispatching // notify before dispatching
s.notifyOnScheduledMessage(nil, &rawMessage) s.notifyOnScheduledMessage(nil, &rawMessage)

View File

@ -34,4 +34,5 @@ type RawMessage struct {
CommunityID []byte CommunityID []byte
CommunityKeyExMsgType CommKeyExMsgType CommunityKeyExMsgType CommKeyExMsgType
Ephemeral bool Ephemeral bool
BeforeDispatch func(*RawMessage) error
} }

View File

@ -2189,30 +2189,34 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message
ResendAutomatically: true, ResendAutomatically: true,
} }
// We want to save the raw message before dispatching it, to avoid race conditions
// since it might get dispatched and confirmed before it's saved.
// This is not the best solution, probably it would be better to split
// the sent status in a different table and join on query for messages,
// but that's a much larger change and it would require an expensive migration of clients
rawMessage.BeforeDispatch = func(rawMessage *common.RawMessage) error {
if rawMessage.Sent {
message.OutgoingStatus = common.OutgoingStatusSent
}
message.ID = rawMessage.ID
err = message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
if err != nil {
return err
}
err = chat.UpdateFromMessage(message, m.getTimesource())
if err != nil {
return err
}
return m.persistence.SaveMessages([]*common.Message{message})
}
rawMessage, err = m.dispatchMessage(ctx, rawMessage) rawMessage, err = m.dispatchMessage(ctx, rawMessage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if rawMessage.Sent {
message.OutgoingStatus = common.OutgoingStatusSent
}
message.ID = rawMessage.ID
err = message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
if err != nil {
return nil, err
}
err = chat.UpdateFromMessage(message, m.getTimesource())
if err != nil {
return nil, err
}
err = m.persistence.SaveMessages([]*common.Message{message})
if err != nil {
return nil, err
}
msg, err := m.pullMessagesAndResponsesFromDB([]*common.Message{message}) msg, err := m.pullMessagesAndResponsesFromDB([]*common.Message{message})
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -2354,6 +2354,12 @@ func (s *MessengerSuite) TestMessageSent() {
s.True(rawMessage.Sent) s.True(rawMessage.Sent)
} }
func (s *MessengerSuite) TestProcessSentMessages() {
ids := []string{"a"}
err := s.m.processSentMessages(ids)
s.Require().NoError(err)
}
func (s *MessengerSuite) TestResendExpiredEmojis() { func (s *MessengerSuite) TestResendExpiredEmojis() {
//send message //send message
chat := CreatePublicChat("test-chat", s.m.transport) chat := CreatePublicChat("test-chat", s.m.transport)