Convert mailer_emoji JSON file to map

This fixes a pending TODO comment regarding inefficient tags to emojis
mapping, by requiring a full scan over emoji aliases to determine
matches.

Instead, now the JSON file is a map, with aliases as keys, and emojis as
values. The script to convert the file with Python was:

```python
import json

with open("./mailer_emoji.json", "r", encoding="utf-8") as f:
    content = json.load(f)

emoji_map = {}
for emoji in content:
    for alias in emoji["aliases"]:
        if alias in emoji_map:
            print("WARNING: Duplicate alias:", alias)
            continue
        emoji_map[alias] = str(emoji["emoji"])

sorted_emoji_map = {k: emoji_map[k] for k in sorted(emoji_map)}

with open("./mailer_emoji_map.json", "w", encoding="utf-8") as f:
    json.dump(sorted_emoji_map, f, indent=4, ensure_ascii=False)
```
This commit is contained in:
Michael Manganiello 2023-05-13 11:33:04 -03:00
parent bd81aef1c9
commit 49bd6129ff
3 changed files with 1868 additions and 19 deletions

File diff suppressed because one or more lines are too long

1857
server/mailer_emoji_map.json Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,14 +4,15 @@ import (
_ "embed" // required by go:embed _ "embed" // required by go:embed
"encoding/json" "encoding/json"
"fmt" "fmt"
"heckel.io/ntfy/log"
"heckel.io/ntfy/util"
"mime" "mime"
"net" "net"
"net/smtp" "net/smtp"
"strings" "strings"
"sync" "sync"
"time" "time"
"heckel.io/ntfy/log"
"heckel.io/ntfy/util"
) )
type mailer interface { type mailer interface {
@ -131,31 +132,23 @@ This message was sent by {ip} at {time} via {topicURL}`
} }
var ( var (
//go:embed "mailer_emoji.json" //go:embed "mailer_emoji_map.json"
emojisJSON string emojisJSON string
) )
type emoji struct {
Emoji string `json:"emoji"`
Aliases []string `json:"aliases"`
}
func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) { func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) {
var emojis []emoji var emojiMap map[string]string
if err = json.Unmarshal([]byte(emojisJSON), &emojis); err != nil { if err = json.Unmarshal([]byte(emojisJSON), &emojiMap); err != nil {
return nil, nil, err return nil, nil, err
} }
tagsOut = make([]string, 0) tagsOut = make([]string, 0)
emojisOut = make([]string, 0) emojisOut = make([]string, 0)
nextTag: for _, t := range tags {
for _, t := range tags { // TODO Super inefficient; we should just create a .json file with a map if emoji, ok := emojiMap[t]; ok {
for _, e := range emojis { emojisOut = append(emojisOut, emoji)
if util.Contains(e.Aliases, t) { } else {
emojisOut = append(emojisOut, e.Emoji) tagsOut = append(tagsOut, t)
continue nextTag
}
} }
tagsOut = append(tagsOut, t)
} }
return return
} }