forked from keybase/client
/
typingmonitor.go
191 lines (167 loc) · 4.84 KB
/
typingmonitor.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
package chat
import (
"context"
"fmt"
"sync"
"time"
"strings"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/utils"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/clockwork"
)
const typingTimeout = 10 * time.Second
const maxExtensions = 50
type typingControlChans struct {
typer chat1.TyperInfo
stopCh chan struct{}
extendCh chan struct{}
}
func newTypingControlChans(typer chat1.TyperInfo) *typingControlChans {
return &typingControlChans{
typer: typer,
// Might not need these buffers, but we really don't want to deadlock
stopCh: make(chan struct{}, 5),
extendCh: make(chan struct{}, 5),
}
}
type TypingMonitor struct {
globals.Contextified
sync.Mutex
utils.DebugLabeler
timeout time.Duration
clock clockwork.Clock
typers map[string]*typingControlChans
// Testing
extendCh *chan struct{}
}
func NewTypingMonitor(g *globals.Context) *TypingMonitor {
return &TypingMonitor{
Contextified: globals.NewContextified(g),
DebugLabeler: utils.NewDebugLabeler(g.ExternalG(), "TypingMonitor", false),
typers: make(map[string]*typingControlChans),
clock: clockwork.NewRealClock(),
timeout: typingTimeout,
}
}
func (t *TypingMonitor) SetClock(clock clockwork.Clock) {
t.clock = clock
}
func (t *TypingMonitor) SetTimeout(timeout time.Duration) {
t.timeout = timeout
}
func (t *TypingMonitor) key(typer chat1.TyperInfo, convID chat1.ConversationID) string {
return fmt.Sprintf("%s:%s:%s", typer.Uid, typer.DeviceID, convID)
}
func (t *TypingMonitor) convKey(key string, convID chat1.ConversationID) bool {
toks := strings.Split(key, ":")
if len(toks) != 3 {
return false
}
return toks[2] == convID.String()
}
func (t *TypingMonitor) notifyConvUpdateLocked(ctx context.Context, convID chat1.ConversationID) {
var typers []chat1.TyperInfo
for k, v := range t.typers {
if t.convKey(k, convID) {
typers = append(typers, v.typer)
}
}
update := chat1.ConvTypingUpdate{
ConvID: convID,
Typers: typers,
}
t.G().ActivityNotifier.TypingUpdate(ctx, []chat1.ConvTypingUpdate{update})
}
func (t *TypingMonitor) Update(ctx context.Context, typer chat1.TyperInfo, convID chat1.ConversationID,
teamType chat1.TeamType, typing bool) {
// If this is about ourselves, then don't bother
cuid := t.G().Env.GetUID()
cdid := t.G().Env.GetDeviceID()
if cuid.Equal(typer.Uid) && cdid.Eq(typer.DeviceID) {
return
}
// If the update is for a big team we are not currently viewing, don't bother sending it
if teamType == chat1.TeamType_COMPLEX && !t.G().Syncer.IsSelectedConversation(convID) {
return
}
// Process the update
t.Lock()
key := t.key(typer, convID)
chans, alreadyTyping := t.typers[key]
t.Unlock()
if typing {
if alreadyTyping {
// If this key is already typing, let's extend it
select {
case chans.extendCh <- struct{}{}:
default:
// This should never happen, but be safe
t.Debug(ctx, "Update: overflowed extend channel, dropping update: %s convID: %s", typer,
convID)
}
} else {
// Not typing yet, just add it in and spawn waiter
chans := newTypingControlChans(typer)
t.insertIntoTypers(ctx, key, chans, convID)
t.waitOnTyper(ctx, chans, convID)
}
} else if alreadyTyping {
// If they are typing, then stop it
select {
case chans.stopCh <- struct{}{}:
default:
// This should never happen, but be safe
t.Debug(ctx, "Update: overflowed stop channel, dropping update: %s convID: %s", typer,
convID)
}
}
}
func (t *TypingMonitor) insertIntoTypers(ctx context.Context, key string, chans *typingControlChans,
convID chat1.ConversationID) {
t.Lock()
defer t.Unlock()
t.typers[key] = chans
t.notifyConvUpdateLocked(ctx, convID)
}
func (t *TypingMonitor) removeFromTypers(ctx context.Context, key string, convID chat1.ConversationID) {
t.Lock()
defer t.Unlock()
delete(t.typers, key)
t.notifyConvUpdateLocked(ctx, convID)
}
func (t *TypingMonitor) waitOnTyper(ctx context.Context, chans *typingControlChans,
convID chat1.ConversationID) {
key := t.key(chans.typer, convID)
ctx = globals.BackgroundChatCtx(ctx, t.G())
deadline := t.clock.Now().Add(t.timeout)
go func() {
extends := 0
for {
select {
case <-t.clock.AfterTime(deadline):
// Send notifications and bail
t.removeFromTypers(ctx, key, convID)
return
case <-chans.extendCh:
// Loop around to restart timer
extends++
if extends > maxExtensions {
t.Debug(ctx, "waitOnTyper: max extensions reached: uid: %s convID: %s", chans.typer.Uid, convID)
t.removeFromTypers(ctx, key, convID)
return
}
deadline = t.clock.Now().Add(t.timeout)
if t.extendCh != nil {
// Alerts tests we extended time
*t.extendCh <- struct{}{}
}
continue
case <-chans.stopCh:
// Stopped typing, just end it and remove entry in typers
t.removeFromTypers(ctx, key, convID)
return
}
}
}()
}