-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
168 lines (144 loc) · 3.18 KB
/
user.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"log"
"strconv"
"time"
bolt "go.etcd.io/bbolt"
)
const dbFile string = "data/data.db"
// NOTE: Dear developer only add new fields AT THE EMD to avoid breaking the
// code.
const (
UserSetup int = iota
UserNormal
)
// The Usertype specifies what a user of this bot is
type User struct {
TelegramID int64
GitLabToken string
LastChecked time.Time
HasError bool
State int
}
// Setup the database on the disk.
// This function must be called before using any other function from this file!
func InitUsers() error {
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
return err
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("Users"))
return err
})
return err
}
// Create a new user and save it to Disk
func NewUser(telegramID int64) (User, error) {
u := User{
TelegramID: telegramID,
GitLabToken: "",
State: UserSetup,
HasError: false,
}
err := u.Save()
return u, err
}
// Load a user from disk with a specified id
func LoadUser(telegramID int64) (User, error) {
// Open the database
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
return User{}, err
}
// Start a read transaction and close the database
var rawJSON []byte
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Users"))
v := b.Get([]byte(strconv.FormatInt(telegramID, 10)))
rawJSON = make([]byte, len(v))
copy(rawJSON, v)
return nil
})
db.Close()
if err != nil {
return User{}, err
}
// Decode the json
user := User{}
err = json.Unmarshal(rawJSON, &user)
if err != nil {
// Since we can't parse the json, we asume that the user doeesn't
// exist so we try to create it
user, err := NewUser(telegramID)
if err != nil {
log.Print("[Error] Unable to create new user")
return User{}, err
}
log.Printf("[Info] Created new user %v", telegramID)
return user, nil
}
return user, nil
}
// Load all user
func LoadAllUsers() ([]User, error) {
// Open the database
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
return nil, err
}
// Start the transaction
cache := make(map[string][]byte)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Users"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
tmp := make([]byte, len(v))
copy(tmp, v)
cache[string(k)] = tmp
}
return nil
})
db.Close()
if err != nil {
return nil, err
}
// Decode the jsons
users := make([]User, 0, len(cache))
for k, v := range cache {
tmp := User{}
err := json.Unmarshal(v, &tmp)
if err != nil {
continue
}
tmp.TelegramID, err = strconv.ParseInt(k, 10, 64)
users = append(users, tmp)
}
return users, err
}
// Save the current User to Disk
func (u *User) Save() error {
// Create the json
rawJSON, err := json.Marshal(u)
if err != nil {
return err
}
// Open the database
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
return err
}
defer db.Close()
// Start the transaction
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Users"))
err := b.Put([]byte(strconv.FormatInt(u.TelegramID, 10)), rawJSON)
return err
})
if err != nil {
return err
}
return nil
}