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