@@ 742,18 742,30 @@ func Login() *mautrix.Client {
func run() int {
client := Login()
defer client.Logout()
+
+ app := NewApp(client)
if storer, err := loadStorer(client.UserID); err == nil {
client.Store = storer
+ if directChats, err := loadDirectChats(client.UserID); err == nil {
+ app.DirectChats = directChats
+ } else {
+ fmt.Fprintln(os.Stderr, "failed to direct chats: " + err.Error())
+ }
} else {
fmt.Fprintln(os.Stderr, "failed to load cache: " + err.Error())
}
- if code := NewApp(client).Run(); code != 0 {
+
+ if code := app.Run(); code != 0 {
return code
}
err := saveStorer(client.Store.(*mautrix.InMemoryStore), client.UserID)
if err != nil {
fatal(err.Error())
}
+ err = saveDirectChats(app.DirectChats, client.UserID)
+ if err != nil {
+ fatal(err.Error())
+ }
return 0
}
@@ 3,6 3,7 @@ package main
import (
"bytes"
"encoding/gob"
+ "encoding/json"
"errors"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
@@ 71,3 72,37 @@ func saveStorer(storer *mautrix.InMemoryStore, user id.UserID) error {
}
return nil
}
+
+func loadDirectChats(user id.UserID) (map[id.RoomID]id.UserID, error) {
+ dir, err := os.UserCacheDir()
+ if err != nil {
+ return nil, err
+ }
+ dir = filepath.Join(dir, "sup", string(user))
+
+ if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
+ return nil, nil
+ }
+
+ data, err := os.ReadFile(filepath.Join(dir, "direct_chats.json"))
+ if err != nil {
+ return nil, err
+ }
+ var directChats map[id.RoomID]id.UserID
+ json.Unmarshal(data, &directChats)
+ return directChats, nil
+}
+
+func saveDirectChats(directChats map[id.RoomID]id.UserID, user id.UserID) error {
+ dir, err := os.UserCacheDir()
+ if err != nil {
+ return err
+ }
+ dir = filepath.Join(dir, "sup", string(user))
+
+ data, err := json.Marshal(directChats)
+ if err != nil {
+ return err
+ }
+ return os.WriteFile(filepath.Join(dir, "direct_chats.json"), data, 0600)
+}