store msg db and inbox by identity
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc 2024-03-29 18:07:06 +01:00
parent 0b8e3c4c90
commit 31df45e771
3 changed files with 28 additions and 10 deletions

View File

@ -31,12 +31,14 @@ type Identity struct {
StaticMtkServerPaths []ServerList `json:"static_mtk_server_paths,omitempty"` StaticMtkServerPaths []ServerList `json:"static_mtk_server_paths,omitempty"`
DynamicMtkServeRules []string `json:"dynamic_mtk_serve_rules,omitempty"` DynamicMtkServeRules []string `json:"dynamic_mtk_serve_rules,omitempty"`
InvitationTimeout int `json:"invitation_timeout,omitempty"` InvitationTimeout int `json:"invitation_timeout,omitempty"`
Uuid string `json:"uuid,omitempty"`
unlockedHiddenPeers PeerList unlockedHiddenPeers PeerList
} }
func CreateIdentity(nickname string) *Identity { func CreateIdentity(nickname string) *Identity {
var id Identity var id Identity
id.Nickname = nickname id.Nickname = nickname
id.Uuid = uuid.New().String()
id.RootKp = meowlib.NewKeyPair() id.RootKp = meowlib.NewKeyPair()
GetConfig().me = &id GetConfig().me = &id
id.MessageServers = ServerStorage{DbFile: uuid.NewString()} id.MessageServers = ServerStorage{DbFile: uuid.NewString()}
@ -44,6 +46,14 @@ func CreateIdentity(nickname string) *Identity {
return &id 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 // 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) { func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerUids []string, InvitationMessage string) (*Peer, error) {
var peer Peer var peer Peer
@ -171,6 +181,10 @@ func (id *Identity) Save() error {
if GetConfig().IdentityFile == "" { if GetConfig().IdentityFile == "" {
return errors.New("identity filename empty") return errors.New("identity filename empty")
} }
//! temp patch
if id.Uuid == "" {
id.Uuid = uuid.New().String()
}
b, _ := json.Marshal(id) b, _ := json.Marshal(id)
armor, err := helper.EncryptMessageWithPassword([]byte(GetConfig().memoryPassword), string(b)) armor, err := helper.EncryptMessageWithPassword([]byte(GetConfig().memoryPassword), string(b))
if err != nil { if err != nil {
@ -296,7 +310,8 @@ func (id *Identity) SaveBackgroundJob() error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }

View File

@ -19,14 +19,16 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []stri
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().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 { 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, 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) err = createMessageTable(sqliteDatabase)
if err != nil { if err != nil {
return err return err
@ -36,7 +38,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, 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() defer db.Close()
// Detach Files // Detach Files
if len(usermessage.Files) > 0 { if len(usermessage.Files) > 0 {
@ -83,7 +85,7 @@ 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().me.Save() GetConfig().GetIdentity().Save()
return nil return nil
} }
@ -96,7 +98,7 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserM
} }
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, 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() 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 {
@ -166,7 +168,7 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in
countStack += newCount countStack += newCount
} }
// 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, 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() 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 {
@ -211,7 +213,7 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in
} }
func getMessageCount(dbid string) (int, error) { 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() defer db.Close()
var count int var count int
query := "SELECT COUNT(*) FROM message" query := "SELECT COUNT(*) FROM message"

View File

@ -19,7 +19,8 @@ type ServerStorage struct {
// Open a badger database from struct ServerStorage // Open a badger database from struct ServerStorage
func (ss *ServerStorage) open() error { 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 opts.Logger = nil
var err error var err error
ss.db, err = badger.Open(opts) ss.db, err = badger.Open(opts)