/
chat_api_handler.go
475 lines (400 loc) · 12.4 KB
/
chat_api_handler.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"github.com/keybase/client/go/protocol/chat1"
"golang.org/x/net/context"
)
const (
methodList = "list"
methodRead = "read"
methodSend = "send"
methodEdit = "edit"
methodDelete = "delete"
methodAttach = "attach"
methodDownload = "download"
methodSetStatus = "setstatus"
methodMark = "mark"
)
// ErrInvalidOptions is returned when the options aren't valid.
type ErrInvalidOptions struct {
method string
version int
err error
}
func (e ErrInvalidOptions) Error() string {
return fmt.Sprintf("invalid %s v%d options: %s", e.method, e.version, e.err)
}
// Call represents a JSON chat call.
type Call struct {
Jsonrpc string
ID int
Method string
Params Params
}
// Params represents the `params` portion of the JSON chat call.
type Params struct {
Version int
Options json.RawMessage
}
// CallError is the result when there is an error.
type CallError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
type RateLimit struct {
Tank string `json:"tank"`
Capacity int `json:"capacity"`
Reset int `json:"reset"`
Gas int `json:"gas"`
}
type RateLimits struct {
RateLimits []RateLimit `json:"ratelimits,omitempty"`
}
// Reply is returned with the results of procressing a Call.
type Reply struct {
Jsonrpc string `json:"jsonrpc,omitempty"`
ID int `json:"id,omitempty"`
Error *CallError `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
}
// ChatAPIHandler can handle all of the chat json api methods.
type ChatAPIHandler interface {
ListV1(context.Context, Call, io.Writer) error
ReadV1(context.Context, Call, io.Writer) error
SendV1(context.Context, Call, io.Writer) error
EditV1(context.Context, Call, io.Writer) error
DeleteV1(context.Context, Call, io.Writer) error
AttachV1(context.Context, Call, io.Writer) error
DownloadV1(context.Context, Call, io.Writer) error
SetStatusV1(context.Context, Call, io.Writer) error
MarkV1(context.Context, Call, io.Writer) error
}
// ChatAPI implements ChatAPIHandler and contains a ChatServiceHandler
// to do all the work.
type ChatAPI struct {
svcHandler ChatServiceHandler
indent bool
}
// ChatChannel represents a channel through which chat happens.
type ChatChannel struct {
Name string `json:"name"`
Public bool `json:"public"`
MembersType string `json:"members_type"`
TopicType string `json:"topic_type,omitempty"`
TopicName string `json:"topic_name,omitempty"`
}
// Valid returns true if the ChatChannel has at least a Name.
func (c ChatChannel) Valid() bool {
if len(c.Name) == 0 {
return false
}
validTyp := false
if len(c.MembersType) > 0 {
for typ := range chat1.ConversationMembersTypeMap {
if strings.ToLower(typ) == c.MembersType {
validTyp = true
break
}
}
} else {
validTyp = true
}
return validTyp
}
func (c ChatChannel) GetMembersType() chat1.ConversationMembersType {
if typ, ok := chat1.ConversationMembersTypeMap[strings.ToUpper(c.MembersType)]; ok {
return typ
}
return chat1.ConversationMembersType_KBFS
}
// ChatMessage represents a text message to be sent.
type ChatMessage struct {
Body string
}
// Valid returns true if the message has a body.
func (c ChatMessage) Valid() bool {
return len(c.Body) > 0
}
type listOptionsV1 struct {
UnreadOnly bool `json:"unread_only,omitempty"`
TopicType string `json:"topic_type,omitempty"`
ShowErrors bool `json:"show_errors,omitempty"`
FailOffline bool `json:"fail_offline,omitempty"`
}
func (l listOptionsV1) Check() error {
_, err := TopicTypeFromStrDefault(l.TopicType)
if err != nil {
return ErrInvalidOptions{version: 1, method: methodList, err: err}
}
return nil
}
type sendOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
Message ChatMessage
Nonblock bool `json:"nonblock"`
MembersType string `json:"members_type"`
}
func (s sendOptionsV1) Check() error {
if err := checkChannelConv(methodSend, s.Channel, s.ConversationID); err != nil {
return err
}
if !s.Message.Valid() {
return ErrInvalidOptions{version: 1, method: methodSend, err: errors.New("invalid message")}
}
return nil
}
type readOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
Pagination *chat1.Pagination `json:"pagination,omitempty"`
Peek bool
UnreadOnly bool `json:"unread_only"`
FailOffline bool `json:"fail_offline"`
}
func (r readOptionsV1) Check() error {
return checkChannelConv(methodRead, r.Channel, r.ConversationID)
}
type editOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
MessageID chat1.MessageID `json:"message_id"`
Message ChatMessage
}
func (e editOptionsV1) Check() error {
if err := checkChannelConv(methodEdit, e.Channel, e.ConversationID); err != nil {
return err
}
if e.MessageID == 0 {
return ErrInvalidOptions{version: 1, method: methodEdit, err: errors.New("invalid message id")}
}
if !e.Message.Valid() {
return ErrInvalidOptions{version: 1, method: methodEdit, err: errors.New("invalid message")}
}
return nil
}
type deleteOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
MessageID chat1.MessageID `json:"message_id"`
}
func (d deleteOptionsV1) Check() error {
if err := checkChannelConv(methodDelete, d.Channel, d.ConversationID); err != nil {
return err
}
if d.MessageID == 0 {
return ErrInvalidOptions{version: 1, method: methodDelete, err: errors.New("invalid message id")}
}
return nil
}
type attachOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
Filename string
Preview string
Title string
NoStream bool
}
func (a attachOptionsV1) Check() error {
if err := checkChannelConv(methodAttach, a.Channel, a.ConversationID); err != nil {
return err
}
if len(strings.TrimSpace(a.Filename)) == 0 {
return ErrInvalidOptions{version: 1, method: methodAttach, err: errors.New("empty filename")}
}
return nil
}
type downloadOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
MessageID chat1.MessageID `json:"message_id"`
Output string
Preview bool
NoStream bool
}
func (a downloadOptionsV1) Check() error {
if err := checkChannelConv(methodDownload, a.Channel, a.ConversationID); err != nil {
return err
}
if a.MessageID == 0 {
return ErrInvalidOptions{version: 1, method: methodDownload, err: errors.New("invalid message id")}
}
if len(strings.TrimSpace(a.Output)) == 0 {
return ErrInvalidOptions{version: 1, method: methodDownload, err: errors.New("empty output filename")}
}
return nil
}
type setStatusOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
Status string `json:"status"`
}
func (o setStatusOptionsV1) Check() error {
if err := checkChannelConv(methodSetStatus, o.Channel, o.ConversationID); err != nil {
return err
}
if _, ok := chat1.ConversationStatusMap[strings.ToUpper(o.Status)]; !ok {
return ErrInvalidOptions{version: 1, method: methodSetStatus, err: fmt.Errorf("unsupported status: '%v'", o.Status)}
}
return nil
}
type markOptionsV1 struct {
Channel ChatChannel
ConversationID string `json:"conversation_id"`
MessageID chat1.MessageID `json:"message_id"`
}
func (o markOptionsV1) Check() error {
return checkChannelConv(methodMark, o.Channel, o.ConversationID)
}
func (a *ChatAPI) ListV1(ctx context.Context, c Call, w io.Writer) error {
var opts listOptionsV1
// Options are optional for list
if len(c.Params.Options) != 0 {
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for list v1
return a.encodeReply(c, a.svcHandler.ListV1(ctx, opts), w)
}
func (a *ChatAPI) ReadV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodRead, err: errors.New("empty options")}
}
var opts readOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for read v1
return a.encodeReply(c, a.svcHandler.ReadV1(ctx, opts), w)
}
func (a *ChatAPI) SendV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodSend, err: errors.New("empty options")}
}
var opts sendOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for send v1
return a.encodeReply(c, a.svcHandler.SendV1(ctx, opts), w)
}
func (a *ChatAPI) EditV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodEdit, err: errors.New("empty options")}
}
var opts editOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for edit v1
return a.encodeReply(c, a.svcHandler.EditV1(ctx, opts), w)
}
func (a *ChatAPI) DeleteV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodDelete, err: errors.New("empty options")}
}
var opts deleteOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for delete v1
return a.encodeReply(c, a.svcHandler.DeleteV1(ctx, opts), w)
}
func (a *ChatAPI) AttachV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodAttach, err: errors.New("empty options")}
}
var opts attachOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for attach v1
return a.encodeReply(c, a.svcHandler.AttachV1(ctx, opts), w)
}
func (a *ChatAPI) DownloadV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodDownload, err: errors.New("empty options")}
}
var opts downloadOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for download v1
return a.encodeReply(c, a.svcHandler.DownloadV1(ctx, opts), w)
}
func (a *ChatAPI) SetStatusV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodSetStatus, err: errors.New("empty options")}
}
var opts setStatusOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for setstatus v1
return a.encodeReply(c, a.svcHandler.SetStatusV1(ctx, opts), w)
}
func (a *ChatAPI) MarkV1(ctx context.Context, c Call, w io.Writer) error {
if len(c.Params.Options) == 0 {
return ErrInvalidOptions{version: 1, method: methodMark, err: errors.New("empty options")}
}
var opts markOptionsV1
if err := json.Unmarshal(c.Params.Options, &opts); err != nil {
return err
}
if err := opts.Check(); err != nil {
return err
}
// opts are valid for mark v1
return a.encodeReply(c, a.svcHandler.MarkV1(ctx, opts), w)
}
func (a *ChatAPI) encodeReply(call Call, reply Reply, w io.Writer) error {
// copy jsonrpc fields from call to reply
reply.Jsonrpc = call.Jsonrpc
reply.ID = call.ID
enc := json.NewEncoder(w)
if a.indent {
enc.SetIndent("", " ")
}
return enc.Encode(reply)
}
func checkChannelConv(method string, channel ChatChannel, convID string) error {
if !channel.Valid() && len(convID) == 0 {
return ErrInvalidOptions{version: 1, method: method, err: errors.New("need channel or conversation_id")}
}
if channel.Valid() && len(convID) > 0 {
return ErrInvalidOptions{version: 1, method: method, err: errors.New("include channel or conversation_id, not both")}
}
return nil
}