Refactor message storage and retrieval
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc 2024-03-31 19:04:37 +02:00
parent 3bf75eb990
commit aaa4d88a2f

View File

@ -14,21 +14,23 @@ import (
func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []string, password string) error {
var dbid string
cfg := GetConfig()
identity := cfg.GetIdentity()
// If no db/no ID create DB + Tablz
// TODO : if file size > X new db
if len(peer.DbIds) == 0 {
dbid = uuid.NewString()
peer.DbIds = []string{dbid}
GetConfig().GetIdentity().Save()
GetConfig().GetIdentity().CreateFolder()
file, err := os.Create(filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix))
identity.Save()
identity.CreateFolder()
file, err := os.Create(filepath.Join(cfg.StoragePath, identity.Uuid, dbid+GetConfig().DbSuffix))
if err != nil {
return err
}
file.Close()
peer.DbIds = append(peer.DbIds, dbid)
sqliteDatabase, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
sqliteDatabase, _ := sql.Open("sqlite3", filepath.Join(cfg.StoragePath, identity.Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
err = createMessageTable(sqliteDatabase)
if err != nil {
return err
@ -38,7 +40,7 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri
dbid = peer.DbIds[len(peer.DbIds)-1]
}
// Open Db
db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
db, _ := sql.Open("sqlite3", filepath.Join(cfg.StoragePath, identity.Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
// Detach Files
if len(usermessage.Files) > 0 {
@ -85,20 +87,22 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri
}
ium := DbMessageToInternalUserMessage(id, dbid, dbm)
peer.LastMessage = ium
GetConfig().GetIdentity().Save()
identity.Save()
return nil
}
// Get new messages from a peer
func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserMessage, error) {
var messages []*InternalUserMessage
cfg := GetConfig()
identity := cfg.GetIdentity()
// 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, GetConfig().GetIdentity().Uuid, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
db, _ := sql.Open("sqlite3", filepath.Join(cfg.StoragePath, identity.Uuid, peer.DbIds[fileidx]+GetConfig().DbSuffix)) // Open the created SQLite File
defer db.Close()
// if it's first app query, it won't hold a lastIndex, so let's start from end
if lastDbId == 0 {