Skip to content

Commit 8d490bd

Browse files
authored
allow custom emojis on more message types (keybase#23584)
* allow custom emojis on more message types * short ciruit * dont forget reactions
1 parent ce7be6c commit 8d490bd

13 files changed

Lines changed: 227 additions & 108 deletions

File tree

‎go/chat/boxer.go‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,24 +1301,13 @@ func (b *Boxer) getEmojis(ctx context.Context, topicType chat1.TopicType, body c
13011301
if topicType != chat1.TopicType_CHAT {
13021302
return nil
13031303
}
1304-
typ, err := body.MessageType()
1305-
if err != nil {
1304+
emojis := body.GetEmojis()
1305+
if emojis == nil {
13061306
return nil
13071307
}
1308-
makeList := func(m map[string]chat1.HarvestedEmoji) (l []chat1.HarvestedEmoji) {
1309-
l = make([]chat1.HarvestedEmoji, 0, len(m))
1310-
for _, e := range m {
1311-
l = append(l, e)
1312-
}
1313-
return l
1314-
}
1315-
switch typ {
1316-
case chat1.MessageType_TEXT:
1317-
return makeList(body.Text().Emojis)
1318-
case chat1.MessageType_REACTION:
1319-
return makeList(body.Reaction().Emojis)
1320-
case chat1.MessageType_EDIT:
1321-
return makeList(body.Edit().Emojis)
1308+
res = make([]chat1.HarvestedEmoji, 0, len(emojis))
1309+
for _, emoji := range emojis {
1310+
res = append(res, emoji)
13221311
}
13231312
return res
13241313
}

‎go/chat/localizer.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,12 @@ func (s *localizerPipeline) localizeConversation(ctx context.Context, uid gregor
854854
conversationLocal.Info.TopicName = body.Metadata().ConversationTitle
855855
case chat1.MessageType_HEADLINE:
856856
conversationLocal.Info.Headline = body.Headline().Headline
857+
emojis := body.Headline().Emojis
858+
headlineEmojis := make([]chat1.HarvestedEmoji, 0, len(emojis))
859+
for _, emoji := range emojis {
860+
headlineEmojis = append(headlineEmojis, emoji)
861+
}
862+
conversationLocal.Info.HeadlineEmojis = headlineEmojis
857863
case chat1.MessageType_PIN:
858864
pinnedMsg, pinnerUsername, valid, err := s.getPinnedMsg(ctx, uid, conversationRemote, mm)
859865
if err != nil {

‎go/chat/sender.go‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -595,22 +595,15 @@ func (s *BlockingSender) handleEmojis(ctx context.Context, uid gregor1.UID,
595595
s.Debug(ctx, "handleEmojis: failed to get body type: %s", err)
596596
return msg, nil
597597
}
598-
var body string
599-
switch typ {
600-
case chat1.MessageType_TEXT:
601-
body = msg.MessageBody.Text().Body
602-
case chat1.MessageType_REACTION:
603-
body = msg.MessageBody.Reaction().Body
604-
case chat1.MessageType_EDIT:
605-
body = msg.MessageBody.Edit().Body
606-
default:
598+
body := msg.MessageBody.TextForDecoration()
599+
if len(body) == 0 {
607600
return msg, nil
608601
}
609602
emojis, err := s.G().EmojiSource.Harvest(ctx, body, uid, convID, types.EmojiHarvestModeNormal)
610603
if err != nil {
611604
return msg, err
612605
}
613-
ct := make(map[string]chat1.HarvestedEmoji)
606+
ct := make(map[string]chat1.HarvestedEmoji, len(emojis))
614607
for _, emoji := range emojis {
615608
ct[emoji.Alias] = emoji
616609
}
@@ -643,6 +636,30 @@ func (s *BlockingSender) handleEmojis(ctx context.Context, uid gregor1.UID,
643636
MessageBody: chat1.NewMessageBodyWithEdit(newBody),
644637
SupersedesOutboxID: msg.SupersedesOutboxID,
645638
}, nil
639+
case chat1.MessageType_REQUESTPAYMENT:
640+
newBody := msg.MessageBody.Requestpayment().DeepCopy()
641+
newBody.Emojis = ct
642+
return chat1.MessagePlaintext{
643+
ClientHeader: msg.ClientHeader,
644+
MessageBody: chat1.NewMessageBodyWithRequestpayment(newBody),
645+
SupersedesOutboxID: msg.SupersedesOutboxID,
646+
}, nil
647+
case chat1.MessageType_ATTACHMENT:
648+
newBody := msg.MessageBody.Attachment().DeepCopy()
649+
newBody.Emojis = ct
650+
return chat1.MessagePlaintext{
651+
ClientHeader: msg.ClientHeader,
652+
MessageBody: chat1.NewMessageBodyWithAttachment(newBody),
653+
SupersedesOutboxID: msg.SupersedesOutboxID,
654+
}, nil
655+
case chat1.MessageType_HEADLINE:
656+
newBody := msg.MessageBody.Headline().DeepCopy()
657+
newBody.Emojis = ct
658+
return chat1.MessagePlaintext{
659+
ClientHeader: msg.ClientHeader,
660+
MessageBody: chat1.NewMessageBodyWithHeadline(newBody),
661+
SupersedesOutboxID: msg.SupersedesOutboxID,
662+
}, nil
646663
}
647664
return msg, nil
648665
}
@@ -1451,13 +1468,7 @@ func (s *Deliverer) Start(ctx context.Context, uid gregor1.UID) {
14511468
}
14521469

14531470
// fill in emojis
1454-
var emojiText string
1455-
switch obr.MessageType() {
1456-
case chat1.MessageType_TEXT:
1457-
emojiText = obr.Msg.MessageBody.Text().Body
1458-
case chat1.MessageType_REACTION:
1459-
emojiText = obr.Msg.MessageBody.Reaction().Body
1460-
}
1471+
emojiText := obr.Msg.MessageBody.TextForDecoration()
14611472
if len(emojiText) > 0 {
14621473
if obr.Msg.Emojis, err = s.G().EmojiSource.Harvest(ctx, emojiText,
14631474
uid, convID, types.EmojiHarvestModeFast); err != nil {

‎go/chat/storage/inbox.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"golang.org/x/net/context"
1818
)
1919

20-
const inboxVersion = 31
20+
const inboxVersion = 32
2121

2222
type InboxFlushMode int
2323

@@ -465,6 +465,7 @@ func (i *Inbox) MergeLocalMetadata(ctx context.Context, uid gregor1.UID, convs [
465465
Name: convLocal.Info.TlfName,
466466
TopicName: topicName,
467467
Headline: convLocal.Info.Headline,
468+
HeadlineEmojis: convLocal.Info.HeadlineEmojis,
468469
Snippet: snippet,
469470
SnippetDecoration: snippetDecoration,
470471
}

‎go/chat/sync.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (s *Syncer) notifyIncrementalSync(ctx context.Context, uid gregor1.UID,
314314
rc.LocalMetadata = md
315315
itemsByTopicType[c.GetTopicType()] = append(itemsByTopicType[c.GetTopicType()],
316316
chat1.ChatSyncIncrementalConv{
317-
Conv: utils.PresentRemoteConversation(ctx, s.G(), rc),
317+
Conv: utils.PresentRemoteConversation(ctx, s.G(), uid, rc),
318318
ShouldUnbox: shouldUnboxMap[c.GetConvID().ConvIDStr()],
319319
})
320320
}

‎go/chat/types/types.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type RemoteConversationMetadata struct {
9292
Snippet string `codec:"s"`
9393
SnippetDecoration chat1.SnippetDecoration `codec:"d"`
9494
Headline string `codec:"h"`
95+
HeadlineEmojis []chat1.HarvestedEmoji `codec:"e"`
9596
WriterNames []string `codec:"w"`
9697
FullNamesForSearch []*string `codec:"f"`
9798
ResetParticipants []string `codec:"r"`
@@ -103,11 +104,13 @@ func (m RemoteConversationMetadata) DeepCopy() (res RemoteConversationMetadata)
103104
res.Snippet = m.Snippet
104105
res.SnippetDecoration = m.SnippetDecoration
105106
res.Headline = m.Headline
107+
res.HeadlineEmojis = make([]chat1.HarvestedEmoji, len(m.HeadlineEmojis))
108+
copy(res.HeadlineEmojis, m.HeadlineEmojis)
106109
res.WriterNames = make([]string, len(m.WriterNames))
107-
res.FullNamesForSearch = make([]*string, len(m.FullNamesForSearch))
108-
res.ResetParticipants = make([]string, len(m.ResetParticipants))
109110
copy(res.WriterNames, m.WriterNames)
111+
res.FullNamesForSearch = make([]*string, len(m.FullNamesForSearch))
110112
copy(res.FullNamesForSearch, m.FullNamesForSearch)
113+
res.ResetParticipants = make([]string, len(m.ResetParticipants))
111114
copy(res.ResetParticipants, m.ResetParticipants)
112115
return res
113116
}

‎go/chat/uiinboxloader.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (h *UIInboxLoader) presentUnverifiedInbox(ctx context.Context, convs []type
128128
rawConv.Conv.GetConvID())
129129
continue
130130
}
131-
res.Items = append(res.Items, utils.PresentRemoteConversation(ctx, h.G(), rawConv))
131+
res.Items = append(res.Items, utils.PresentRemoteConversation(ctx, h.G(), h.uid, rawConv))
132132
}
133133
res.Offline = offline
134134
return res, err
@@ -237,7 +237,7 @@ func (h *UIInboxLoader) flushFailed(r failedResponse) {
237237
if err == nil {
238238
if err := ui.ChatInboxFailed(ctx, chat1.ChatInboxFailedArg{
239239
ConvID: r.Conv.GetConvID(),
240-
Error: utils.PresentConversationErrorLocal(ctx, h.G(), *r.Conv.Error),
240+
Error: utils.PresentConversationErrorLocal(ctx, h.G(), h.uid, *r.Conv.Error),
241241
}); err != nil {
242242
h.Debug(ctx, "flushFailed: failed to send failed conv: %s", err)
243243
}

‎go/chat/utils/utils.go‎

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ func PresentRemoteConversationAsBigTeamChannelRow(ctx context.Context, rc types.
13261326
return res
13271327
}
13281328

1329-
func PresentRemoteConversation(ctx context.Context, g *globals.Context, rc types.RemoteConversation) (res chat1.UnverifiedInboxUIItem) {
1329+
func PresentRemoteConversation(ctx context.Context, g *globals.Context, uid gregor1.UID, rc types.RemoteConversation) (res chat1.UnverifiedInboxUIItem) {
13301330
var tlfName string
13311331
rawConv := rc.Conv
13321332
latest, err := PickLatestMessageSummary(rawConv, nil)
@@ -1360,9 +1360,10 @@ func PresentRemoteConversation(ctx context.Context, g *globals.Context, rc types
13601360
chat1.NewConversationCommandGroupsWithBuiltin(g.CommandsSource.GetBuiltinCommandType(ctx, rc))
13611361
if rc.LocalMetadata != nil {
13621362
res.LocalMetadata = &chat1.UnverifiedInboxUIItemMetadata{
1363-
ChannelName: rc.LocalMetadata.TopicName,
1364-
Headline: rc.LocalMetadata.Headline,
1365-
HeadlineDecorated: DecorateWithLinks(ctx, EscapeForDecorate(ctx, rc.LocalMetadata.Headline)),
1363+
ChannelName: rc.LocalMetadata.TopicName,
1364+
Headline: rc.LocalMetadata.Headline,
1365+
HeadlineDecorated: DecorateWithLinks(ctx, PresentDecoratedSnippet(ctx, g, rc.LocalMetadata.Headline, uid, rc.GetConvID(),
1366+
chat1.MessageType_HEADLINE, rc.LocalMetadata.HeadlineEmojis)),
13661367
Snippet: rc.LocalMetadata.Snippet,
13671368
SnippetDecoration: rc.LocalMetadata.SnippetDecoration,
13681369
WriterNames: rc.LocalMetadata.WriterNames,
@@ -1376,9 +1377,10 @@ func PresentRemoteConversation(ctx context.Context, g *globals.Context, rc types
13761377
return res
13771378
}
13781379

1379-
func PresentRemoteConversations(ctx context.Context, g *globals.Context, rcs []types.RemoteConversation) (res []chat1.UnverifiedInboxUIItem) {
1380+
func PresentRemoteConversations(ctx context.Context, g *globals.Context, uid gregor1.UID, rcs []types.RemoteConversation) (res []chat1.UnverifiedInboxUIItem) {
1381+
res = make([]chat1.UnverifiedInboxUIItem, 0, len(rcs))
13801382
for _, rc := range rcs {
1381-
res = append(res, PresentRemoteConversation(ctx, g, rc))
1383+
res = append(res, PresentRemoteConversation(ctx, g, uid, rc))
13821384
}
13831385
return res
13841386
}
@@ -1404,16 +1406,17 @@ func PresentRemoteConversationAsSearchHit(conv types.RemoteConversation, usernam
14041406
}
14051407

14061408
func PresentRemoteConversationsAsSearchHits(convs []types.RemoteConversation, username string) (res []chat1.UIChatSearchConvHit) {
1409+
res = make([]chat1.UIChatSearchConvHit, 0, len(convs))
14071410
for _, c := range convs {
14081411
res = append(res, PresentRemoteConversationAsSearchHit(c, username))
14091412
}
14101413
return res
14111414
}
14121415

1413-
func PresentConversationErrorLocal(ctx context.Context, g *globals.Context, rawConv chat1.ConversationErrorLocal) (res chat1.InboxUIItemError) {
1416+
func PresentConversationErrorLocal(ctx context.Context, g *globals.Context, uid gregor1.UID, rawConv chat1.ConversationErrorLocal) (res chat1.InboxUIItemError) {
14141417
res.Message = rawConv.Message
14151418
res.RekeyInfo = rawConv.RekeyInfo
1416-
res.RemoteConv = PresentRemoteConversation(ctx, g, types.RemoteConversation{
1419+
res.RemoteConv = PresentRemoteConversation(ctx, g, uid, types.RemoteConversation{
14171420
Conv: rawConv.RemoteConv,
14181421
ConvIDStr: rawConv.RemoteConv.GetConvID().ConvIDStr(),
14191422
})
@@ -1433,6 +1436,7 @@ func getParticipantType(username string) chat1.UIParticipantType {
14331436
}
14341437

14351438
func PresentConversationParticipantsLocal(ctx context.Context, rawParticipants []chat1.ConversationLocalParticipant) (participants []chat1.UIParticipant) {
1439+
participants = make([]chat1.UIParticipant, 0, len(rawParticipants))
14361440
for _, p := range rawParticipants {
14371441
participantType := getParticipantType(p.Username)
14381442
participants = append(participants, chat1.UIParticipant{
@@ -1465,7 +1469,8 @@ func PresentConversationLocal(ctx context.Context, g *globals.Context, uid grego
14651469
GetConvSnippet(ctx, g, uid, rawConv, g.GetEnv().GetUsername().String())
14661470
res.Channel = rawConv.Info.TopicName
14671471
res.Headline = rawConv.Info.Headline
1468-
res.HeadlineDecorated = DecorateWithLinks(ctx, EscapeForDecorate(ctx, rawConv.Info.Headline))
1472+
res.HeadlineDecorated = DecorateWithLinks(ctx, PresentDecoratedSnippet(ctx, g, rawConv.Info.Headline, uid,
1473+
rawConv.GetConvID(), chat1.MessageType_HEADLINE, rawConv.Info.HeadlineEmojis))
14691474
res.ResetParticipants = rawConv.Info.ResetNames
14701475
res.Status = rawConv.Info.Status
14711476
res.MembersType = rawConv.GetMembersType()
@@ -1805,20 +1810,11 @@ func PresentDecoratedSnippet(ctx context.Context, g *globals.Context, body strin
18051810

18061811
func PresentDecoratedPendingTextBody(ctx context.Context, g *globals.Context, uid gregor1.UID,
18071812
convID chat1.ConversationID, msg chat1.MessagePlaintext) *string {
1808-
msgBody := msg.MessageBody
1809-
typ, err := msgBody.MessageType()
1813+
typ, err := msg.MessageBody.MessageType()
18101814
if err != nil {
18111815
return nil
18121816
}
1813-
var body string
1814-
switch typ {
1815-
case chat1.MessageType_TEXT:
1816-
body = msgBody.Text().Body
1817-
case chat1.MessageType_REACTION:
1818-
body = msgBody.Reaction().Body
1819-
default:
1820-
return nil
1821-
}
1817+
body := msg.MessageBody.TextForDecoration()
18221818
body = PresentDecoratedTextNoMentions(ctx, body)
18231819
body = g.EmojiSource.Decorate(ctx, body, uid, convID, typ, msg.Emojis)
18241820
return &body
@@ -1831,31 +1827,20 @@ func PresentDecoratedTextBody(ctx context.Context, g *globals.Context, uid grego
18311827
if err != nil {
18321828
return nil
18331829
}
1834-
var body string
1835-
var payments []chat1.TextPayment
1830+
body := msgBody.TextForDecoration()
1831+
if len(body) == 0 {
1832+
return nil
1833+
}
18361834
switch typ {
18371835
case chat1.MessageType_TEXT:
1838-
body = msgBody.Text().Body
1839-
payments = msgBody.Text().Payments
1840-
case chat1.MessageType_FLIP:
1841-
body = msgBody.Flip().Text
1842-
case chat1.MessageType_REQUESTPAYMENT:
1843-
body = msgBody.Requestpayment().Note
1844-
case chat1.MessageType_ATTACHMENT:
1845-
body = msgBody.Attachment().Object.Title
1836+
// Payment decorations
1837+
body = g.StellarSender.DecorateWithPayments(ctx, body, msgBody.Text().Payments)
18461838
case chat1.MessageType_SYSTEM:
18471839
body = systemMsgPresentText(ctx, uid, msg)
1848-
default:
1849-
return nil
18501840
}
18511841
body = PresentDecoratedTextNoMentions(ctx, body)
1852-
1853-
// Payment decorations
1854-
body = g.StellarSender.DecorateWithPayments(ctx, body, payments)
1855-
18561842
// Emojis
18571843
body = g.EmojiSource.Decorate(ctx, body, uid, convID, typ, msg.Emojis)
1858-
18591844
// Mentions
18601845
body = DecorateWithMentions(ctx, body, msg.AtMentionUsernames, msg.MaybeMentions, msg.ChannelMention,
18611846
msg.ChannelNameMentions)

‎go/protocol/chat1/extras.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,21 @@ func (b MessageBody) IsType(typ MessageType) bool {
962962
return btyp == typ
963963
}
964964

965+
func (b MessageBody) TextForDecoration() string {
966+
typ, err := b.MessageType()
967+
if err != nil {
968+
return ""
969+
}
970+
switch typ {
971+
case MessageType_REACTION:
972+
return b.Reaction().Body
973+
case MessageType_HEADLINE:
974+
return b.Headline().Headline
975+
default:
976+
return b.SearchableText()
977+
}
978+
}
979+
965980
func (b MessageBody) SearchableText() string {
966981
typ, err := b.MessageType()
967982
if err != nil {
@@ -987,6 +1002,29 @@ func (b MessageBody) SearchableText() string {
9871002
}
9881003
}
9891004

1005+
func (b MessageBody) GetEmojis() map[string]HarvestedEmoji {
1006+
typ, err := b.MessageType()
1007+
if err != nil {
1008+
return nil
1009+
}
1010+
switch typ {
1011+
case MessageType_TEXT:
1012+
return b.Text().Emojis
1013+
case MessageType_REACTION:
1014+
return b.Reaction().Emojis
1015+
case MessageType_EDIT:
1016+
return b.Edit().Emojis
1017+
case MessageType_REQUESTPAYMENT:
1018+
return b.Requestpayment().Emojis
1019+
case MessageType_ATTACHMENT:
1020+
return b.Attachment().Emojis
1021+
case MessageType_HEADLINE:
1022+
return b.Headline().Emojis
1023+
default:
1024+
return nil
1025+
}
1026+
}
1027+
9901028
func (m UIMessage) IsValid() bool {
9911029
if state, err := m.State(); err == nil {
9921030
return state == MessageUnboxedState_VALID

0 commit comments

Comments
 (0)