Save history to disk. Fixes #1

This commit is contained in:
Tulir Asokan
2018-03-22 19:51:20 +02:00
parent 232f7fe1be
commit 702a75a8c0
8 changed files with 147 additions and 25 deletions

View File

@ -30,19 +30,29 @@ type Config struct {
UserID string `yaml:"mxid"`
HS string `yaml:"homeserver"`
dir string `yaml:"-"`
Session *Session `yaml:"-"`
Dir string `yaml:"-"`
HistoryDir string `yaml:"history_dir"`
Session *Session `yaml:"-"`
}
func NewConfig(dir string) *Config {
return &Config{
dir: dir,
Dir: dir,
HistoryDir: filepath.Join(dir, "history"),
}
}
func (config *Config) Clear() {
if config.Session != nil {
config.Session.Clear()
}
os.RemoveAll(config.HistoryDir)
}
func (config *Config) Load() {
os.MkdirAll(config.dir, 0700)
configPath := filepath.Join(config.dir, "config.yaml")
os.MkdirAll(config.Dir, 0700)
os.MkdirAll(config.HistoryDir, 0700)
configPath := filepath.Join(config.Dir, "config.yaml")
data, err := ioutil.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
@ -61,14 +71,14 @@ func (config *Config) Load() {
}
func (config *Config) Save() {
os.MkdirAll(config.dir, 0700)
os.MkdirAll(config.Dir, 0700)
data, err := yaml.Marshal(&config)
if err != nil {
debug.Print("Failed to marshal config")
panic(err)
}
path := filepath.Join(config.dir, "config.yaml")
path := filepath.Join(config.Dir, "config.yaml")
err = ioutil.WriteFile(path, data, 0600)
if err != nil {
debug.Print("Failed to write config to", path)

View File

@ -23,7 +23,7 @@ import (
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/matrix/pushrules"
rooms "maunium.net/go/gomuks/matrix/room"
"maunium.net/go/gomuks/matrix/room"
"maunium.net/go/gomuks/ui/debug"
)
@ -45,7 +45,7 @@ func (config *Config) LoadSession(mxid string) error {
func (config *Config) NewSession(mxid string) *Session {
return &Session{
UserID: mxid,
path: filepath.Join(config.dir, mxid+".session"),
path: filepath.Join(config.Dir, mxid+".session"),
Rooms: make(map[string]*rooms.Room),
}
}
@ -61,13 +61,13 @@ func (s *Session) Clear() {
func (s *Session) Load() error {
data, err := ioutil.ReadFile(s.path)
if err != nil {
debug.Print("Failed to read session from", s.path, err)
debug.Printf("Failed to read session from %s: %v", s.path, err)
return err
}
err = json.Unmarshal(data, s)
if err != nil {
debug.Print("Failed to parse session at", s.path, err)
debug.Printf("Failed to parse session at %s: %v", s.path, err)
return err
}
return nil
@ -76,13 +76,13 @@ func (s *Session) Load() error {
func (s *Session) Save() error {
data, err := json.Marshal(s)
if err != nil {
debug.Print("Failed to marshal session of", s.UserID, err)
debug.Printf("Failed to marshal session of %s: %v", s.UserID, err)
return err
}
err = ioutil.WriteFile(s.path, data, 0600)
if err != nil {
debug.Print("Failed to write session to", s.path, err)
debug.Printf("Failed to write session of %s to %s: %v", s.UserID, s.path, err)
return err
}
return nil