From aaa4d88a2fedcf5c7baca65f178be7f0aaabd5f9 Mon Sep 17 00:00:00 2001 From: ycc Date: Sun, 31 Mar 2024 19:04:37 +0200 Subject: [PATCH] Refactor message storage and retrieval --- client/messagestorage.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/client/messagestorage.go b/client/messagestorage.go index db0bfaf..56e4bce 100644 --- a/client/messagestorage.go +++ b/client/messagestorage.go @@ -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 {