-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
129 lines (121 loc) · 3.66 KB
/
users.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
package main
import (
"log"
"sync"
"time"
)
var userList = privateUsers{}
var privateUsersLock sync.RWMutex
type privateUser struct {
ID string
DiscordID string
Name string
GamerTag string
DisplayName string
Image string
Thumb string
Seen int64
}
type privateUsers map[string]*privateUser
func maybeInsertUser(discordID, nickname, image, thumb string) {
privateUsersLock.Lock()
defer privateUsersLock.Unlock()
for id, u := range userList {
if u.DiscordID != discordID {
continue
}
if u.Name != nickname || u.DisplayName != nickname {
oldName := userList[id].Name
userList[id].DisplayName = nickname
userList[id].Name = nickname
if _, err := updateName.Exec(nickname, id); err != nil {
log.Printf("Error updating name for %s to %s: %s", userList[id].Name, nickname, err.Error())
} else {
log.Printf("Updated name for %s to %s", oldName, userList[id].Name)
}
}
if u.Thumb != thumb {
userList[id].Thumb = thumb
if _, err := insertUserMeta.Exec(id, "thumb", thumb); err != nil {
log.Printf("Error updating thumb for %s to %s: %s", userList[id].Name, thumb, err.Error())
} else {
log.Printf("Updated thumb for %s to %s", userList[id].Name, thumb)
}
}
if u.Image != image {
userList[id].Image = image
if _, err := insertUserMeta.Exec(id, "image", image); err != nil {
log.Printf("Error updating image for %s to %s: %s", userList[id].Name, image, err.Error())
} else {
log.Printf("Updated image for %s to %s", userList[id].Name, image)
}
}
return
}
if _, err := insertUser.Exec(discordID, nickname); err != nil {
log.Printf("Error inserting user %s (%s): %s", discordID, nickname, err.Error())
return
}
u := new(user)
if err := u.fromRow(getDiscordUser.QueryRow(discordID)); err != nil {
log.Printf("Error querying user %s (%s) after insert: %s", discordID, nickname, err.Error())
return
}
p := u.privateUser()
userList[p.ID] = p
userList[p.ID].Thumb = thumb
if _, err := insertUserMeta.Exec(p.ID, "thumb", thumb); err != nil {
log.Printf("Error updating thumb for inserted user %s to %s: %s", userList[p.ID].Name, thumb, err.Error())
} else {
log.Printf("Updated thumb for inserted user %s to %s", userList[p.ID].Name, thumb)
}
userList[p.ID].Image = image
if _, err := insertUserMeta.Exec(p.ID, "image", image); err != nil {
log.Printf("Error updating image for inserted user %s to %s: %s", userList[p.ID].Name, image, err.Error())
} else {
log.Printf("Updated image for inserted user %s to %s", userList[p.ID].Name, image)
}
log.Printf("Created new user id %s: %s, discord: %s, image: %s", p.ID, p.Name, p.DiscordID, p.Image)
}
func maybeUpdateSeen(discordID string) {
privateUsersLock.Lock()
defer privateUsersLock.Unlock()
for id, u := range userList {
if u.DiscordID != discordID {
continue
}
now := time.Now().Unix()
if now < (u.Seen + 60) {
return
}
userList[id].Seen = now
if _, err := updateSeen.Exec(now, id); err != nil {
log.Printf("Error updating seen for %s (%s): %s", userList[id].Name, id, err.Error())
} else {
log.Printf("Updated seen for %s (%s): %d", userList[id].Name, id, now)
}
return
}
}
func loadUsersFromDatabase() {
for {
go func(){
if rows, err := getAllUsers.Query(); err == nil {
privateUsersLock.Lock()
defer privateUsersLock.Unlock()
defer rows.Close()
for rows.Next() {
u := new(user)
if err := u.fromRows(rows); err != nil {
log.Printf("Error loading user from database: %s", err.Error())
continue
}
p := u.privateUser()
userList[p.ID] = p
}
return
}
}()
time.Sleep(5 * time.Second) // should probably use a tick/chan?
}
}