/
notifications.go
197 lines (177 loc) · 6.76 KB
/
notifications.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
package keybase
import (
"errors"
"fmt"
"runtime"
"time"
"github.com/adamwalz/keybase-client/go/chat"
"github.com/adamwalz/keybase-client/go/chat/globals"
"github.com/adamwalz/keybase-client/go/chat/storage"
"github.com/adamwalz/keybase-client/go/chat/types"
"github.com/adamwalz/keybase-client/go/chat/utils"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/chat1"
"github.com/adamwalz/keybase-client/go/protocol/gregor1"
"github.com/adamwalz/keybase-client/go/protocol/keybase1"
"github.com/kyokomi/emoji"
context "golang.org/x/net/context"
)
type Person struct {
KeybaseUsername string
KeybaseAvatar string
IsBot bool
}
type Message struct {
ID int
Kind string // "Text" | "Reaction"
Plaintext string
ServerMessage string // This is the server's suggested display message for the notification
From *Person
At int64
}
type ChatNotification struct {
Message *Message
ConvID string
TeamName string
TopicName string
TlfName string
// e.g. "keybase#general, CoolTeam, Susannah,Jake"
ConversationName string
IsGroupConversation bool
IsPlaintext bool
SoundName string
BadgeCount int
}
func HandlePostTextReply(strConvID, tlfName string, intMessageID int, body string) (err error) {
ctx := context.Background()
defer kbCtx.CTrace(ctx, "HandlePostTextReply", &err)()
defer func() { err = flattenError(err) }()
outboxID, err := storage.NewOutboxID()
if err != nil {
return err
}
convID, err := chat1.MakeConvID(strConvID)
if err != nil {
return err
}
_, err = kbCtx.ChatHelper.SendTextByIDNonblock(context.Background(), convID, tlfName, body, &outboxID, nil)
kbCtx.Log.CDebugf(ctx, "Marking as read from QuickReply: convID: %s", strConvID)
gc := globals.NewContext(kbCtx, kbChatCtx)
uid, err := utils.AssertLoggedInUID(ctx, gc)
if err != nil {
return err
}
msgID := chat1.MessageID(intMessageID)
if err = kbChatCtx.InboxSource.MarkAsRead(context.Background(), convID, uid, &msgID, false /* forceUnread */); err != nil {
kbCtx.Log.CDebugf(ctx, "Failed to mark as read from QuickReply: convID: %s. Err: %s", strConvID, err)
// We don't want to fail this method call just because we couldn't mark it as aread
err = nil
}
return nil
}
func HandleBackgroundNotification(strConvID, body, serverMessageBody, sender string, intMembersType int,
displayPlaintext bool, intMessageID int, pushID string, badgeCount, unixTime int, soundName string,
pusher PushNotifier, showIfStale bool) (err error) {
if err := waitForInit(10 * time.Second); err != nil {
return err
}
gc := globals.NewContext(kbCtx, kbChatCtx)
ctx := globals.ChatCtx(context.Background(), gc,
keybase1.TLFIdentifyBehavior_CHAT_GUI, nil, chat.NewCachingIdentifyNotifier(gc))
defer kbCtx.CTrace(ctx, fmt.Sprintf("HandleBackgroundNotification(%s,%s,%v,%d,%d,%s,%d,%d)",
strConvID, sender, displayPlaintext, intMembersType, intMessageID, pushID, badgeCount, unixTime), &err)()
defer func() { err = flattenError(err) }()
// Unbox
if !kbCtx.ActiveDevice.HaveKeys() {
return libkb.LoginRequiredError{}
}
mp := chat.NewMobilePush(gc)
uid := gregor1.UID(kbCtx.Env.GetUID().ToBytes())
convID, err := chat1.MakeConvID(strConvID)
if err != nil {
kbCtx.Log.CDebugf(ctx, "HandleBackgroundNotification: invalid convID: %s msg: %s", strConvID, err)
return err
}
membersType := chat1.ConversationMembersType(intMembersType)
conv, err := utils.GetVerifiedConv(ctx, gc, uid, convID, types.InboxSourceDataSourceLocalOnly)
if err != nil {
kbCtx.Log.CDebugf(ctx, "Failed to get conversation info", err)
return err
}
chatNotification := ChatNotification{
IsPlaintext: displayPlaintext,
Message: &Message{
ID: intMessageID,
ServerMessage: serverMessageBody,
From: &Person{},
At: int64(unixTime) * 1000,
},
ConvID: strConvID,
TopicName: conv.Info.TopicName,
TlfName: conv.Info.TlfName,
IsGroupConversation: len(conv.Info.Participants) > 2,
ConversationName: utils.FormatConversationName(conv.Info, string(kbCtx.Env.GetUsername())),
SoundName: soundName,
BadgeCount: badgeCount,
}
msgUnboxed, err := mp.UnboxPushNotification(ctx, uid, convID, membersType, body)
if err == nil && msgUnboxed.IsValid() {
chatNotification.Message.From.IsBot = msgUnboxed.SenderIsBot()
username := msgUnboxed.Valid().SenderUsername
chatNotification.Message.From.KeybaseUsername = username
if displayPlaintext && !msgUnboxed.Valid().IsEphemeral() {
// We show avatars on Android
if runtime.GOOS == "android" {
avatar, err := kbSvc.GetUserAvatar(username)
if err != nil {
kbCtx.Log.CDebugf(ctx, "Push Notif: Err in getting user avatar %v", err)
} else {
chatNotification.Message.From.KeybaseAvatar = avatar
}
}
switch msgUnboxed.GetMessageType() {
case chat1.MessageType_TEXT:
chatNotification.Message.Kind = "Text"
chatNotification.Message.Plaintext = msgUnboxed.Valid().MessageBody.Text().Body
case chat1.MessageType_REACTION:
chatNotification.Message.Kind = "Reaction"
reaction, err := utils.GetReaction(msgUnboxed)
if err != nil {
return err
}
chatNotification.Message.Plaintext =
emoji.Sprintf("Reacted to your message with %v", reaction)
default:
kbCtx.Log.CDebugf(ctx, "unboxNotification: Unknown message type: %v",
msgUnboxed.GetMessageType())
return errors.New("invalid message type for plaintext")
}
}
} else {
kbCtx.Log.CDebugf(ctx, "unboxNotification: failed to unbox: %s", err)
chatNotification.Message.From.KeybaseUsername = sender
// just bail out of here at this point since we won't be displaying anything useful,
// and we don't want to accidentally ack the plaintext notification when we didn't really
// display it.
if len(serverMessageBody) == 0 {
return errors.New("Unbox failed; nothing to display")
}
}
age := time.Since(time.Unix(int64(unixTime), 0))
// On iOS we don't want to show stale notifications. Nonsilent notifications
// can come later and cause duplicate notifications. On Android, both silent
// and non-silent notifications go through this function; and Java checks if we
// have already seen a notification. We don't need this stale logic.
if !showIfStale && age >= 2*time.Minute {
kbCtx.Log.CDebugf(ctx, "HandleBackgroundNotification: stale notification: %v", age)
return errors.New("stale notification")
}
// only display and ack this notification if we actually have something to display
if pusher != nil && (len(chatNotification.Message.Plaintext) > 0 || len(chatNotification.Message.ServerMessage) > 0) {
pusher.DisplayChatNotification(&chatNotification)
if len(pushID) > 0 {
mp.AckNotificationSuccess(ctx, []string{pushID})
}
}
return nil
}