| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/bradenaw/juniper/xslices" |
| 8 |
) |
| 9 |
|
| 10 |
type Event struct { |
| 11 |
EventID string |
| 12 |
|
| 13 |
Refresh RefreshFlag |
| 14 |
|
| 15 |
User *User |
| 16 |
|
| 17 |
UserSettings *UserSettings |
| 18 |
|
| 19 |
MailSettings *MailSettings |
| 20 |
|
| 21 |
Messages []MessageEvent |
| 22 |
|
| 23 |
Labels []LabelEvent |
| 24 |
|
| 25 |
Addresses []AddressEvent |
| 26 |
|
| 27 |
UsedSpace *int |
| 28 |
} |
| 29 |
|
| 30 |
func (event Event) String() string { |
| 31 |
var parts []string |
| 32 |
|
| 33 |
if event.Refresh != 0 { |
| 34 |
parts = append(parts, fmt.Sprintf("refresh: %v", event.Refresh)) |
| 35 |
} |
| 36 |
|
| 37 |
if event.User != nil { |
| 38 |
parts = append(parts, "user: [modified]") |
| 39 |
} |
| 40 |
|
| 41 |
if event.MailSettings != nil { |
| 42 |
parts = append(parts, "mail-settings: [modified]") |
| 43 |
} |
| 44 |
|
| 45 |
if len(event.Messages) > 0 { |
| 46 |
parts = append(parts, fmt.Sprintf( |
| 47 |
"messages: created=%d, updated=%d, deleted=%d", |
| 48 |
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventCreate }), |
| 49 |
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }), |
| 50 |
xslices.CountFunc(event.Messages, func(e MessageEvent) bool { return e.Action == EventDelete }), |
| 51 |
)) |
| 52 |
} |
| 53 |
|
| 54 |
if len(event.Labels) > 0 { |
| 55 |
parts = append(parts, fmt.Sprintf( |
| 56 |
"labels: created=%d, updated=%d, deleted=%d", |
| 57 |
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventCreate }), |
| 58 |
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }), |
| 59 |
xslices.CountFunc(event.Labels, func(e LabelEvent) bool { return e.Action == EventDelete }), |
| 60 |
)) |
| 61 |
} |
| 62 |
|
| 63 |
if len(event.Addresses) > 0 { |
| 64 |
parts = append(parts, fmt.Sprintf( |
| 65 |
"addresses: created=%d, updated=%d, deleted=%d", |
| 66 |
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventCreate }), |
| 67 |
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventUpdate || e.Action == EventUpdateFlags }), |
| 68 |
xslices.CountFunc(event.Addresses, func(e AddressEvent) bool { return e.Action == EventDelete }), |
| 69 |
)) |
| 70 |
} |
| 71 |
|
| 72 |
return fmt.Sprintf("Event %s: %s", event.EventID, strings.Join(parts, ", ")) |
| 73 |
} |
| 74 |
|
| 75 |
type RefreshFlag uint8 |
| 76 |
|
| 77 |
const ( |
| 78 |
RefreshMail RefreshFlag = 1 << iota // 1<<0 = 1 |
| 79 |
_ // 1<<1 = 2 |
| 80 |
_ // 1<<2 = 4 |
| 81 |
_ // 1<<3 = 8 |
| 82 |
_ // 1<<4 = 16 |
| 83 |
_ // 1<<5 = 32 |
| 84 |
_ // 1<<6 = 64 |
| 85 |
_ // 1<<7 = 128 |
| 86 |
RefreshAll RefreshFlag = 1<<iota - 1 // 1<<8 - 1 = 255 |
| 87 |
) |
| 88 |
|
| 89 |
type EventAction int |
| 90 |
|
| 91 |
const ( |
| 92 |
EventDelete EventAction = iota |
| 93 |
EventCreate |
| 94 |
EventUpdate |
| 95 |
EventUpdateFlags |
| 96 |
) |
| 97 |
|
| 98 |
type EventItem struct { |
| 99 |
ID string |
| 100 |
Action EventAction |
| 101 |
} |
| 102 |
|
| 103 |
type MessageEvent struct { |
| 104 |
EventItem |
| 105 |
|
| 106 |
Message MessageMetadata |
| 107 |
} |
| 108 |
|
| 109 |
type LabelEvent struct { |
| 110 |
EventItem |
| 111 |
|
| 112 |
Label Label |
| 113 |
} |
| 114 |
|
| 115 |
type AddressEvent struct { |
| 116 |
EventItem |
| 117 |
|
| 118 |
Address Address |
| 119 |
} |