Return count of updated messages in MarkMessagesSeen

The frontend needs to know if the messaged marked as seen was actually
seen or not, as we might not have it loaded in the frontend.
This commit is contained in:
Andrea Maria Piana 2020-04-06 14:08:53 +02:00
parent 4f9f7cd258
commit 3c5354280c
8 changed files with 32 additions and 17 deletions

View File

@ -1 +1 @@
0.50.2
0.51.0

View File

@ -442,6 +442,7 @@ func (m *MessageHandler) HandleAcceptRequestAddressForTransaction(messageState *
initialMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
initialMessage.Text = requestAddressForTransactionAcceptedMessage
initialMessage.CommandParameters.Address = command.Address
initialMessage.Seen = false
initialMessage.CommandParameters.CommandState = CommandStateRequestAddressForTransactionAccepted
// Hide previous message
@ -510,6 +511,7 @@ func (m *MessageHandler) HandleDeclineRequestAddressForTransaction(messageState
oldMessage.Clock = command.Clock
oldMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
oldMessage.Text = requestAddressForTransactionDeclinedMessage
oldMessage.Seen = false
oldMessage.CommandParameters.CommandState = CommandStateRequestAddressForTransactionDeclined
// Hide previous message
@ -550,6 +552,7 @@ func (m *MessageHandler) HandleDeclineRequestTransaction(messageState *ReceivedM
oldMessage.Clock = command.Clock
oldMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
oldMessage.Text = transactionRequestDeclinedMessage
oldMessage.Seen = false
oldMessage.CommandParameters.CommandState = CommandStateRequestTransactionDeclined
// Hide previous message

View File

@ -1999,20 +1999,23 @@ func (m *Messenger) DeleteMessagesByChatID(id string) error {
return m.persistence.DeleteMessagesByChatID(id)
}
func (m *Messenger) MarkMessagesSeen(chatID string, ids []string) error {
// MarkMessagesSeen marks messages with `ids` as seen in the chat `chatID`.
// It returns the number of affected messages or error. If there is an error,
// the number of affected messages is always zero.
func (m *Messenger) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
err := m.persistence.MarkMessagesSeen(chatID, ids)
count, err := m.persistence.MarkMessagesSeen(chatID, ids)
if err != nil {
return err
return 0, err
}
chat, err := m.persistence.Chat(chatID)
if err != nil {
return err
return 0, err
}
m.allChats[chatID] = chat
return nil
return count, nil
}
func (m *Messenger) MarkAllRead(chatID string) error {
@ -2750,6 +2753,7 @@ func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types.
message.Timestamp = timestamp
message.WhisperTimestamp = timestamp
message.Text = "Transaction received"
message.Seen = false
message.ID = validationResult.Transaction.MessageID
if message.CommandParameters == nil {

View File

@ -319,8 +319,9 @@ func (s *MessengerSuite) TestMarkMessagesSeen() {
err = s.m.SaveMessages([]*Message{inputMessage1, inputMessage2})
s.Require().NoError(err)
err = s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
count, err := s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
s.Require().NoError(err)
s.Require().Equal(uint64(1), count)
chats := s.m.Chats()
s.Require().Len(chats, 1)

View File

@ -456,10 +456,10 @@ func (db sqlitePersistence) MarkAllRead(chatID string) error {
return err
}
func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) error {
func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
return 0, err
}
defer func() {
if err == nil {
@ -479,7 +479,13 @@ func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) error
q := "UPDATE user_messages SET seen = 1 WHERE id IN (" + inVector + ")" // nolint: gosec
_, err = tx.Exec(q, idsArgs...)
if err != nil {
return err
return 0, err
}
var count uint64
row := tx.QueryRow("SELECT changes();")
if err := row.Scan(&count); err != nil {
return 0, err
}
// Update denormalized count
@ -490,7 +496,7 @@ func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) error
FROM user_messages
WHERE local_chat_id = ? AND seen = 0)
WHERE id = ?`, chatID, chatID)
return err
return count, err
}
func (db sqlitePersistence) UpdateMessageOutgoingStatus(id string, newOutgoingStatus string) error {

View File

@ -331,8 +331,9 @@ func TestMarkMessageSeen(t *testing.T) {
require.NoError(t, err)
require.False(t, m.Seen)
err = p.MarkMessagesSeen(chatID, []string{m.ID})
count, err := p.MarkMessagesSeen(chatID, []string{m.ID})
require.NoError(t, err)
require.Equal(t, uint64(1), count)
m, err = p.MessageByID(id)
require.NoError(t, err)

View File

@ -102,10 +102,10 @@ type MembershipUpdateEvent struct {
ClockValue uint64 `json:"clockValue"`
Members []string `json:"members,omitempty"` // in "members-added" and "admins-added" events
Name string `json:"name,omitempty"` // name of the group chat
From string
Signature []byte
ChatID string
RawPayload []byte
From string `json:"from,omitempty"`
Signature []byte `json:"signature,omitempty"`
ChatID string `json:"chatId"`
RawPayload []byte `json:"rawPayload"`
}
func (u *MembershipUpdateEvent) Equal(update MembershipUpdateEvent) bool {

View File

@ -309,7 +309,7 @@ func (api *PublicAPI) DeleteMessagesByChatID(id string) error {
return api.service.messenger.DeleteMessagesByChatID(id)
}
func (api *PublicAPI) MarkMessagesSeen(chatID string, ids []string) error {
func (api *PublicAPI) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
return api.service.messenger.MarkMessagesSeen(chatID, ids)
}