Return counter for messages with mention which are marked as seen

This commit is contained in:
Roman Volosovskyi 2021-08-31 11:49:39 +03:00
parent 719a303b05
commit 0c0e02e93a
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
6 changed files with 47 additions and 19 deletions

View File

@ -1 +1 @@
0.84.2
0.85.0

View File

@ -1254,10 +1254,10 @@ func (db sqlitePersistence) MarkAllRead(chatID string) error {
return err
}
func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint64, uint64, error) {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return 0, err
return 0, 0, err
}
defer func() {
if err == nil {
@ -1274,16 +1274,28 @@ func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint6
}
inVector := strings.Repeat("?, ", len(ids)-1) + "?"
q := "UPDATE user_messages SET seen = 1 WHERE NOT(seen) AND id IN (" + inVector + ")" // nolint: gosec
q := "UPDATE user_messages SET seen = 1 WHERE NOT(seen) AND mentioned AND id IN (" + inVector + ")" // nolint: gosec
_, err = tx.Exec(q, idsArgs...)
if err != nil {
return 0, err
return 0, 0, err
}
var count uint64
var countWithMentions uint64
row := tx.QueryRow("SELECT changes();")
if err := row.Scan(&count); err != nil {
return 0, err
if err := row.Scan(&countWithMentions); err != nil {
return 0, 0, err
}
q = "UPDATE user_messages SET seen = 1 WHERE NOT(seen) AND NOT(mentioned) AND id IN (" + inVector + ")" // nolint: gosec
_, err = tx.Exec(q, idsArgs...)
if err != nil {
return 0, 0, err
}
var countNoMentions uint64
row = tx.QueryRow("SELECT changes();")
if err := row.Scan(&countNoMentions); err != nil {
return 0, 0, err
}
// Update denormalized count
@ -1298,7 +1310,7 @@ func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint6
FROM user_messages
WHERE local_chat_id = ? AND seen = 0 AND mentioned)
WHERE id = ?`, chatID, chatID, chatID)
return count, err
return countWithMentions + countNoMentions, countWithMentions, err
}
func (db sqlitePersistence) UpdateMessageOutgoingStatus(id string, newOutgoingStatus string) error {

View File

@ -3200,17 +3200,17 @@ func (m *Messenger) DeleteMessagesByChatID(id 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) {
count, err := m.persistence.MarkMessagesSeen(chatID, ids)
func (m *Messenger) MarkMessagesSeen(chatID string, ids []string) (uint64, uint64, error) {
count, countWithMentions, err := m.persistence.MarkMessagesSeen(chatID, ids)
if err != nil {
return 0, err
return 0, 0, err
}
chat, err := m.persistence.Chat(chatID)
if err != nil {
return 0, err
return 0, 0, err
}
m.allChats.Store(chatID, chat)
return count, nil
return count, countWithMentions, nil
}
func (m *Messenger) MarkAllRead(chatID string) error {
@ -4084,6 +4084,8 @@ func (m *Messenger) pushNotificationOptions() *pushnotificationclient.Registrati
return true
})
m.logger.Info("FOOBAR", zap.Any("pubchat", publicChatIDs))
return &pushnotificationclient.RegistrationOptions{
ContactIDs: contactIDs,
MutedChatIDs: mutedChatIDs,

View File

@ -392,14 +392,16 @@ func (s *MessengerSuite) TestMarkMessagesSeen() {
err = s.m.SaveMessages([]*common.Message{inputMessage1, inputMessage2})
s.Require().NoError(err)
count, err := s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
count, countWithMentions, err := s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
s.Require().NoError(err)
s.Require().Equal(uint64(1), count)
s.Require().Equal(uint64(1), countWithMentions)
// 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})
count, countWithMentions, err = s.m.MarkMessagesSeen(chat.ID, []string{inputMessage1.ID})
s.Require().NoError(err)
s.Require().Equal(uint64(0), count)
s.Require().Equal(uint64(0), countWithMentions)
chats := s.m.Chats()
for _, c := range chats {

View File

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

View File

@ -474,6 +474,11 @@ type ApplicationMessagesResponse struct {
Cursor string `json:"cursor"`
}
type MarkMessagSeenResponse struct {
Count uint64 `json:"count"`
CountWithMentions uint64 `json:"countWithMentions"`
}
type ApplicationPinnedMessagesResponse struct {
PinnedMessages []*common.PinnedMessage `json:"pinnedMessages"`
Cursor string `json:"cursor"`
@ -556,8 +561,14 @@ func (api *PublicAPI) DeleteMessagesByChatID(id string) error {
return api.service.messenger.DeleteMessagesByChatID(id)
}
func (api *PublicAPI) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
return api.service.messenger.MarkMessagesSeen(chatID, ids)
func (api *PublicAPI) MarkMessagesSeen(chatID string, ids []string) (*MarkMessagSeenResponse, error) {
count, withMentions, err := api.service.messenger.MarkMessagesSeen(chatID, ids)
if err != nil {
return nil, err
}
response := &MarkMessagSeenResponse{Count: count, CountWithMentions: withMentions}
return response, nil
}
func (api *PublicAPI) MarkAllRead(chatID string) error {