2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-11-21 16:19:22 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTableUserMessagesAllFieldsCount(t *testing.T) {
|
|
|
|
db := sqlitePersistence{}
|
|
|
|
expected := len(strings.Split(db.tableUserMessagesLegacyAllFields(), ","))
|
|
|
|
require.Equal(t, expected, db.tableUserMessagesLegacyAllFieldsCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveMessages(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
id := strconv.Itoa(i)
|
|
|
|
err := insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, id, m.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageByID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, id, m.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessagesExist(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, "1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result, err := p.MessagesExist([]string{"1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, result["1"])
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, "2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result, err = p.MessagesExist([]string{"1", "2", "3"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, result["1"])
|
|
|
|
require.True(t, result["2"])
|
|
|
|
require.False(t, result["3"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageByChatID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
2020-02-21 14:48:53 +00:00
|
|
|
chatID := testPublicChatID
|
2019-11-21 16:19:22 +00:00
|
|
|
count := 1000
|
|
|
|
pageSize := 50
|
|
|
|
|
|
|
|
var messages []*Message
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
messages = append(messages, &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: strconv.Itoa(i),
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: uint64(i),
|
|
|
|
},
|
|
|
|
From: "me",
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Add some other chats.
|
|
|
|
if count%5 == 0 {
|
|
|
|
messages = append(messages, &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: strconv.Itoa(count + i),
|
|
|
|
LocalChatID: "other-chat",
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: uint64(i),
|
|
|
|
},
|
|
|
|
|
|
|
|
From: "me",
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some out-of-order message. Add more than page size.
|
|
|
|
outOfOrderCount := pageSize + 1
|
|
|
|
allCount := count + outOfOrderCount
|
|
|
|
for i := 0; i < pageSize+1; i++ {
|
|
|
|
messages = append(messages, &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: strconv.Itoa(count*2 + i),
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: uint64(i),
|
|
|
|
},
|
|
|
|
|
|
|
|
From: "me",
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var (
|
|
|
|
result []*Message
|
|
|
|
cursor string
|
|
|
|
iter int
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
items []*Message
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
items, cursor, err = p.MessageByChatID(chatID, cursor, pageSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
result = append(result, items...)
|
|
|
|
|
|
|
|
iter++
|
|
|
|
if len(cursor) == 0 || iter > count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "", cursor) // for loop should exit because of cursor being empty
|
|
|
|
require.EqualValues(t, math.Ceil(float64(allCount)/float64(pageSize)), iter)
|
|
|
|
require.Equal(t, len(result), allCount)
|
|
|
|
require.True(
|
|
|
|
t,
|
|
|
|
// Verify descending order.
|
|
|
|
sort.SliceIsSorted(result, func(i, j int) bool {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return result[i].Clock > result[j].Clock
|
2019-11-21 16:19:22 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageReplies(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
2020-02-21 14:48:53 +00:00
|
|
|
chatID := testPublicChatID
|
2019-11-21 16:19:22 +00:00
|
|
|
message1 := &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: "id-1",
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Text: "content-1",
|
|
|
|
Clock: uint64(1),
|
|
|
|
},
|
|
|
|
From: "1",
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
message2 := &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: "id-2",
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Text: "content-2",
|
|
|
|
Clock: uint64(2),
|
|
|
|
ResponseTo: "id-1",
|
|
|
|
},
|
|
|
|
|
|
|
|
From: "2",
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
message3 := &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: "id-3",
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Text: "content-3",
|
|
|
|
Clock: uint64(3),
|
|
|
|
ResponseTo: "non-existing",
|
|
|
|
},
|
|
|
|
From: "3",
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messages := []*Message{message1, message2, message3}
|
|
|
|
|
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
retrievedMessages, _, err := p.MessageByChatID(chatID, "", 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
require.Equal(t, "non-existing", retrievedMessages[0].ResponseTo)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.Nil(t, retrievedMessages[0].QuotedMessage)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
require.Equal(t, "id-1", retrievedMessages[1].ResponseTo)
|
|
|
|
require.Equal(t, &QuotedMessage{From: "1", Text: "content-1"}, retrievedMessages[1].QuotedMessage)
|
2019-11-21 16:19:22 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
require.Equal(t, "", retrievedMessages[2].ResponseTo)
|
2019-11-21 16:19:22 +00:00
|
|
|
require.Nil(t, retrievedMessages[2].QuotedMessage)
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
func TestMessageByChatIDWithTheSameClocks(t *testing.T) {
|
2019-11-21 16:19:22 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
2020-02-21 14:48:53 +00:00
|
|
|
chatID := testPublicChatID
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
clockValues := []uint64{10, 10, 9, 9, 9, 11, 12, 11, 100000, 6, 4, 5, 5, 5, 5}
|
2019-11-21 16:19:22 +00:00
|
|
|
count := len(clockValues)
|
|
|
|
pageSize := 2
|
|
|
|
|
|
|
|
var messages []*Message
|
|
|
|
|
|
|
|
for i, clock := range clockValues {
|
|
|
|
messages = append(messages, &Message{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: strconv.Itoa(i),
|
|
|
|
LocalChatID: chatID,
|
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: clock,
|
|
|
|
},
|
|
|
|
From: "me",
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.SaveMessagesLegacy(messages)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var (
|
|
|
|
result []*Message
|
|
|
|
cursor string
|
|
|
|
iter int
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
var (
|
|
|
|
items []*Message
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
items, cursor, err = p.MessageByChatID(chatID, cursor, pageSize)
|
|
|
|
require.NoError(t, err)
|
|
|
|
result = append(result, items...)
|
|
|
|
|
|
|
|
iter++
|
|
|
|
if cursor == "" || iter > count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Empty(t, cursor) // for loop should exit because of cursor being empty
|
|
|
|
require.Len(t, result, count)
|
|
|
|
// Verify the order.
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
expectedClocks := make([]uint64, len(clockValues))
|
|
|
|
copy(expectedClocks, clockValues)
|
|
|
|
sort.Slice(expectedClocks, func(i, j int) bool {
|
|
|
|
return expectedClocks[i] > expectedClocks[j]
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
resultClocks := make([]uint64, 0, len(clockValues))
|
2019-11-21 16:19:22 +00:00
|
|
|
for _, m := range result {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
resultClocks = append(resultClocks, m.Clock)
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
require.EqualValues(t, expectedClocks, resultClocks)
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteMessageByID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, id, m.ID)
|
|
|
|
|
|
|
|
err = p.DeleteMessage(m.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = p.MessageByID(id)
|
|
|
|
require.EqualError(t, err, "record not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteMessagesByChatID(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, "1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, "2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, _, err := p.MessageByChatID("chat-id", "", 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(m))
|
|
|
|
|
|
|
|
err = p.DeleteMessagesByChatID("chat-id")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, _, err = p.MessageByChatID("chat-id", "", 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(m))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarkMessageSeen(t *testing.T) {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
chatID := "test-chat"
|
2019-11-21 16:19:22 +00:00
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, m.Seen)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
err = p.MarkMessagesSeen(chatID, []string{m.ID})
|
2019-11-21 16:19:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err = p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, m.Seen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateMessageOutgoingStatus(t *testing.T) {
|
|
|
|
db, err := openTestDB()
|
|
|
|
require.NoError(t, err)
|
|
|
|
p := sqlitePersistence{db: db}
|
|
|
|
id := "1"
|
|
|
|
|
|
|
|
err = insertMinimalMessage(p, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = p.UpdateMessageOutgoingStatus(id, "new-status")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := p.MessageByID(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "new-status", m.OutgoingStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func openTestDB() (*sql.DB, error) {
|
|
|
|
dbPath, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sqlite.Open(dbPath.Name(), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertMinimalMessage(p sqlitePersistence, id string) error {
|
2019-12-19 15:40:44 +00:00
|
|
|
return p.SaveMessagesLegacy([]*Message{{
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
ID: id,
|
|
|
|
LocalChatID: "chat-id",
|
|
|
|
ChatMessage: protobuf.ChatMessage{Text: "some-text"},
|
|
|
|
From: "me",
|
2019-11-21 16:19:22 +00:00
|
|
|
}})
|
|
|
|
}
|