-
Notifications
You must be signed in to change notification settings - Fork 17
/
event_types.go
121 lines (90 loc) · 2.89 KB
/
event_types.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
package proton
import (
"fmt"
"strings"
"github.com/bradenaw/juniper/xslices"
)
type Event struct {
EventID string
Refresh RefreshFlag
User *User
UserSettings *UserSettings
MailSettings *MailSettings
Messages []MessageEvent
Labels []LabelEvent
Addresses []AddressEvent
Notifications []NotificationEvent
UsedSpace *int64
}
func (event Event) String() string {
var parts []string
if event.Refresh != 0 {
parts = append(parts, fmt.Sprintf("refresh: %v", event.Refresh))
}
if event.User != nil {
parts = append(parts, "user: [modified]")
}
if event.MailSettings != nil {
parts = append(parts, "mail-settings: [modified]")
}
if len(event.Messages) > 0 {
parts = append(parts, fmt.Sprintf(
"messages: created=%d, updated=%d, deleted=%d",
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventCreate }),
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }),
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventDelete }),
))
}
if len(event.Labels) > 0 {
parts = append(parts, fmt.Sprintf(
"labels: created=%d, updated=%d, deleted=%d",
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventCreate }),
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }),
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventDelete }),
))
}
if len(event.Addresses) > 0 {
parts = append(parts, fmt.Sprintf(
"addresses: created=%d, updated=%d, deleted=%d",
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventCreate }),
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }),
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventDelete }),
))
}
return fmt.Sprintf("Event %s: %s", event.EventID, strings.Join(parts, ", "))
}
type RefreshFlag uint8
const (
RefreshMail RefreshFlag = 1 << iota // 1<<0 = 1
_ // 1<<1 = 2
_ // 1<<2 = 4
_ // 1<<3 = 8
_ // 1<<4 = 16
_ // 1<<5 = 32
_ // 1<<6 = 64
_ // 1<<7 = 128
RefreshAll RefreshFlag = 1<<iota - 1 // 1<<8 - 1 = 255
)
type EventAction int
const (
EventDelete EventAction = iota
EventCreate
EventUpdate
EventUpdateFlags
)
type EventItem struct {
ID string
Action EventAction
}
type MessageEvent struct {
EventItem
Message MessageMetadata
}
type LabelEvent struct {
EventItem
Label Label
}
type AddressEvent struct {
EventItem
Address Address
}