imagepack: move emoji shortcodes to go-util

This commit is contained in:
Tulir Asokan
2026-04-30 12:24:08 +03:00
parent eaf387abfe
commit 2a0da7801a
7 changed files with 5 additions and 84 deletions

1
.gitignore vendored
View File

@@ -7,7 +7,6 @@
*.json
!pkg/connector/emojis/unicodemojipack.json
!pkg/connector/emojis/shortcodes.json
*.db*
*.log
*.bak

2
go.mod
View File

@@ -27,7 +27,7 @@ require (
github.com/rs/zerolog v1.35.0
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.18.0
go.mau.fi/util v0.9.9-0.20260424160448-fd0d9737ad38
go.mau.fi/util v0.9.9-0.20260430092340-8772e7714ea5
go.mau.fi/webp v0.2.0
go.mau.fi/zerozap v0.1.2
go.opentelemetry.io/otel v1.42.0

4
go.sum
View File

@@ -112,8 +112,8 @@ github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
go.mau.fi/util v0.9.9-0.20260424160448-fd0d9737ad38 h1:D4OKITjyvlud39Q10oMnfhdeNkzEIVkXrEeCW6nvgLk=
go.mau.fi/util v0.9.9-0.20260424160448-fd0d9737ad38/go.mod h1:up/5mbzH2M1pSBNXqRxODn8dg/hEKbLJu92W4/SNAX0=
go.mau.fi/util v0.9.9-0.20260430092340-8772e7714ea5 h1:cNm4gkt7j907g1Q4XvyNKW8tTM8BaU91Kbfa5GGyiCs=
go.mau.fi/util v0.9.9-0.20260430092340-8772e7714ea5/go.mod h1:up/5mbzH2M1pSBNXqRxODn8dg/hEKbLJu92W4/SNAX0=
go.mau.fi/webp v0.2.0 h1:QVMenHw7JDb4vall5sV75JNBQj9Hw4u8AKbi1QetHvg=
go.mau.fi/webp v0.2.0/go.mod h1:VSg9MyODn12Mb5pyG0NIyNFhujrmoFSsZBs8syOZD1Q=
go.mau.fi/zeroconfig v0.2.0 h1:e/OGEERqVRRKlgaro7E6bh8xXiKFSXB3eNNIud7FUjU=

View File

@@ -20,11 +20,9 @@ import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"sync"
"go.mau.fi/util/exstrings"
"go.mau.fi/util/variationselector"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/id"
@@ -38,35 +36,6 @@ var initOnce sync.Once
var unicodemojiPack = map[string]int64{}
var reverseUnicodemojiPack = map[int64]string{}
//go:embed shortcodes.json
//go:generate go run shortcode_gen.go
var shortcodesJSON string
var shortcodeMap = map[string]string{}
var shortcodeInit = sync.OnceFunc(func() {
if err := json.Unmarshal(exstrings.UnsafeBytes(shortcodesJSON), &shortcodeMap); err != nil {
panic(fmt.Errorf("failed to unmarshal shortcodes: %w", err))
}
})
func GetShortcodes() map[string]string {
shortcodeInit()
return shortcodeMap
}
var skinToneRemover = strings.NewReplacer(
"\U0001F3FB", "",
"\U0001F3FC", "",
"\U0001F3FD", "",
"\U0001F3FE", "",
"\U0001F3FF", "",
)
func GetShortcode(emoji string) string {
sc := GetShortcodes()
emoji = skinToneRemover.Replace(emoji)
return sc[variationselector.Add(emoji)]
}
func doInit() {
if err := json.Unmarshal(exstrings.UnsafeBytes(unicodemojiPackJSON), &unicodemojiPack); err != nil {
panic(fmt.Errorf("Failed to unmarshal unicodemojipack: %w", err))

View File

@@ -1,46 +0,0 @@
//go:build ignore
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"go.mau.fi/util/exerrors"
"go.mau.fi/util/variationselector"
)
type Emoji struct {
Unified string `json:"unified"`
ShortName string `json:"short_name"`
}
func unifiedToUnicode(input string) string {
parts := strings.Split(input, "-")
output := make([]rune, len(parts))
for i, part := range parts {
output[i] = rune(exerrors.Must(strconv.ParseInt(part, 16, 32)))
}
return string(output)
}
func main() {
resp := exerrors.Must(http.Get("https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json"))
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(fmt.Errorf("unexpected status code %d", resp.StatusCode))
}
var emojis []*Emoji
exerrors.PanicIfNotNil(json.NewDecoder(resp.Body).Decode(&emojis))
output := make(map[string]string)
for _, emoji := range emojis {
output[variationselector.Add(unifiedToUnicode(emoji.Unified))] = emoji.ShortName
}
f := exerrors.Must(os.OpenFile("shortcodes.json", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644))
exerrors.PanicIfNotNil(json.NewEncoder(f).Encode(output))
_ = f.Close()
}

File diff suppressed because one or more lines are too long

View File

@@ -32,6 +32,7 @@ import (
"time"
"github.com/rs/zerolog"
"go.mau.fi/util/emojishortcodes"
"go.mau.fi/util/exmaps"
"go.mau.fi/util/ffmpeg"
"go.mau.fi/util/variationselector"
@@ -43,7 +44,6 @@ import (
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
"go.mau.fi/mautrix-telegram/pkg/connector/media"
"go.mau.fi/mautrix-telegram/pkg/connector/store"
"go.mau.fi/mautrix-telegram/pkg/gotd/telegram/uploader"
@@ -555,7 +555,7 @@ func (tc *TelegramClient) DownloadImagePack(ctx context.Context, url string) (*b
var firstShortcode string
if key == "" {
for _, emoji := range imageEmojis {
shortcode := emojis.GetShortcode(emoji)
shortcode := emojishortcodes.Get(emoji)
if shortcode == "" {
continue
}