diff --git a/client/messagestorage.go b/client/messagestorage.go index 41ee534..9a4e71d 100644 --- a/client/messagestorage.go +++ b/client/messagestorage.go @@ -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]) diff --git a/client/messagestorage_test.go b/client/messagestorage_test.go index 7db6ad8..c2a1b5f 100644 --- a/client/messagestorage_test.go +++ b/client/messagestorage_test.go @@ -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) }