reactions: support unicode custom emojis
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
!provisioning-spec.yaml
|
!provisioning-spec.yaml
|
||||||
|
|
||||||
*.json
|
*.json
|
||||||
|
!pkg/connector/emojis/unicodemojipack.json
|
||||||
*.db*
|
*.db*
|
||||||
*.log
|
*.log
|
||||||
*.bak
|
*.bak
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package emojis
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
|
|
||||||
|
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed unicodemojipack.json
|
||||||
|
var unicodemojiPackJSON []byte
|
||||||
|
|
||||||
|
var unicodemojiPack = map[string]int64{}
|
||||||
|
var reverseUnicodemojiPack = map[int64]string{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := json.Unmarshal(unicodemojiPackJSON, &unicodemojiPack); err != nil {
|
||||||
|
panic("Failed to unmarshal unicodemojipack")
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range unicodemojiPack {
|
||||||
|
reverseUnicodemojiPack[v] = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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{}
|
||||||
|
for _, e := range emojiIDs {
|
||||||
|
if v, ok := reverseUnicodemojiPack[e]; ok {
|
||||||
|
emojiID := ids.MakeEmojiIDFromDocumentID(e)
|
||||||
|
result[emojiID] = v
|
||||||
|
} else {
|
||||||
|
remaining = append(remaining, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -13,6 +13,7 @@ import (
|
|||||||
"maunium.net/go/mautrix/bridgev2/database"
|
"maunium.net/go/mautrix/bridgev2/database"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
|
|
||||||
|
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/util"
|
"go.mau.fi/mautrix-telegram/pkg/connector/util"
|
||||||
)
|
)
|
||||||
@@ -283,8 +284,7 @@ func (t *TelegramClient) handleTelegramReactions(ctx context.Context, msg *tg.Me
|
|||||||
if e, ok := reaction.Reaction.(*tg.ReactionCustomEmoji); ok {
|
if e, ok := reaction.Reaction.(*tg.ReactionCustomEmoji); ok {
|
||||||
customEmojiIDs = append(customEmojiIDs, e.DocumentID)
|
customEmojiIDs = append(customEmojiIDs, e.DocumentID)
|
||||||
} else if reaction.Reaction.TypeID() != tg.ReactionEmojiTypeID {
|
} else if reaction.Reaction.TypeID() != tg.ReactionEmojiTypeID {
|
||||||
// We don't know how to process this type of emoji
|
return fmt.Errorf("unknown reaction type %T", reaction.Reaction)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p, ok := reaction.PeerID.(*tg.PeerUser); !ok {
|
if p, ok := reaction.PeerID.(*tg.PeerUser); !ok {
|
||||||
@@ -325,9 +325,20 @@ func (t *TelegramClient) getReactionLimit(ctx context.Context, sender networkid.
|
|||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO move this to emojis package
|
||||||
|
func (t *TelegramClient) transferEmojisToMatrix(ctx context.Context, customEmojiIDs []int64) (result map[networkid.EmojiID]string, err error) {
|
||||||
|
result, customEmojiIDs = emojis.ConvertKnownEmojis(customEmojiIDs)
|
||||||
|
fmt.Printf("leftover custom emoji ids %+v\n", customEmojiIDs)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) handleTelegramParsedReactionsLocked(ctx context.Context, msg *database.Message, reactions map[networkid.UserID][]tg.MessagePeerReaction, customEmojiIDs []int64, isFull bool, onlyUserID *networkid.UserID, timestamp *time.Time) error {
|
func (t *TelegramClient) handleTelegramParsedReactionsLocked(ctx context.Context, msg *database.Message, reactions map[networkid.UserID][]tg.MessagePeerReaction, customEmojiIDs []int64, isFull bool, onlyUserID *networkid.UserID, timestamp *time.Time) error {
|
||||||
// TODO deal with the custom emoji IDs
|
// TODO deal with the custom emoji IDs
|
||||||
fmt.Printf("custom emoji IDs %v\n", customEmojiIDs)
|
fmt.Printf("custom emoji IDs %v\n", customEmojiIDs)
|
||||||
|
customEmojis, err := t.transferEmojisToMatrix(ctx, customEmojiIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
existingReactions, err := t.main.Bridge.DB.Reaction.GetAllToMessage(ctx, msg.ID)
|
existingReactions, err := t.main.Bridge.DB.Reaction.GetAllToMessage(ctx, msg.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -364,12 +375,14 @@ func (t *TelegramClient) handleTelegramParsedReactionsLocked(ctx context.Context
|
|||||||
for _, reaction := range reactions {
|
for _, reaction := range reactions {
|
||||||
var emojiID networkid.EmojiID
|
var emojiID networkid.EmojiID
|
||||||
var emoji string
|
var emoji string
|
||||||
if rce, ok := reaction.Reaction.(*tg.ReactionCustomEmoji); ok {
|
if r, ok := reaction.Reaction.(*tg.ReactionCustomEmoji); ok {
|
||||||
emojiID = ids.MakeEmojiIDFromDocumentID(rce.DocumentID)
|
emojiID = ids.MakeEmojiIDFromDocumentID(r.DocumentID)
|
||||||
emoji = "custom" // TODO
|
emoji = customEmojis[emojiID]
|
||||||
} else if r, ok := reaction.Reaction.(*tg.ReactionEmoji); ok {
|
} else if r, ok := reaction.Reaction.(*tg.ReactionEmoji); ok {
|
||||||
emojiID = ids.MakeEmojiIDFromEmoticon(r.Emoticon)
|
emojiID = ids.MakeEmojiIDFromEmoticon(r.Emoticon)
|
||||||
emoji = r.Emoticon
|
emoji = r.Emoticon
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("invalid reaction type %T", reaction.Reaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
evt := &bridgev2.SimpleRemoteEvent[any]{
|
evt := &bridgev2.SimpleRemoteEvent[any]{
|
||||||
|
|||||||
Reference in New Issue
Block a user