Debian Codegraph @

server/backend/updates.go

raw
1 package backend
2
3 import (
4 "github.com/bradenaw/juniper/xslices"
5 "github.com/henrybear327/go-proton-api"
6 )
7
8 func merge(updates []update) []update {
9 if len(updates) < 2 {
10 return updates
11 }
12
13 if merged := merge(updates[1:]); xslices.IndexFunc(merged, func(other update) bool {
14 return other.replaces(updates[0])
15 }) < 0 {
16 return append([]update{updates[0]}, merged...)
17 } else {
18 return merged
19 }
20 }
21
22 type update interface {
23 replaces(other update) bool
24
25 _isUpdate()
26 }
27
28 type baseUpdate struct{}
29
30 func (baseUpdate) replaces(update) bool {
31 return false
32 }
33
34 func (baseUpdate) _isUpdate() {}
35
36 type userRefreshed struct {
37 baseUpdate
38
39 refresh proton.RefreshFlag
40 }
41
42 type messageCreated struct {
43 baseUpdate
44 messageID string
45 }
46
47 type messageUpdated struct {
48 baseUpdate
49 messageID string
50 }
51
52 func (update *messageUpdated) replaces(other update) bool {
53 switch other := other.(type) {
54 case *messageUpdated:
55 return update.messageID == other.messageID
56
57 default:
58 return false
59 }
60 }
61
62 type messageDeleted struct {
63 baseUpdate
64 messageID string
65 }
66
67 func (update *messageDeleted) replaces(other update) bool {
68 switch other := other.(type) {
69 case *messageCreated:
70 return update.messageID == other.messageID
71
72 case *messageUpdated:
73 return update.messageID == other.messageID
74
75 case *messageDeleted:
76 if update.messageID != other.messageID {
77 return false
78 }
79
80 panic("message deleted twice")
81
82 default:
83 return false
84 }
85 }
86
87 type labelCreated struct {
88 baseUpdate
89 labelID string
90 }
91
92 type labelUpdated struct {
93 baseUpdate
94 labelID string
95 }
96
97 func (update *labelUpdated) replaces(other update) bool {
98 switch other := other.(type) {
99 case *labelUpdated:
100 return update.labelID == other.labelID
101
102 default:
103 return false
104 }
105 }
106
107 type labelDeleted struct {
108 baseUpdate
109 labelID string
110 }
111
112 func (update *labelDeleted) replaces(other update) bool {
113 switch other := other.(type) {
114 case *labelCreated:
115 return update.labelID == other.labelID
116
117 case *labelUpdated:
118 return update.labelID == other.labelID
119
120 case *labelDeleted:
121 if update.labelID != other.labelID {
122 return false
123 }
124
125 panic("label deleted twice")
126
127 default:
128 return false
129 }
130 }
131
132 type addressCreated struct {
133 baseUpdate
134 addressID string
135 }
136
137 type addressUpdated struct {
138 baseUpdate
139 addressID string
140 }
141
142 func (update *addressUpdated) replaces(other update) bool {
143 switch other := other.(type) {
144 case *addressUpdated:
145 return update.addressID == other.addressID
146
147 default:
148 return false
149 }
150 }
151
152 type addressDeleted struct {
153 baseUpdate
154 addressID string
155 }
156
157 func (update *addressDeleted) replaces(other update) bool {
158 switch other := other.(type) {
159 case *addressCreated:
160 return update.addressID == other.addressID
161
162 case *addressUpdated:
163 return update.addressID == other.addressID
164
165 case *addressDeleted:
166 if update.addressID != other.addressID {
167 return false
168 }
169
170 panic("address deleted twice")
171
172 default:
173 return false
174 }
175 }
176
177 type userSettingsUpdate struct {
178 baseUpdate
179 settings proton.UserSettings
180 }
181
182 func (update *userSettingsUpdate) replaces(other update) bool {
183 switch other.(type) {
184 case *userSettingsUpdate:
185 return true
186 default:
187 return false
188 }
189 }