-
Notifications
You must be signed in to change notification settings - Fork 11
/
userdb.go
172 lines (150 loc) · 3.23 KB
/
userdb.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
169
170
171
172
package userdb
import (
"fmt"
"os"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/jbsmith7741/toml"
"github.com/xackery/talkeq/config"
"github.com/xackery/talkeq/tlog"
)
var (
isStarted bool
mu sync.RWMutex
users map[string]UserEntry
usersDatabasePath string
)
// UserEntry represents a record in the database
type UserEntry struct {
CharacterName string
DiscordID string
}
// New initializes and creates the user database
func New(config *config.Config) error {
if isStarted {
return fmt.Errorf("already started")
}
usersDatabasePath = config.UsersDatabasePath
tlog.Debugf("[userdb] initializing user db")
_, err := os.Stat(usersDatabasePath)
if os.IsNotExist(err) {
tlog.Debugf("[userdb] not found, creating a new one")
f, err := os.Create(usersDatabasePath)
if err != nil {
return fmt.Errorf("create user database: %w", err)
}
defer f.Close()
enc := toml.NewEncoder(f)
mu.Lock()
err = enc.Encode(users)
mu.Unlock()
if err != nil {
return fmt.Errorf("create user database: %w", err)
}
return nil
}
err = reload()
if err != nil {
return fmt.Errorf("reload: %w", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("newWatcher: %w", err)
}
err = watcher.Add(config.UsersDatabasePath)
if err != nil {
return fmt.Errorf("watcherAdd: %w", err)
}
go loop(watcher)
return nil
}
func loop(watcher *fsnotify.Watcher) {
if !isStarted {
return
}
mu.Lock()
isStarted = true
mu.Unlock()
defer watcher.Close()
var err error
for {
select {
case event, ok := <-watcher.Events:
if !ok {
tlog.Warn("[userdb] failed to read file")
return
}
if event.Op&fsnotify.Write != fsnotify.Write {
continue
}
tlog.Debugf("[userdb] modified, reloading")
err = reload()
if err != nil {
tlog.Warnf("[userdb] reload failed, ignoring: %s", err)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
tlog.Warnf("[userdb] read failed, ignoring: %s", err)
}
}
}
func reload() error {
mu.Lock()
defer mu.Unlock()
ue := make(map[string]UserEntry)
_, err := os.Stat(usersDatabasePath)
if os.IsNotExist(err) {
return fmt.Errorf("%s reload failed: %w", usersDatabasePath, err)
}
_, err = toml.DecodeFile(usersDatabasePath, &ue)
if err != nil {
return fmt.Errorf("decode: %w", err)
}
users = ue
return nil
}
// Set updates or adds an entry for a specified user id
func Set(discordID string, characterName string) {
mu.Lock()
ue, ok := users[discordID]
if ok {
ue.CharacterName = characterName
ue.DiscordID = discordID
return
}
ue = UserEntry{
DiscordID: discordID,
CharacterName: characterName,
}
users[discordID] = ue
err := save()
if err != nil {
tlog.Warnf("[userdb] save failed: %s", err)
}
}
// Name returns the name of a user based on their ID
func Name(discordID string) string {
var name string
mu.RLock()
ue, ok := users[discordID]
if ok {
name = ue.CharacterName
}
mu.RUnlock()
return name
}
func save() error {
f, err := os.Create(usersDatabasePath)
if err != nil {
return fmt.Errorf("create: %w", err)
}
defer f.Close()
enc := toml.NewEncoder(f)
err = enc.Encode(users)
if err != nil {
return fmt.Errorf("encode: %w", err)
}
return nil
}