forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
273 lines (240 loc) · 7.48 KB
/
factory.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package gregor1
import (
"bytes"
"errors"
"sort"
"time"
"github.com/keybase/client/go/gregor"
"github.com/keybase/go-codec/codec"
)
type ObjFactory struct{}
func (o ObjFactory) MakeUID(b []byte) (gregor.UID, error) { return UID(b), nil }
func (o ObjFactory) MakeMsgID(b []byte) (gregor.MsgID, error) { return MsgID(b), nil }
func (o ObjFactory) MakeDeviceID(b []byte) (gregor.DeviceID, error) { return DeviceID(b), nil }
func (o ObjFactory) MakeBody(b []byte) (gregor.Body, error) { return Body(b), nil }
func (o ObjFactory) MakeSystem(s string) (gregor.System, error) { return System(s), nil }
func (o ObjFactory) MakeCategory(s string) (gregor.Category, error) { return Category(s), nil }
func castUID(uid gregor.UID) (ret UID, err error) {
if uid == nil {
return
}
ret, ok := uid.(UID)
if !ok {
err = errors.New("bad UID; wrong type")
}
return
}
func castDeviceID(d gregor.DeviceID) (ret DeviceID, err error) {
if d == nil {
return
}
ret, ok := d.(DeviceID)
if !ok {
err = errors.New("bad Device ID; wrong type")
}
return
}
func castItem(i gregor.Item) (ret ItemAndMetadata, err error) {
ret, ok := i.(ItemAndMetadata)
if !ok {
err = errors.New("bad Item; wrong type")
}
return
}
func castInBandMessage(i gregor.InBandMessage) (ret InBandMessage, err error) {
ret, ok := i.(InBandMessage)
if !ok {
err = errors.New("bad InBandMessage; wrong type")
}
return
}
func timeToTimeOrOffset(timeIn *time.Time) (too TimeOrOffset) {
if timeIn != nil {
too.Time_ = ToTime(*timeIn)
}
return
}
func (o ObjFactory) makeMetadata(uid gregor.UID, msgid gregor.MsgID, devid gregor.DeviceID, ctime time.Time, i gregor.InBandMsgType) (Metadata, error) {
uid2, e := castUID(uid)
if e != nil {
return Metadata{}, e
}
devid2, e := castDeviceID(devid)
if e != nil {
return Metadata{}, e
}
return Metadata{
Uid_: uid2,
MsgID_: MsgID(msgid.Bytes()),
Ctime_: ToTime(ctime),
DeviceID_: devid2,
InBandMsgType_: int(i),
}, nil
}
func (o ObjFactory) makeItem(c gregor.Category, d *time.Time, b gregor.Body) *Item {
return &Item{
Dtime_: timeToTimeOrOffset(d),
Category_: Category(c.String()),
Body_: Body(b.Bytes()),
}
}
func (o ObjFactory) MakeItem(u gregor.UID, msgid gregor.MsgID, deviceid gregor.DeviceID, ctime time.Time, c gregor.Category, dtime *time.Time, body gregor.Body) (gregor.Item, error) {
md, err := o.makeMetadata(u, msgid, deviceid, ctime, gregor.InBandMsgTypeUpdate)
if err != nil {
return nil, err
}
return ItemAndMetadata{
Md_: &md,
Item_: o.makeItem(c, dtime, body),
}, nil
}
func (o ObjFactory) MakeReminder(i gregor.Item, seqno int, t time.Time) (gregor.Reminder, error) {
it, ok := i.(ItemAndMetadata)
if !ok {
return nil, errors.New("item is not gregor1.ItemAndMetadata")
}
return Reminder{
Item_: it,
Seqno_: seqno,
RemindTime_: ToTime(t),
}, nil
}
func (o ObjFactory) MakeDismissalByRange(uid gregor.UID, msgid gregor.MsgID, devid gregor.DeviceID, ctime time.Time, c gregor.Category, d time.Time) (gregor.InBandMessage, error) {
md, err := o.makeMetadata(uid, msgid, devid, ctime, gregor.InBandMsgTypeUpdate)
if err != nil {
return nil, err
}
return InBandMessage{
StateUpdate_: &StateUpdateMessage{
Md_: md,
Dismissal_: &Dismissal{
Ranges_: []MsgRange{{
EndTime_: timeToTimeOrOffset(&d),
Category_: Category(c.String()),
}},
},
},
}, nil
}
func (o ObjFactory) MakeDismissalByIDs(uid gregor.UID, msgid gregor.MsgID, devid gregor.DeviceID, ctime time.Time, ids []gregor.MsgID) (gregor.InBandMessage, error) {
md, err := o.makeMetadata(uid, msgid, devid, ctime, gregor.InBandMsgTypeUpdate)
if err != nil {
return nil, err
}
ourIds := make([]MsgID, len(ids), len(ids))
for i, id := range ids {
ourIds[i] = MsgID(id.Bytes())
}
return InBandMessage{
StateUpdate_: &StateUpdateMessage{
Md_: md,
Dismissal_: &Dismissal{
MsgIDs_: ourIds,
},
},
}, nil
}
func (o ObjFactory) MakeStateSyncMessage(uid gregor.UID, msgid gregor.MsgID, devid gregor.DeviceID, ctime time.Time) (gregor.InBandMessage, error) {
md, err := o.makeMetadata(uid, msgid, devid, ctime, gregor.InBandMsgTypeUpdate)
if err != nil {
return nil, err
}
return InBandMessage{
StateSync_: &StateSyncMessage{
Md_: md,
},
}, nil
}
type itemSlice []ItemAndMetadata
func (its itemSlice) Len() int { return len(its) }
func (its itemSlice) Swap(i, j int) { its[i], its[j] = its[j], its[i] }
// Less returns true if i's ctime is before j's, or if they're equal and
// i's MsgID is lexicographically before j's.
func (its itemSlice) Less(i, j int) bool {
mI, mJ := its[i].Metadata(), its[j].Metadata()
if mI != nil && mJ != nil {
if mI.CTime().Equal(mJ.CTime()) {
if mI.MsgID() != nil && mJ.MsgID() != nil {
return bytes.Compare(mI.MsgID().Bytes(), mJ.MsgID().Bytes()) < 0
} else {
return mI.MsgID() == nil
}
}
return mI.CTime().Before(mJ.CTime())
}
return mI == nil
}
func (o ObjFactory) MakeState(items []gregor.Item) (gregor.State, error) {
var ourItems itemSlice
for _, item := range items {
ourItem, err := castItem(item)
if err != nil {
return nil, err
}
ourItems = append(ourItems, ourItem)
}
sort.Sort(ourItems)
return State{Items_: ourItems}, nil
}
func (o ObjFactory) MakeStateWithLookupTable(items []gregor.Item, table map[string]gregor.Item) (gregor.State, error) {
return o.MakeState(items)
}
func (o ObjFactory) MakeMetadata(uid gregor.UID, msgid gregor.MsgID, devid gregor.DeviceID, ctime time.Time, i gregor.InBandMsgType) (gregor.Metadata, error) {
return o.makeMetadata(uid, msgid, devid, ctime, gregor.InBandMsgTypeUpdate)
}
func (o ObjFactory) MakeInBandMessageFromItem(i gregor.Item) (gregor.InBandMessage, error) {
ourItem, err := castItem(i)
if err != nil {
return nil, err
}
return InBandMessage{
StateUpdate_: &StateUpdateMessage{
Md_: *ourItem.Md_,
Creation_: ourItem.Item_,
},
}, nil
}
func (o ObjFactory) MakeMessageFromInBandMessage(i gregor.InBandMessage) (gregor.Message, error) {
ourInBandMessage, err := castInBandMessage(i)
if err != nil {
return nil, err
}
return Message{
Ibm_: &ourInBandMessage,
}, nil
}
func (o ObjFactory) UnmarshalState(b []byte) (gregor.State, error) {
var state State
err := codec.NewDecoderBytes(b, &codec.MsgpackHandle{WriteExt: true}).
Decode(&state)
if err != nil {
return nil, err
}
return state, nil
}
func (o ObjFactory) MakeTimeOrOffsetFromTime(t time.Time) (gregor.TimeOrOffset, error) {
return timeToTimeOrOffset(&t), nil
}
func (o ObjFactory) MakeTimeOrOffsetFromOffset(d time.Duration) (gregor.TimeOrOffset, error) {
return TimeOrOffset{Offset_: DurationMsec(d / time.Millisecond)}, nil
}
func (o ObjFactory) MakeReminderID(u gregor.UID, msgid gregor.MsgID, seqno int) (gregor.ReminderID, error) {
return ReminderID{Uid_: u.Bytes(), MsgID_: msgid.Bytes(), Seqno_: seqno}, nil
}
func (o ObjFactory) MakeReminderSetFromReminders(reminders []gregor.Reminder, moreRemindersReady bool) (gregor.ReminderSet, error) {
ret := ReminderSet{MoreRemindersReady_: moreRemindersReady}
for _, reminder := range reminders {
if r, ok := reminder.(Reminder); ok {
ret.Reminders_ = append(ret.Reminders_, r)
} else {
return nil, errors.New("Can't upcast reminder")
}
}
return ret, nil
}
func (o ObjFactory) MakeOutOfBandMessage(uid gregor.UID, system gregor.System, body gregor.Body) gregor.Message {
return Message{
Oobm_: &OutOfBandMessage{Uid_: uid.Bytes(), System_: System(system.String()), Body_: body.Bytes()},
}
}
var _ gregor.ObjFactory = ObjFactory{}