emojis: properly handle inline emojis on local
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||
)
|
||||
@@ -27,11 +28,11 @@ func init() {
|
||||
|
||||
// ConvertKnownEmojis converts known document IDs from the unicode emoji pack
|
||||
// to the corresponding unicode string and returns the remaining IDs.
|
||||
func ConvertKnownEmojis(emojiIDs []int64) (result map[networkid.EmojiID]string, remaining []int64) {
|
||||
result = map[networkid.EmojiID]string{}
|
||||
func ConvertKnownEmojis(emojiIDs []int64) (result map[networkid.EmojiID]EmojiInfo, remaining []int64) {
|
||||
result = map[networkid.EmojiID]EmojiInfo{}
|
||||
for _, e := range emojiIDs {
|
||||
if v, ok := reverseUnicodemojiPack[e]; ok {
|
||||
result[ids.MakeEmojiIDFromDocumentID(e)] = v
|
||||
result[ids.MakeEmojiIDFromDocumentID(e)] = EmojiInfo{Emoji: v}
|
||||
} else {
|
||||
remaining = append(remaining, e)
|
||||
}
|
||||
@@ -43,3 +44,9 @@ func GetEmojiDocumentID(emoji string) (int64, bool) {
|
||||
id, ok := unicodemojiPack[emoji]
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// EmojiInfo contains information about an emoji.
|
||||
type EmojiInfo struct {
|
||||
Emoji string
|
||||
EmojiURI id.ContentURIString
|
||||
}
|
||||
|
||||
@@ -11,10 +11,11 @@ import (
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/bridgev2/simplevent"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||
)
|
||||
|
||||
func (t *TelegramClient) computeReactionsList(ctx context.Context, peer tg.PeerClass, msgID int, msgReactions tg.MessageReactions) (reactions []tg.MessagePeerReaction, isFull bool, customEmojis map[networkid.EmojiID]string, err error) {
|
||||
func (t *TelegramClient) computeReactionsList(ctx context.Context, peer tg.PeerClass, msgID int, msgReactions tg.MessageReactions) (reactions []tg.MessagePeerReaction, isFull bool, customEmojis map[networkid.EmojiID]emojis.EmojiInfo, err error) {
|
||||
log := zerolog.Ctx(ctx).With().Str("fn", "computeReactionsList").Logger()
|
||||
var totalCount int
|
||||
for _, r := range msgReactions.Results {
|
||||
@@ -72,10 +73,13 @@ func (t *TelegramClient) computeReactionsList(ctx context.Context, peer tg.PeerC
|
||||
return reactionsList, len(reactionsList) == totalCount, customEmojis, err
|
||||
}
|
||||
|
||||
func computeEmojiAndID(reaction tg.ReactionClass, customEmojis map[networkid.EmojiID]string) (emojiID networkid.EmojiID, emoji string, err error) {
|
||||
func computeEmojiAndID(reaction tg.ReactionClass, customEmojis map[networkid.EmojiID]emojis.EmojiInfo) (emojiID networkid.EmojiID, emoji string, err error) {
|
||||
if r, ok := reaction.(*tg.ReactionCustomEmoji); ok {
|
||||
emojiID = ids.MakeEmojiIDFromDocumentID(r.DocumentID)
|
||||
emoji = customEmojis[emojiID]
|
||||
emoji = customEmojis[emojiID].Emoji
|
||||
if emoji == "" {
|
||||
emoji = string(customEmojis[emojiID].EmojiURI)
|
||||
}
|
||||
} else if r, ok := reaction.(*tg.ReactionEmoji); ok {
|
||||
emojiID = ids.MakeEmojiIDFromEmoticon(r.Emoticon)
|
||||
emoji = r.Emoticon
|
||||
|
||||
@@ -960,7 +960,7 @@ func (t *TelegramClient) getAvailableReactions(ctx context.Context) (map[string]
|
||||
return t.availableReactions, nil
|
||||
}
|
||||
|
||||
func (t *TelegramClient) transferEmojisToMatrix(ctx context.Context, customEmojiIDs []int64) (result map[networkid.EmojiID]string, err error) {
|
||||
func (t *TelegramClient) transferEmojisToMatrix(ctx context.Context, customEmojiIDs []int64) (result map[networkid.EmojiID]emojis.EmojiInfo, err error) {
|
||||
result, customEmojiIDs = emojis.ConvertKnownEmojis(customEmojiIDs)
|
||||
|
||||
if len(customEmojiIDs) == 0 {
|
||||
@@ -980,7 +980,7 @@ func (t *TelegramClient) transferEmojisToMatrix(ctx context.Context, customEmoji
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[ids.MakeEmojiIDFromDocumentID(customEmojiDocument.GetID())] = string(mxcURI)
|
||||
result[ids.MakeEmojiIDFromDocumentID(customEmojiDocument.GetID())] = emojis.EmojiInfo{EmojiURI: mxcURI}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user