Skip to content

Commit

Permalink
emoji: initialize lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 15, 2024
1 parent 825091d commit 318f8b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
5 changes: 2 additions & 3 deletions pkg/connector/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (s *SlackClient) syncEmojis(ctx context.Context, onlyIfCountMismatch bool)
if uri, ok := uploaded[alias]; ok {
dbEmoji.Alias = alias
dbEmoji.ImageMXC = uri
} else if unicode, ok := emoji.ShortcodeToUnicodeMap[alias]; ok {
} else if unicode := emoji.GetUnicode(alias); unicode != "" {
dbEmoji.Alias = unicode
}
err = s.Main.DB.Emoji.Put(ctx, dbEmoji)
Expand All @@ -245,8 +245,7 @@ func (s *SlackClient) syncEmojis(ctx context.Context, onlyIfCountMismatch bool)
}

func (s *SlackClient) TryGetEmoji(ctx context.Context, shortcode string) (string, bool) {
unicode, ok := emoji.ShortcodeToUnicodeMap[shortcode]
if ok {
if unicode := emoji.GetUnicode(shortcode); unicode != "" {
return unicode, false
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ func (s *SlackClient) PreHandleMatrixReaction(ctx context.Context, msg *bridgev2
}
emojiID = dbEmoji.EmojiID
} else {
var ok bool
emojiID, ok = emoji.UnicodeToShortcodeMap[key]
if !ok {
emojiID = emoji.GetShortcode(key)
if emojiID == "" {
err = fmt.Errorf("unknown emoji %q", key)
}
}
Expand Down
52 changes: 33 additions & 19 deletions pkg/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,53 @@
package emoji

import (
_ "embed"
"embed"
"encoding/json"
"regexp"
"strings"
"sync"

"go.mau.fi/util/exerrors"
)

//go:generate go run ./emoji-generate.go
//go:embed emoji.json
var emojiFileData []byte
var emojiFileData embed.FS

var ShortcodeToUnicodeMap map[string]string
var UnicodeToShortcodeMap map[string]string
var shortcodeToUnicodeMap map[string]string
var unicodeToShortcodeMap map[string]string
var shortcodeRegex *regexp.Regexp
var initOnce sync.Once

func init() {
exerrors.PanicIfNotNil(json.Unmarshal(emojiFileData, &ShortcodeToUnicodeMap))
UnicodeToShortcodeMap = make(map[string]string, len(ShortcodeToUnicodeMap))
for shortcode, emoji := range ShortcodeToUnicodeMap {
UnicodeToShortcodeMap[emoji] = shortcode
func doInit() {
file := exerrors.Must(emojiFileData.Open("emoji.json"))
exerrors.PanicIfNotNil(json.NewDecoder(file).Decode(&shortcodeToUnicodeMap))
exerrors.PanicIfNotNil(file.Close())
unicodeToShortcodeMap = make(map[string]string, len(shortcodeToUnicodeMap))
for shortcode, emoji := range shortcodeToUnicodeMap {
unicodeToShortcodeMap[emoji] = shortcode
}
shortcodeRegex = regexp.MustCompile(`:[^:\s]*:`)
}

var ShortcodeRegex = regexp.MustCompile(`:[^:\s]*:`)
func GetShortcode(unicode string) string {
initOnce.Do(doInit)
return unicodeToShortcodeMap[unicode]
}

func GetUnicode(shortcode string) string {
return shortcodeToUnicodeMap[strings.Trim(shortcode, ":")]
}

func replaceShortcode(code string) string {
emoji := GetUnicode(code)
if emoji == "" {
return code
}
return emoji
}

func ReplaceShortcodesWithUnicode(text string) string {
return ShortcodeRegex.ReplaceAllStringFunc(text, func(code string) string {
strippedCode := strings.Trim(code, ":")
emoji, found := ShortcodeToUnicodeMap[strippedCode]
if found {
return emoji
} else {
return code
}
})
initOnce.Do(doInit)
return shortcodeRegex.ReplaceAllStringFunc(text, replaceShortcode)
}

0 comments on commit 318f8b9

Please sign in to comment.