feat: add `FirstUnseenMessageID` API

This commit is contained in:
Patryk Osmaczko 2022-12-11 20:08:51 +01:00 committed by osmaczko
parent cd9fb48579
commit 3025cdcba4
4 changed files with 78 additions and 0 deletions

View File

@ -620,6 +620,31 @@ func (db sqlitePersistence) MessageByChatID(chatID string, currCursor string, li
return result, newCursor, nil
}
func (db sqlitePersistence) FirstUnseenMessageID(chatID string) (string, error) {
var id string
err := db.db.QueryRow(
fmt.Sprintf(
`
SELECT
id
FROM
user_messages m1
WHERE
m1.local_chat_id = ? AND NOT(m1.seen) AND NOT(m1.hide) AND NOT(m1.deleted) AND NOT(m1.deleted_for_me)
ORDER BY %s ASC
LIMIT 1
`, cursor),
chatID).Scan(&id)
if err == sql.ErrNoRows {
return "", nil
}
if err != nil {
return "", err
}
return id, nil
}
// Get last chat message that is not hide or deleted or deleted_for_me
func (db sqlitePersistence) LatestMessageByChatID(chatID string) ([]*common.Message, error) {
args := []interface{}{chatID}

View File

@ -4378,6 +4378,10 @@ func (m *Messenger) MessagesExist(ids []string) (map[string]bool, error) {
return m.persistence.MessagesExist(ids)
}
func (m *Messenger) FirstUnseenMessageID(chatID string) (string, error) {
return m.persistence.FirstUnseenMessageID(chatID)
}
func (m *Messenger) latestIncomingMessageClock(chatID string) (uint64, error) {
return m.persistence.latestIncomingMessageClock(chatID)
}

View File

@ -247,6 +247,51 @@ func TestMessageByChatID(t *testing.T) {
)
}
func TestFirstUnseenMessageIDByChatID(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := newSQLitePersistence(db)
messageID, err := p.FirstUnseenMessageID(testPublicChatID)
require.NoError(t, err)
require.Equal(t, "", messageID)
err = p.SaveMessages([]*common.Message{
{
ID: "1",
LocalChatID: testPublicChatID,
ChatMessage: protobuf.ChatMessage{
Clock: 1,
Text: "some-text"},
From: "me",
Seen: true,
},
{
ID: "2",
LocalChatID: testPublicChatID,
ChatMessage: protobuf.ChatMessage{
Clock: 2,
Text: "some-text"},
From: "me",
Seen: false,
},
{
ID: "3",
LocalChatID: testPublicChatID,
ChatMessage: protobuf.ChatMessage{
Clock: 3,
Text: "some-text"},
From: "me",
Seen: false,
},
})
require.NoError(t, err)
messageID, err = p.FirstUnseenMessageID(testPublicChatID)
require.NoError(t, err)
require.Equal(t, "2", messageID)
}
func TestLatestMessageByChatID(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)

View File

@ -589,6 +589,10 @@ func (api *PublicAPI) MessageByMessageID(messageID string) (*common.Message, err
return api.service.messenger.MessageByID(messageID)
}
func (api *PublicAPI) FirstUnseenMessageID(chatID string) (string, error) {
return api.service.messenger.FirstUnseenMessageID(chatID)
}
func (api *PublicAPI) AllMessagesFromChatWhichMatchTerm(chatID, searchTerm string, caseSensitive bool) (*ApplicationMessagesResponse, error) {
messages, err := api.service.messenger.AllMessageByChatIDWhichMatchTerm(chatID, searchTerm, caseSensitive)
if err != nil {