-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.go
More file actions
84 lines (68 loc) · 2.32 KB
/
user.go
File metadata and controls
84 lines (68 loc) · 2.32 KB
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
package main
import (
"log"
"time"
)
type UserData struct {
PasswordHash string
UserName string
CloudKey string
ID int
ReceivingAddresses []ReceivingAddress
TelegramUserId string
Paid int
Admin int
LastActive int64 // unix_epoch
TimeZone string
}
type ReceivingAddress struct {
DisplayName string
ReceivingAddress string
}
var lastActive = make(map[int]int64)
var userList = make(map[string]UserData)
var cloudKeyList = make(map[string]UserData)
var userIDList = make(map[int]UserData)
func updateUserLastActive(userID int) {
_, err := db.Exec("UPDATE users SET last_active = ? WHERE ID = ?", time.Now().Unix(), userID)
if err != nil {
log.Printf("Failed to update last active time for user id %d: %s\n", userID, err.Error())
}
}
// Get all users in the DB:
func getAllUserInfo() {
results, err := db.Query("select password_hash, username, cloud_key, id, telegram_user_id, paid, admin, last_active, timezone from users")
if err != nil {
log.Printf("Unable to get user data from DB\n")
return
}
for results.Next() {
var thisUser UserData
err = results.Scan(&thisUser.PasswordHash, &thisUser.UserName, &thisUser.CloudKey, &thisUser.ID, &thisUser.TelegramUserId, &thisUser.Paid, &thisUser.Admin, &thisUser.LastActive, &thisUser.TimeZone)
if err != nil {
log.Printf("Unable to read user from DB\n")
continue
}
results2, err2 := db.Query("select display_name, receiving_address from receiving_addresses where user_id = ?", thisUser.ID)
if err2 != nil {
log.Printf("Unable to get receiving addresses for user id: %d\n", thisUser.ID)
} else {
var thisAddress ReceivingAddress
for results2.Next() {
err = results2.Scan(&thisAddress.DisplayName, &thisAddress.ReceivingAddress)
if err != nil {
log.Printf("Unable to read receiving address from DB for user_id %d\n", thisUser.ID)
} else {
log.Printf("Got receiving address %s for user %s", thisAddress.ReceivingAddress, thisUser.UserName)
thisUser.ReceivingAddresses = append(thisUser.ReceivingAddresses, thisAddress)
}
}
}
mutex.Lock()
userList[thisUser.UserName] = thisUser
cloudKeyList[thisUser.CloudKey] = thisUser
userIDList[thisUser.ID] = thisUser
lastActive[thisUser.ID] = thisUser.LastActive
mutex.Unlock()
}
}