1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-21 19:03:26 +01:00

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