-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
context.go
280 lines (244 loc) · 8.09 KB
/
context.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
274
275
276
277
278
279
280
package globals
import (
"context"
"sync"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
type keyfinderKey int
type identifyNotifierKey int
type chatTrace int
type identifyModeKey int
type upakfinderKey int
type rateLimitKey int
type nameInfoOverride int
type localizerCancelableKeyTyp int
type messageSkipsKeyTyp int
type unboxModeKeyTyp int
type emojiHarvesterKeyTyp int
type ctxMutexKeyTyp int
var kfKey keyfinderKey
var inKey identifyNotifierKey
var chatTraceKey chatTrace
var identModeKey identifyModeKey
var upKey upakfinderKey
var rlKey rateLimitKey
var nameInfoOverrideKey nameInfoOverride
var localizerCancelableKey localizerCancelableKeyTyp
var messageSkipsKey messageSkipsKeyTyp
var unboxModeKey unboxModeKeyTyp
var emojiHarvesterKey emojiHarvesterKeyTyp
var ctxMutexKey ctxMutexKeyTyp
type identModeData struct {
mode keybase1.TLFIdentifyBehavior
breaks *[]keybase1.TLFIdentifyFailure
}
func CtxKeyFinder(ctx context.Context, g *Context) types.KeyFinder {
if kf, ok := ctx.Value(kfKey).(types.KeyFinder); ok {
return kf
}
return g.CtxFactory.NewKeyFinder()
}
func CtxUPAKFinder(ctx context.Context, g *Context) types.UPAKFinder {
if up, ok := ctx.Value(upKey).(types.UPAKFinder); ok {
return up
}
return g.CtxFactory.NewUPAKFinder()
}
func CtxIdentifyMode(ctx context.Context) (ib keybase1.TLFIdentifyBehavior, breaks *[]keybase1.TLFIdentifyFailure, ok bool) {
if imd, ok := ctx.Value(identModeKey).(identModeData); ok {
return imd.mode, imd.breaks, ok
}
return keybase1.TLFIdentifyBehavior_CHAT_CLI, nil, false
}
func CtxAddIdentifyMode(ctx context.Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure) context.Context {
if mode == keybase1.TLFIdentifyBehavior_UNSET {
mode = keybase1.TLFIdentifyBehavior_CHAT_CLI
}
return context.WithValue(ctx, identModeKey, identModeData{mode: mode, breaks: breaks})
}
func CtxIdentifyNotifier(ctx context.Context) types.IdentifyNotifier {
if in, ok := ctx.Value(inKey).(types.IdentifyNotifier); ok {
return in
}
return nil
}
func CtxModifyIdentifyNotifier(ctx context.Context, notifier types.IdentifyNotifier) context.Context {
return context.WithValue(ctx, inKey, notifier)
}
func CtxAddRateLimit(ctx context.Context, rl []chat1.RateLimit) {
if l, ok := ctx.Value(ctxMutexKey).(*sync.RWMutex); ok {
l.Lock()
defer l.Unlock()
if existingRL, ok := ctx.Value(rlKey).(map[string]chat1.RateLimit); ok {
for _, r := range rl {
existingRL[r.Name] = r
}
}
}
}
func CtxRateLimits(ctx context.Context) (res []chat1.RateLimit) {
if l, ok := ctx.Value(ctxMutexKey).(*sync.RWMutex); ok {
l.RLock()
defer l.RUnlock()
if existingRL, ok := ctx.Value(rlKey).(map[string]chat1.RateLimit); ok {
for _, rl := range existingRL {
res = append(res, rl)
}
}
}
return res
}
func CtxAddMessageCacheSkips(ctx context.Context, convID chat1.ConversationID, msgs []chat1.MessageUnboxed) {
if l, ok := ctx.Value(ctxMutexKey).(*sync.RWMutex); ok {
l.Lock()
defer l.Unlock()
if existingSkips, ok := ctx.Value(messageSkipsKey).(map[chat1.ConvIDStr]MessageCacheSkip); ok {
existingSkips[convID.ConvIDStr()] = MessageCacheSkip{
ConvID: convID,
Msgs: append(existingSkips[convID.ConvIDStr()].Msgs, msgs...),
}
}
}
}
type MessageCacheSkip struct {
ConvID chat1.ConversationID
Msgs []chat1.MessageUnboxed
}
func CtxMessageCacheSkips(ctx context.Context) (res []MessageCacheSkip) {
if l, ok := ctx.Value(ctxMutexKey).(*sync.RWMutex); ok {
l.RLock()
defer l.RUnlock()
if existingSkips, ok := ctx.Value(messageSkipsKey).(map[chat1.ConvIDStr]MessageCacheSkip); ok {
for _, skips := range existingSkips {
res = append(res, skips)
}
}
}
return res
}
func CtxModifyUnboxMode(ctx context.Context, unboxMode types.UnboxMode) context.Context {
return context.WithValue(ctx, unboxModeKey, unboxMode)
}
func CtxUnboxMode(ctx context.Context) types.UnboxMode {
if unboxMode, ok := ctx.Value(unboxModeKey).(types.UnboxMode); ok {
return unboxMode
}
return types.UnboxModeFull
}
func CtxOverrideNameInfoSource(ctx context.Context) (types.NameInfoSource, bool) {
if ni, ok := ctx.Value(nameInfoOverrideKey).(types.NameInfoSource); ok {
return ni, true
}
return nil, false
}
func CtxAddOverrideNameInfoSource(ctx context.Context, ni types.NameInfoSource) context.Context {
return context.WithValue(ctx, nameInfoOverrideKey, ni)
}
func CtxTrace(ctx context.Context) (string, bool) {
if trace, ok := ctx.Value(chatTraceKey).(string); ok {
return trace, true
}
return "", false
}
func CtxAddLogTags(ctx context.Context, g *Context) context.Context {
// Add trace context value
trace := libkb.RandStringB64(3)
ctx = context.WithValue(ctx, chatTraceKey, trace)
// Add log tags
ctx = libkb.WithLogTagWithValue(ctx, "chat-trace", trace)
rpcTags := make(map[string]interface{})
rpcTags["user-agent"] = libkb.UserAgent
rpcTags["platform"] = libkb.GetPlatformString()
rpcTags["apptype"] = g.GetAppType()
ctx = rpc.AddRpcTagsToContext(ctx, rpcTags)
return ctx
}
func IsLocalizerCancelableCtx(ctx context.Context) bool {
if bval, ok := ctx.Value(localizerCancelableKey).(bool); ok && bval {
return true
}
return false
}
func CtxAddLocalizerCancelable(ctx context.Context) context.Context {
return context.WithValue(ctx, localizerCancelableKey, true)
}
func CtxRemoveLocalizerCancelable(ctx context.Context) context.Context {
if IsLocalizerCancelableCtx(ctx) {
return context.WithValue(ctx, localizerCancelableKey, false)
}
return ctx
}
func IsEmojiHarvesterCtx(ctx context.Context) bool {
if bval, ok := ctx.Value(emojiHarvesterKey).(bool); ok && bval {
return true
}
return false
}
func CtxMakeEmojiHarvester(ctx context.Context) context.Context {
return context.WithValue(ctx, emojiHarvesterKey, true)
}
func ChatCtx(ctx context.Context, g *Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure, notifier types.IdentifyNotifier) context.Context {
if breaks == nil {
breaks = new([]keybase1.TLFIdentifyFailure)
}
res := ctx
if _, _, ok := CtxIdentifyMode(res); !ok {
res = CtxAddIdentifyMode(res, mode, breaks)
}
if _, ok := res.Value(kfKey).(types.KeyFinder); !ok {
res = context.WithValue(res, kfKey, g.CtxFactory.NewKeyFinder())
}
if _, ok := res.Value(inKey).(types.IdentifyNotifier); !ok {
res = context.WithValue(res, inKey, notifier)
}
if _, ok := res.Value(upKey).(types.UPAKFinder); !ok {
res = context.WithValue(res, upKey, g.CtxFactory.NewUPAKFinder())
}
if _, ok := res.Value(ctxMutexKey).(*sync.RWMutex); !ok {
res = context.WithValue(res, ctxMutexKey, &sync.RWMutex{})
}
if _, ok := res.Value(rlKey).(map[string]chat1.RateLimit); !ok {
res = context.WithValue(res, rlKey, make(map[string]chat1.RateLimit))
}
if _, ok := res.Value(messageSkipsKey).(map[chat1.ConvIDStr]MessageCacheSkip); !ok {
res = context.WithValue(res, messageSkipsKey, make(map[chat1.ConvIDStr]MessageCacheSkip))
}
if _, ok := res.Value(unboxModeKey).(types.UnboxMode); !ok {
res = context.WithValue(res, unboxModeKey, types.UnboxModeFull)
}
if _, ok := CtxTrace(res); !ok {
res = CtxAddLogTags(res, g)
}
return res
}
func BackgroundChatCtx(sourceCtx context.Context, g *Context) context.Context {
rctx := libkb.CopyTagsToBackground(sourceCtx)
in := CtxIdentifyNotifier(sourceCtx)
if ident, breaks, ok := CtxIdentifyMode(sourceCtx); ok {
rctx = ChatCtx(rctx, g, ident, breaks, in)
}
// Overwrite trace tag
if tr, ok := sourceCtx.Value(chatTraceKey).(string); ok {
rctx = context.WithValue(rctx, chatTraceKey, tr)
}
if ni, ok := CtxOverrideNameInfoSource(sourceCtx); ok {
rctx = CtxAddOverrideNameInfoSource(rctx, ni)
}
rctx = context.WithValue(rctx, kfKey, CtxKeyFinder(sourceCtx, g))
rctx = context.WithValue(rctx, upKey, CtxUPAKFinder(sourceCtx, g))
rctx = context.WithValue(rctx, inKey, in)
rctx = libkb.WithLogTag(rctx, "CHTBKG")
if IsLocalizerCancelableCtx(sourceCtx) {
rctx = CtxAddLocalizerCancelable(rctx)
}
if IsEmojiHarvesterCtx(sourceCtx) {
rctx = CtxMakeEmojiHarvester(rctx)
}
return rctx
}