diff --git a/client/identity.go b/client/identity.go index 7483b87..4f55f65 100644 --- a/client/identity.go +++ b/client/identity.go @@ -31,12 +31,14 @@ type Identity struct { StaticMtkServerPaths []ServerList `json:"static_mtk_server_paths,omitempty"` DynamicMtkServeRules []string `json:"dynamic_mtk_serve_rules,omitempty"` InvitationTimeout int `json:"invitation_timeout,omitempty"` + Uuid string `json:"uuid,omitempty"` unlockedHiddenPeers PeerList } func CreateIdentity(nickname string) *Identity { var id Identity id.Nickname = nickname + id.Uuid = uuid.New().String() id.RootKp = meowlib.NewKeyPair() GetConfig().me = &id id.MessageServers = ServerStorage{DbFile: uuid.NewString()} @@ -44,6 +46,14 @@ func CreateIdentity(nickname string) *Identity { return &id } +func (id *Identity) CreateFolder() error { + err := os.MkdirAll(filepath.Join(GetConfig().StoragePath, id.Uuid), 0700) + if err != nil { + return err + } + return nil +} + // Creates an invitation for a peer, returns the newly created peer including infos to provide a ContactCard func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerUids []string, InvitationMessage string) (*Peer, error) { var peer Peer @@ -171,6 +181,10 @@ func (id *Identity) Save() error { if GetConfig().IdentityFile == "" { return errors.New("identity filename empty") } + //! temp patch + if id.Uuid == "" { + id.Uuid = uuid.New().String() + } b, _ := json.Marshal(id) armor, err := helper.EncryptMessageWithPassword([]byte(GetConfig().memoryPassword), string(b)) if err != nil { @@ -296,7 +310,8 @@ func (id *Identity) SaveBackgroundJob() error { if err != nil { return err } - err = os.WriteFile(filepath.Join(GetConfig().StoragePath, ".jobs"), jsonjobs, 0600) + id.CreateFolder() + err = os.WriteFile(filepath.Join(GetConfig().StoragePath, id.Uuid, ".jobs"), jsonjobs, 0600) if err != nil { return err } diff --git a/client/messagestorage.go b/client/messagestorage.go index 9a4e71d..db0bfaf 100644 --- a/client/messagestorage.go +++ b/client/messagestorage.go @@ -19,14 +19,16 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri if len(peer.DbIds) == 0 { dbid = uuid.NewString() peer.DbIds = []string{dbid} - GetConfig().me.Save() - file, err := os.Create(filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix)) + + GetConfig().GetIdentity().Save() + GetConfig().GetIdentity().CreateFolder() + file, err := os.Create(filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().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, dbid+GetConfig().DbSuffix)) // Open the created SQLite File + sqliteDatabase, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File err = createMessageTable(sqliteDatabase) if err != nil { return err @@ -36,7 +38,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, dbid+GetConfig().DbSuffix)) // Open the created SQLite File + db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File defer db.Close() // Detach Files if len(usermessage.Files) > 0 { @@ -83,7 +85,7 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri } ium := DbMessageToInternalUserMessage(id, dbid, dbm) peer.LastMessage = ium - GetConfig().me.Save() + GetConfig().GetIdentity().Save() return nil } @@ -96,7 +98,7 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserM } 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 + db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().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 { @@ -166,7 +168,7 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in countStack += newCount } // 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 + db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().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 { @@ -211,7 +213,7 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in } func getMessageCount(dbid string) (int, error) { - db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, dbid+GetConfig().DbSuffix)) // Open the created SQLite File + db, _ := sql.Open("sqlite3", filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, dbid+GetConfig().DbSuffix)) // Open the created SQLite File defer db.Close() var count int query := "SELECT COUNT(*) FROM message" diff --git a/client/serverstorage.go b/client/serverstorage.go index a5800d5..934b0c9 100644 --- a/client/serverstorage.go +++ b/client/serverstorage.go @@ -19,7 +19,8 @@ type ServerStorage struct { // Open a badger database from struct ServerStorage func (ss *ServerStorage) open() error { - opts := badger.DefaultOptions(filepath.Join(GetConfig().StoragePath, ss.DbFile)) + + opts := badger.DefaultOptions(filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, ss.DbFile)) opts.Logger = nil var err error ss.db, err = badger.Open(opts)