1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package persistence
import (
"github.com/pkg/errors"
"io/ioutil"
"time"
"dev.narayana.im/narayana/telegabber/yamldb"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var zeroLocation *time.Location
func init() {
var err error
zeroLocation, err = time.LoadLocation("")
if err != nil {
log.Fatal("Wrong hardcoded timezone")
}
}
// SessionsYamlDB wraps YamlDB with Session
type SessionsYamlDB struct {
yamldb.YamlDB
Data *SessionsMap
}
// SessionsMap is for :sessions: subtree
type SessionsMap struct {
Sessions map[string]Session `yaml:":sessions"`
}
// Session is a key-values subtree
type Session struct {
Login string `yaml:":login"`
Timezone string `yaml:":timezone"`
}
var sessionDB *SessionsYamlDB
// SessionMarshaller implementation for YamlDB
func SessionMarshaller() ([]byte, error) {
return yaml.Marshal(sessionDB.Data)
}
// LoadSessions restores TDlib sessions from the previous run
func LoadSessions(path string) (*SessionsYamlDB, error) {
var sessionData SessionsMap
var err error
sessionDB, err = initYamlDB(path, &sessionData)
if err != nil {
return sessionDB, errors.Wrap(err, "Sessions restore error")
}
return sessionDB, nil
}
func emptySessionsMap(dataPtr *SessionsMap) {
dataPtr.Sessions = make(map[string]Session)
}
func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
file, err := ioutil.ReadFile(path)
if err == nil {
err = yaml.Unmarshal(file, dataPtr)
if err != nil {
return nil, errors.Wrap(err, "YamlDB is corrupted")
}
if dataPtr.Sessions == nil {
emptySessionsMap(dataPtr)
}
log.Debugf("Unmarshalled YAML: %#v", *dataPtr)
} else {
// DB file does not exist, create an empty DB
emptySessionsMap(dataPtr)
}
return &SessionsYamlDB{
YamlDB: yamldb.YamlDB{
Path: path,
PathNew: path + ".new",
},
Data: dataPtr,
}, nil
}
// Get retrieves a session value
func (s *Session) Get(key string) (string, error) {
switch key {
case "timezone":
return s.Timezone, nil
}
return "", errors.New("Unknown session property")
}
// ToMap converts the session to a map
func (s *Session) ToMap() map[string]string {
m := make(map[string]string)
for _, configKey := range []string{"timezone"} {
value, _ := s.Get(configKey)
m[configKey] = value
}
return m
}
// Set sets a session value
func (s *Session) Set(key string, value string) (string, error) {
switch key {
case "timezone":
s.Timezone = value
return value, nil
}
return "", errors.New("Unknown session property")
}
func (s *Session) TimezoneToLocation() *time.Location {
time, err := time.Parse("-07:00", s.Timezone)
if err == nil {
return time.Location()
}
// default
return zeroLocation
}
|