-
Notifications
You must be signed in to change notification settings - Fork 0
/
usernotify.go
91 lines (68 loc) · 1.71 KB
/
usernotify.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
package main
import (
"sort"
"sync"
)
type discordUserMention string
type nickname string
// NotifyMap maps one player name to a discord user.
type NotifyMap struct {
sync.Mutex
m map[nickname]map[discordUserMention]bool
}
func newNotifyMap() *NotifyMap {
n := NotifyMap{}
n.m = make(map[nickname]map[discordUserMention]bool, 32)
return &n
}
// Add adds a new nickname to be tracked by Discord User ID
func (n *NotifyMap) Add(discordMention, nick string) {
n.Lock()
defer n.Unlock()
playername := nickname(nick)
discordName := discordUserMention(discordMention)
// initialize
if n.m[playername] == nil {
n.m[playername] = make(map[discordUserMention]bool, 2)
}
// add dc user to tracked users of player
n.m[playername][discordName] = true
}
// Remove removes a moderator's tracking of a specific user.
func (n *NotifyMap) Remove(discordMention string) {
n.Lock()
defer n.Unlock()
dcMention := discordUserMention(discordMention)
for playername, dcUserMap := range n.m {
if len(dcUserMap) == 1 {
_, ok := dcUserMap[dcMention]
if ok {
// player is tracked by dc user
delete(n.m, playername)
}
} else if len(dcUserMap) > 1 {
_, ok := dcUserMap[dcMention]
if ok {
// player is tracked by dc user
delete(dcUserMap, dcMention)
}
}
}
}
// Tracked checks if a nickname is tracked and returns eithernil or the
// pointer to the requesting user.
func (n *NotifyMap) Tracked(nick string) (mentions []string) {
n.Lock()
defer n.Unlock()
name := nickname(nick)
dcUsers, ok := n.m[name]
if !ok {
return
}
mentions = make([]string, 0, len(dcUsers))
for dcUser := range dcUsers {
mentions = append(mentions, string(dcUser))
}
sort.Sort(byName(mentions))
return
}