emojis: properly handle inline emojis on local

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2025-03-04 11:27:09 -07:00
parent ebc1aa05b1
commit dcc8689835
6 changed files with 30 additions and 34 deletions
+4 -17
View File
@@ -19,7 +19,6 @@ package telegramfmt
import (
"context"
"html"
"strings"
"github.com/gotd/td/tg"
"github.com/rs/zerolog"
@@ -28,6 +27,7 @@ import (
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
)
@@ -37,21 +37,13 @@ type UserInfo struct {
}
type FormatParams struct {
CustomEmojis map[networkid.EmojiID]string
CustomEmojis map[networkid.EmojiID]emojis.EmojiInfo
GetUserInfoByUsername func(ctx context.Context, username string) (UserInfo, error)
GetUserInfoByID func(ctx context.Context, id int64) (UserInfo, error)
NormalizeURL func(ctx context.Context, url string) string
}
func (fp FormatParams) GetCustomEmoji(emojiID networkid.EmojiID) (string, id.ContentURIString) {
if strings.HasPrefix(fp.CustomEmojis[emojiID], "mxc://") {
return "", id.ContentURIString(fp.CustomEmojis[emojiID])
} else {
return fp.CustomEmojis[emojiID], ""
}
}
func (fp FormatParams) WithCustomEmojis(emojis map[networkid.EmojiID]string) FormatParams {
func (fp FormatParams) WithCustomEmojis(emojis map[networkid.EmojiID]emojis.EmojiInfo) FormatParams {
return FormatParams{
CustomEmojis: emojis,
GetUserInfoByUsername: fp.GetUserInfoByUsername,
@@ -140,12 +132,7 @@ func Parse(ctx context.Context, message string, entities []tg.MessageEntityClass
case *tg.MessageEntitySpoiler:
br.Value = Style{Type: StyleSpoiler}
case *tg.MessageEntityCustomEmoji:
emoji, contentURI := params.GetCustomEmoji(ids.MakeEmojiIDFromDocumentID(entity.DocumentID))
if emoji != "" {
br.Value = Style{Type: StyleCustomEmoji, Emoji: emoji}
} else {
br.Value = Style{Type: StyleCustomEmoji, EmojiURI: contentURI}
}
br.Value = Style{Type: StyleCustomEmoji, EmojiInfo: params.CustomEmojis[ids.MakeEmojiIDFromDocumentID(entity.DocumentID)]}
case *tg.MessageEntityBlockquote:
br.Value = Style{Type: StyleBlockquote}
}
+3 -3
View File
@@ -69,12 +69,12 @@ func (s Style) Format(message string) string {
}
return fmt.Sprintf(`<a href='%s'>%s</a>`, s.URL, message)
case StyleCustomEmoji:
if s.Emoji != "" {
return s.Emoji
if s.EmojiInfo.Emoji != "" {
return s.EmojiInfo.Emoji
} else {
return fmt.Sprintf(
`<img data-mx-emoticon data-mau-animated-emoji src="%s" height="32" width="32" alt="%s" title="%s"/>`,
s.EmojiURI, message, message,
s.EmojiInfo.EmojiURI, message, message,
)
}
case StyleBotCommand:
+4 -6
View File
@@ -20,7 +20,8 @@ import (
"fmt"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
)
type BodyRangeValue interface {
@@ -125,11 +126,8 @@ type Style struct {
// URL is the URL to link to, if applicable.
URL string
// Emoji is the emoji to display, if applicable.
Emoji string
// EmojiURI is the URI to the emoji, if applicable.
EmojiURI id.ContentURIString
// EmojiInfo is the emoji to display, if applicable.
EmojiInfo emojis.EmojiInfo
}
func (s Style) String() string {