handle no db yet in message retrieve
continuous-integration/drone/push Build is failing Details

This commit is contained in:
ycc 2024-03-05 23:15:19 +01:00
parent f40f6520d2
commit 0a70206b11
2 changed files with 15 additions and 1 deletions

View File

@ -90,6 +90,10 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri
// Get new messages from a peer
func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserMessage, error) {
var messages []*InternalUserMessage
// handle no db yet
if len(peer.DbIds) == 0 {
return messages, nil
}
fileidx := len(peer.DbIds) - 1
// There fileidx should provide the db that we need (unless wantMore overlaps the next DB)
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
@ -139,6 +143,10 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserM
// Get old messages from a peer
func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore int, password string) ([]InternalUserMessage, error) {
var messages []InternalUserMessage
// handle no db yet
if len(peer.DbIds) == 0 {
return messages, nil
}
fileidx := len(peer.DbIds) - 1
// initialize count with last db message count
countStack, err := getMessageCount(peer.DbIds[fileidx])

View File

@ -42,6 +42,12 @@ func TestStoreMessage(t *testing.T) {
func TestManyStoreMessage(t *testing.T) {
id := createId()
// test with zero messages
messages, err := GetMessagesHistory(id.Peers[0], 0, 0, 10, GetConfig().memoryPassword)
if err != nil {
log.Fatal(err)
}
assert.Equal(t, len(messages), 0, "not 0 message")
for i := 1; i < 100; i++ {
var um meowlib.UserMessage
um.Data = []byte(randomLenString(20, 200))
@ -50,7 +56,7 @@ func TestManyStoreMessage(t *testing.T) {
log.Fatal(err)
}
}
messages, err := GetMessagesHistory(id.Peers[0], 0, 0, 10, GetConfig().memoryPassword)
messages, err = GetMessagesHistory(id.Peers[0], 0, 0, 10, GetConfig().memoryPassword)
if err != nil {
log.Fatal(err)
}