Mark message as seen when hiding

When hiding a message, we want to mark it as seen in order to preserve
the right count
This commit is contained in:
Andrea Maria Piana 2020-12-07 16:13:39 +01:00
parent d4518d7f16
commit f3f6cdcd93
4 changed files with 45 additions and 2 deletions

View File

@ -800,7 +800,7 @@ func (db sqlitePersistence) DeleteMessage(id string) error {
}
func (db sqlitePersistence) HideMessage(id string) error {
_, err := db.db.Exec(`UPDATE user_messages SET hide = 1 WHERE id = ?`, id)
_, err := db.db.Exec(`UPDATE user_messages SET hide = 1, seen = 1 WHERE id = ?`, id)
return err
}
@ -851,7 +851,7 @@ func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint6
}
inVector := strings.Repeat("?, ", len(ids)-1) + "?"
q := "UPDATE user_messages SET seen = 1 WHERE id IN (" + inVector + ")" // nolint: gosec
q := "UPDATE user_messages SET seen = 1 WHERE NOT(seen) AND id IN (" + inVector + ")" // nolint: gosec
_, err = tx.Exec(q, idsArgs...)
if err != nil {
return 0, err

View File

@ -2957,6 +2957,7 @@ func (m *Messenger) RequestTransaction(ctx context.Context, chatID, value, contr
message.MessageType = protobuf.MessageType_ONE_TO_ONE
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
message.Seen = true
message.Text = "Request transaction"
request := &protobuf.RequestTransaction{
@ -3034,6 +3035,7 @@ func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, fr
message.MessageType = protobuf.MessageType_ONE_TO_ONE
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
message.Seen = true
message.Text = "Request address for transaction"
request := &protobuf.RequestAddressForTransaction{
@ -3118,6 +3120,7 @@ func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, mess
message.WhisperTimestamp = timestamp
message.Timestamp = timestamp
message.Text = "Request address for transaction accepted"
message.Seen = true
message.OutgoingStatus = common.OutgoingStatusSending
// Hide previous message
@ -3213,6 +3216,7 @@ func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID str
message.WhisperTimestamp = timestamp
message.Timestamp = timestamp
message.Text = "Transaction request declined"
message.Seen = true
message.OutgoingStatus = common.OutgoingStatusSending
message.Replace = messageID
@ -3295,6 +3299,7 @@ func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, mes
message.WhisperTimestamp = timestamp
message.Timestamp = timestamp
message.Text = "Request address for transaction declined"
message.Seen = true
message.OutgoingStatus = common.OutgoingStatusSending
message.Replace = messageID
@ -3376,6 +3381,7 @@ func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHas
message.Clock = clock
message.WhisperTimestamp = timestamp
message.Timestamp = timestamp
message.Seen = true
message.Text = transactionSentTxt
message.OutgoingStatus = common.OutgoingStatusSending
@ -3473,6 +3479,7 @@ func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
message.Clock = clock
message.WhisperTimestamp = timestamp
message.Seen = true
message.Timestamp = timestamp
message.Text = transactionSentTxt

View File

@ -319,6 +319,11 @@ func (s *MessengerSuite) TestMarkMessagesSeen() {
s.Require().NoError(err)
s.Require().Equal(uint64(1), count)
// Make sure that if it's not seen, it does not return a count of 1
count, err = s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
s.Require().NoError(err)
s.Require().Equal(uint64(0), count)
chats := s.m.Chats()
s.Require().Len(chats, 1)
s.Require().Equal(uint(1), chats[0].UnviewedMessagesCount)

View File

@ -668,3 +668,34 @@ func TestSaveLinks(t *testing.T) {
require.Equal(t, retrievedMessages[0].Links, message.Links)
}
func TestHideMessage(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := sqlitePersistence{db: db}
chatID := testPublicChatID
message := &common.Message{
ID: "id-1",
LocalChatID: chatID,
ChatMessage: protobuf.ChatMessage{
Text: "content-1",
Clock: uint64(1),
},
From: "1",
}
messages := []*common.Message{message}
err = p.SaveMessages(messages)
require.NoError(t, err)
err = p.HideMessage(message.ID)
require.NoError(t, err)
var actualHidden, actualSeen bool
err = p.db.QueryRow("SELECT hide, seen FROM user_messages WHERE id = ?", message.ID).Scan(&actualHidden, &actualSeen)
require.NoError(t, err)
require.True(t, actualHidden)
require.True(t, actualSeen)
}