Skip to content

Commit befba64

Browse files
authored
typing notification support CORE-5074 (keybase#6956)
* protocol * local rpcs * hook up notification * separate remote an local update * rework * testing * despam * fix tests
1 parent c873c47 commit befba64

28 files changed

Lines changed: 826 additions & 2 deletions

‎go/chat/convsource_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func (f failingRemote) SyncAll(ctx context.Context, arg chat1.SyncAllArg) (chat1
233233
return chat1.SyncAllResult{}, nil
234234
}
235235

236+
func (f failingRemote) UpdateTypingRemote(ctx context.Context, arg chat1.UpdateTypingRemoteArg) error {
237+
require.Fail(f.t, "UpdateTypingRemote")
238+
return nil
239+
}
240+
236241
type failingTlf struct {
237242
t *testing.T
238243
}

‎go/chat/push.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ type PushHandler struct {
180180
badger *badges.Badger
181181
identNotifier *IdentifyNotifier
182182
orderer *gregorMessageOrderer
183+
typingMonitor *TypingMonitor
183184
}
184185

185186
func NewPushHandler(g *globals.Context) *PushHandler {
@@ -188,6 +189,7 @@ func NewPushHandler(g *globals.Context) *PushHandler {
188189
DebugLabeler: utils.NewDebugLabeler(g, "PushHandler", false),
189190
identNotifier: NewIdentifyNotifier(g),
190191
orderer: newGregorMessageOrderer(g),
192+
typingMonitor: NewTypingMonitor(g),
191193
}
192194
}
193195

@@ -369,6 +371,12 @@ func (g *PushHandler) Activity(ctx context.Context, m gregor.OutOfBandMessage) (
369371
g.Debug(ctx, "chat activity: newMessage: outboxID is empty")
370372
}
371373

374+
// Update typing status to stopped
375+
g.typingMonitor.Update(ctx, chat1.TyperInfo{
376+
Uid: keybase1.UID(nm.Message.ClientHeader.Sender.String()),
377+
DeviceID: keybase1.DeviceID(nm.Message.ClientHeader.SenderDevice.String()),
378+
}, convID, false)
379+
372380
var conv *chat1.ConversationLocal
373381
decmsg, appended, pushErr := g.G().ConvSource.Push(ctx, nm.ConvID, gregor1.UID(uid), nm.Message)
374382
if pushErr != nil {
@@ -525,3 +533,40 @@ func (g *PushHandler) notifyNewChatActivity(ctx context.Context, uid gregor.UID,
525533

526534
return nil
527535
}
536+
537+
func (g *PushHandler) Typing(ctx context.Context, m gregor.OutOfBandMessage) (err error) {
538+
var identBreaks []keybase1.TLFIdentifyFailure
539+
ctx = Context(ctx, g.G().GetEnv(), keybase1.TLFIdentifyBehavior_CHAT_GUI, &identBreaks,
540+
g.identNotifier)
541+
defer g.Trace(ctx, func() error { return err }, "Typing")()
542+
if m.Body() == nil {
543+
return errors.New("gregor handler for typing: nil message body")
544+
}
545+
546+
var update chat1.RemoteUserTypingUpdate
547+
reader := bytes.NewReader(m.Body().Bytes())
548+
dec := codec.NewDecoder(reader, &codec.MsgpackHandle{WriteExt: true})
549+
err = dec.Decode(&update)
550+
if err != nil {
551+
return err
552+
}
553+
554+
// Lookup username and device name
555+
kuid := keybase1.UID(update.Uid.String())
556+
kdid := keybase1.DeviceID(update.DeviceID.String())
557+
user, device, dtype, err := g.G().GetUPAKLoader().LookupUsernameAndDevice(ctx, kuid, kdid)
558+
if err != nil {
559+
g.Debug(ctx, "Typing: failed to lookup username/device: msg: %s", err.Error())
560+
return err
561+
}
562+
563+
// Fire off update with all relevant info
564+
g.typingMonitor.Update(ctx, chat1.TyperInfo{
565+
Uid: kuid,
566+
DeviceID: kdid,
567+
Username: user.String(),
568+
DeviceName: device,
569+
DeviceType: dtype,
570+
}, update.ConvID, update.Typing)
571+
return nil
572+
}

‎go/chat/push_test.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/keybase/client/go/chat/storage"
9+
"github.com/keybase/client/go/gregor"
910
"github.com/keybase/client/go/kbtest"
1011
"github.com/keybase/client/go/protocol/chat1"
1112
"github.com/keybase/client/go/protocol/gregor1"
@@ -172,3 +173,100 @@ func TestPushAppState(t *testing.T) {
172173
require.Fail(t, "no message received")
173174
}
174175
}
176+
177+
func makeTypingNotification(t *testing.T, uid gregor1.UID, convID chat1.ConversationID, typing bool) gregor.OutOfBandMessage {
178+
179+
nm := chat1.RemoteUserTypingUpdate{
180+
Uid: uid,
181+
ConvID: convID,
182+
Typing: typing,
183+
}
184+
var data []byte
185+
enc := codec.NewEncoderBytes(&data, &codec.MsgpackHandle{WriteExt: true})
186+
require.NoError(t, enc.Encode(nm))
187+
m := gregor1.OutOfBandMessage{
188+
Uid_: uid,
189+
System_: "chat.typing",
190+
Body_: data,
191+
}
192+
return m
193+
}
194+
195+
func TestPushTyping(t *testing.T) {
196+
world, ri2, _, sender, list, tlf := setupTest(t, 1)
197+
defer world.Cleanup()
198+
199+
ri := ri2.(*kbtest.ChatRemoteMock)
200+
u := world.GetUsers()[0]
201+
uid := u.User.GetUID().ToBytes()
202+
tc := world.Tcs[u.Username]
203+
handler := NewPushHandler(tc.Context())
204+
handler.SetClock(world.Fc)
205+
handler.typingMonitor.SetClock(world.Fc)
206+
handler.typingMonitor.SetTimeout(time.Minute)
207+
208+
conv := newBlankConv(t, uid, ri, sender, tlf, u.Username)
209+
210+
confirmTyping := func(list *chatListener) {
211+
select {
212+
case updates := <-list.typingUpdate:
213+
require.Equal(t, 1, len(updates))
214+
require.Equal(t, conv.GetConvID(), updates[0].ConvID)
215+
require.Equal(t, 1, len(updates[0].Typers))
216+
require.Equal(t, uid, updates[0].Typers[0].Uid.ToBytes())
217+
case <-time.After(20 * time.Second):
218+
require.Fail(t, "no typing notification")
219+
}
220+
}
221+
222+
confirmNotTyping := func(list *chatListener) {
223+
select {
224+
case updates := <-list.typingUpdate:
225+
require.Equal(t, 1, len(updates))
226+
require.Equal(t, conv.GetConvID(), updates[0].ConvID)
227+
require.Zero(t, len(updates[0].Typers))
228+
case <-time.After(2 * time.Second):
229+
require.Fail(t, "no typing notification")
230+
}
231+
}
232+
233+
t.Logf("test basic")
234+
handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true))
235+
confirmTyping(list)
236+
handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), false))
237+
confirmNotTyping(list)
238+
239+
t.Logf("test expiration")
240+
handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true))
241+
confirmTyping(list)
242+
world.Fc.Advance(time.Hour)
243+
confirmNotTyping(list)
244+
245+
t.Logf("test extend")
246+
extendCh := make(chan struct{})
247+
handler.typingMonitor.extendCh = &extendCh
248+
handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true))
249+
confirmTyping(list)
250+
world.Fc.Advance(30 * time.Second)
251+
handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true))
252+
select {
253+
case <-list.typingUpdate:
254+
require.Fail(t, "should have extended")
255+
default:
256+
}
257+
select {
258+
case <-extendCh:
259+
case <-time.After(20 * time.Second):
260+
require.Fail(t, "no extend callback")
261+
}
262+
world.Fc.Advance(40 * time.Second)
263+
select {
264+
case <-list.typingUpdate:
265+
require.Fail(t, "not far enough")
266+
default:
267+
}
268+
world.Fc.Advance(40 * time.Second)
269+
confirmNotTyping(list)
270+
271+
require.Zero(t, len(handler.typingMonitor.typers))
272+
}

‎go/chat/sender_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type chatListener struct {
2929
inboxStale chan struct{}
3030
threadsStale chan []chat1.ConversationID
3131
bgConvLoads chan chat1.ConversationID
32+
typingUpdate chan []chat1.ConvTypingUpdate
3233
}
3334

3435
var _ libkb.NotifyListener = (*chatListener)(nil)
@@ -72,6 +73,14 @@ func (n *chatListener) ChatThreadsStale(uid keybase1.UID, cids []chat1.Conversat
7273
}
7374
}
7475

76+
func (n *chatListener) ChatTypingUpdate(updates []chat1.ConvTypingUpdate) {
77+
select {
78+
case n.typingUpdate <- updates:
79+
case <-time.After(5 * time.Second):
80+
panic("timeout on typing update")
81+
}
82+
}
83+
7584
func (n *chatListener) NewChatActivity(uid keybase1.UID, activity chat1.ChatActivity) {
7685
n.Lock()
7786
defer n.Unlock()
@@ -143,6 +152,7 @@ func setupTest(t *testing.T, numUsers int) (*kbtest.ChatMockWorld, chat1.RemoteI
143152
inboxStale: make(chan struct{}, 1),
144153
threadsStale: make(chan []chat1.ConversationID, 1),
145154
bgConvLoads: make(chan chat1.ConversationID, 10),
155+
typingUpdate: make(chan []chat1.ConvTypingUpdate, 10),
146156
}
147157
g.ConvSource = NewHybridConversationSource(g, boxer, storage.New(g), getRI)
148158
g.InboxSource = NewHybridInboxSource(g, getRI, tlf)

‎go/chat/server.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,3 +1972,33 @@ func (h *Server) FindConversationsLocal(ctx context.Context,
19721972
res.RateLimits = utils.AggRateLimits(res.RateLimits)
19731973
return res, nil
19741974
}
1975+
1976+
func (h *Server) UpdateTyping(ctx context.Context, arg chat1.UpdateTypingArg) (err error) {
1977+
var identBreaks []keybase1.TLFIdentifyFailure
1978+
ctx = Context(ctx, h.G().GetEnv(), keybase1.TLFIdentifyBehavior_CHAT_GUI,
1979+
&identBreaks, h.identNotifier)
1980+
defer h.Trace(ctx, func() error { return err }, fmt.Sprintf("StartTyping(%s)", arg.ConversationID))()
1981+
if err = h.assertLoggedIn(ctx); err != nil {
1982+
return err
1983+
}
1984+
uid := h.G().Env.GetUID()
1985+
deviceID := make([]byte, libkb.DeviceIDLen)
1986+
if err := h.G().Env.GetDeviceID().ToBytes(deviceID); err != nil {
1987+
return err
1988+
}
1989+
1990+
// Just bail out if we are offline
1991+
if !h.G().Syncer.IsConnected(ctx) {
1992+
return nil
1993+
}
1994+
if err := h.remoteClient().UpdateTypingRemote(ctx, chat1.UpdateTypingRemoteArg{
1995+
Uid: uid.ToBytes(),
1996+
DeviceID: deviceID,
1997+
ConvID: arg.ConversationID,
1998+
Typing: arg.Typing,
1999+
}); err != nil {
2000+
h.Debug(ctx, "StartTyping: failed to hit the server: %s", err.Error())
2001+
}
2002+
2003+
return nil
2004+
}

‎go/chat/server_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ func (n *serverChatListener) NewChatActivity(uid keybase1.UID, activity chat1.Ch
939939
n.newMessage <- activity.IncomingMessage().Message
940940
}
941941
}
942+
func (n *serverChatListener) ChatTypingUpdate(updates []chat1.ConvTypingUpdate) {
943+
}
942944

943945
func newServerChatListener() *serverChatListener {
944946
return &serverChatListener{

‎go/chat/types/interfaces.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type PushHandler interface {
122122
TlfFinalize(context.Context, gregor.OutOfBandMessage) error
123123
TlfResolve(context.Context, gregor.OutOfBandMessage) error
124124
Activity(context.Context, gregor.OutOfBandMessage) error
125+
Typing(context.Context, gregor.OutOfBandMessage) error
125126
}
126127

127128
type AppState interface {

0 commit comments

Comments
 (0)