/
emoji.go
161 lines (138 loc) · 3.99 KB
/
emoji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// mautrix-slack - A Matrix-Slack puppeting bridge.
// Copyright (C) 2022 Max Sandholm
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
_ "embed"
"encoding/json"
"regexp"
"strings"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-slack/database"
)
//go:embed resources/emoji.json
var emojiFileData []byte
var emojis map[string]string
var re regexp.Regexp = *regexp.MustCompile(`:[^:\s]*:`)
func replaceShortcodesWithEmojis(text string) string {
return re.ReplaceAllStringFunc(text, shortcodeToEmoji)
}
func convertSlackReaction(text string) string {
var converted string
emoji := strings.Split(text, "::")
for _, e := range emoji {
converted += shortcodeToEmoji(e)
}
return converted
}
func shortcodeToEmoji(code string) string {
strippedCode := strings.Trim(code, ":")
emoji, found := emojis[strippedCode]
if found {
return emoji
} else {
return code
}
}
func emojiToShortcode(emoji string) string {
var partCodes []string
for _, r := range withoutVariationSelector(emoji) {
for code, e := range emojis {
if string(r) == withoutVariationSelector(e) {
partCodes = append(partCodes, code)
continue
}
}
}
return strings.Join(partCodes, "::")
}
func withoutVariationSelector(str string) string {
return strings.Map(func(r rune) rune {
if r == '\ufe0f' {
return -1
}
return r
}, str)
}
func init() {
json.Unmarshal(emojiFileData, &emojis)
}
func (br *SlackBridge) ImportEmojis(userTeam *database.UserTeam, list *map[string]string, overwrite bool) error {
if list == nil {
resp, err := userTeam.Client.GetEmoji()
if err != nil {
br.ZLog.Err(err).Msg("failed to fetch emoji list from Slack")
return err
}
list = &resp
}
deferredAliases := map[string]string{}
uploaded := map[string]id.ContentURI{}
for key, url := range *list {
existing := br.DB.Emoji.GetBySlackID(key, userTeam.Key.TeamID)
if existing == nil || overwrite {
if strings.HasPrefix(url, "alias:") {
deferredAliases[key] = strings.TrimPrefix(url, "alias:")
continue
}
uri, err := uploadPlainFile(br.AS.BotIntent(), url)
if err != nil {
br.ZLog.Err(err).Str("url", url).Msg("failed to upload emoji to matrix")
continue
}
uploaded[key] = uri
dbEmoji := br.DB.Emoji.New()
dbEmoji.SlackID = key
dbEmoji.SlackTeam = userTeam.Key.TeamID
dbEmoji.ImageURL = uri
dbEmoji.Upsert(nil)
}
}
for key, alias := range deferredAliases {
if uri, ok := uploaded[alias]; ok {
dbEmoji := br.DB.Emoji.New()
dbEmoji.SlackID = key
dbEmoji.SlackTeam = userTeam.Key.TeamID
dbEmoji.Alias = alias
dbEmoji.ImageURL = uri
dbEmoji.Upsert(nil)
} else if unicode := shortcodeToEmoji(alias); unicode != alias {
dbEmoji := br.DB.Emoji.New()
dbEmoji.SlackID = key
dbEmoji.SlackTeam = userTeam.Key.TeamID
dbEmoji.Alias = unicode
dbEmoji.Upsert(nil)
}
}
return nil
}
func (br *SlackBridge) GetEmoji(shortcode string, userTeam *database.UserTeam) string {
converted := convertSlackReaction(shortcode)
if converted != shortcode {
return converted
}
dbEmoji := br.DB.Emoji.GetBySlackID(shortcode, userTeam.Key.TeamID)
if dbEmoji == nil {
br.ImportEmojis(userTeam, nil, false)
dbEmoji = br.DB.Emoji.GetBySlackID(shortcode, userTeam.Key.TeamID)
}
if dbEmoji != nil && !dbEmoji.ImageURL.IsEmpty() {
return dbEmoji.ImageURL.String()
} else if dbEmoji != nil {
return dbEmoji.Alias
} else {
return shortcode
}
}