-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.go
86 lines (72 loc) · 1.46 KB
/
notification.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
package main
import (
"strings"
bf "github.com/goiste/bit_flags"
)
const (
Messages = 1 << iota // 1
Replies // 2
Likes // 4
NewArticles // ... powers of 2
News
ByEmail
BySms
ByTelegram
AllNotifications = Messages | Replies | Likes | NewArticles | News
AllMethods = ByEmail | BySms | ByTelegram
)
type Notification struct {
SomeOtherFields string
Flags bf.BitFlags[uint8]
}
func New() *Notification {
n := &Notification{}
n.SetDefaultFlags()
return n
}
func (n *Notification) SetDefaultFlags() {
n.Flags.Set(Messages | Replies | Likes | ByEmail)
}
func (n *Notification) SetAll() {
n.Flags.Set(AllNotifications | AllMethods)
}
func (n *Notification) SetNone() {
n.Flags.Reset()
}
func (n Notification) HasFlag(flag uint8) bool {
return n.Flags.Has(flag)
}
func (n Notification) FlagsToString() string {
return strings.Join(n.GetFlagsNames(), ", ")
}
func (n Notification) GetFlagsNames() (names []string) {
for _, p := range n.Flags.List() {
name := getFlagName(p)
if name == "" {
continue
}
names = append(names, name)
}
return
}
func getFlagName(flag uint8) string {
switch flag {
case Messages:
return "Messages"
case Replies:
return "Replies"
case Likes:
return "Likes"
case NewArticles:
return "New articles"
case News:
return "News"
case ByEmail:
return "By email"
case BySms:
return "By sms"
case ByTelegram:
return "By telegram"
}
return ""
}