Skip to content

Commit

Permalink
add quick reaction input (keybase#23602)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaxim authored Apr 9, 2020
1 parent bed2a57 commit f53fe99
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
69 changes: 69 additions & 0 deletions go/chat/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,9 +877,78 @@ func (h *Server) runStellarSendUI(ctx context.Context, sessionID int, uid gregor
return chat1.NewMessageBodyWithText(newBody), nil
}

var quickReactionPattern = regexp.MustCompile(`(?:^\+:)([^\s]+)(?::)$`)

func (h *Server) isQuickReaction(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
body string) (reaction string, msgID chat1.MessageID, ok bool) {
body = strings.TrimSpace(body)
if !(strings.HasPrefix(body, "+:") && strings.HasSuffix(body, ":")) {
return "", 0, false
}
hits := quickReactionPattern.FindStringSubmatch(body)
if len(hits) < 2 {
return "", 0, false
}
tryStock := func() (string, bool) {
if h.G().EmojiSource.IsStockEmoji(hits[1]) {
return ":" + hits[1] + ":", true
}
return "", false
}
tryCustom := func() (string, bool) {
emojis, err := h.G().EmojiSource.Harvest(ctx, body, uid, convID, types.EmojiHarvestModeFast)
if err != nil {
h.Debug(ctx, "isQuickReaction: failed to harvest: %s", err)
return "", false
}
if len(emojis) != 1 {
return "", false
}
return ":" + emojis[0].Alias + ":", true
}
var hit *string
if reaction, ok = tryStock(); ok {
hit = new(string)
*hit = reaction
}
if hit == nil {
if reaction, ok = tryCustom(); ok {
hit = new(string)
*hit = reaction
}
}
if hit == nil {
return "", 0, false
}
conv, err := utils.GetUnverifiedConv(ctx, h.G(), uid, convID, types.InboxSourceDataSourceLocalOnly)
if err != nil {
h.Debug(ctx, "isQuickReaction: failed to get conv: %s", err)
return "", 0, false
}
return *hit, conv.MaxVisibleMsgID(), true
}

func (h *Server) PostTextNonblock(ctx context.Context, arg chat1.PostTextNonblockArg) (res chat1.PostLocalNonblockRes, err error) {
ctx = globals.ChatCtx(ctx, h.G(), arg.IdentifyBehavior, nil, h.identNotifier)
defer h.Trace(ctx, func() error { return err }, "PostTextNonblock")()
uid, err := utils.AssertLoggedInUID(ctx, h.G())
if err != nil {
return res, err
}
reaction, msgID, ok := h.isQuickReaction(ctx, uid, arg.ConversationID, arg.Body)
if ok {
h.Debug(ctx, "PostTextNonblock: detected quick reaction")
return h.PostReactionNonblock(ctx, chat1.PostReactionNonblockArg{
ConversationID: arg.ConversationID,
TlfName: arg.TlfName,
TlfPublic: arg.TlfPublic,
Supersedes: msgID,
Body: reaction,
OutboxID: arg.OutboxID,
ClientPrev: arg.ClientPrev,
IdentifyBehavior: arg.IdentifyBehavior,
})
}

var parg chat1.PostLocalNonblockArg
parg.SessionID = arg.SessionID
Expand Down
6 changes: 3 additions & 3 deletions shared/chat/conversation/input-area/normal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const searchUsersAndTeamsAndTeamChannels = memoize(
const suggestorToMarker = {
channels: '#',
commands: /(!|\/)/,
emoji: ':',
emoji: /(\+?):/,
// 'users' is for @user, @team, and @team#channel
users: /((\+\d+(\.\d+)?[a-zA-Z]{3,12}@)|@)/, // match normal mentions and ones in a stellar send
}
Expand Down Expand Up @@ -138,8 +138,8 @@ const emojiRenderer = (item: EmojiData, selected: boolean) => {
</Kb.Box2>
)
}
const emojiTransformer = (emoji: EmojiData, _: unknown, tData: TransformerData, preview: boolean) => {
return standardTransformer(`:${emoji.short_name}:`, tData, preview)
const emojiTransformer = (emoji: EmojiData, marker: string, tData: TransformerData, preview: boolean) => {
return standardTransformer(`${marker}${emoji.short_name}:`, tData, preview)
}

type InputState = {
Expand Down

0 comments on commit f53fe99

Please sign in to comment.