(telegram|matrix)fmt: mention formatting
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -18,11 +18,11 @@ package telegramfmt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html"
|
||||
"strings"
|
||||
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/rs/zerolog"
|
||||
"golang.org/x/exp/maps"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/event"
|
||||
@@ -37,9 +37,10 @@ type UserInfo struct {
|
||||
}
|
||||
|
||||
type FormatParams struct {
|
||||
CustomEmojis map[networkid.EmojiID]string
|
||||
GetUserInfo func(ctx context.Context, id networkid.UserID) (UserInfo, error)
|
||||
NormalizeURL func(ctx context.Context, url string) string
|
||||
CustomEmojis map[networkid.EmojiID]string
|
||||
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) {
|
||||
@@ -52,9 +53,10 @@ func (fp FormatParams) GetCustomEmoji(emojiID networkid.EmojiID) (string, id.Con
|
||||
|
||||
func (fp FormatParams) WithCustomEmojis(emojis map[networkid.EmojiID]string) FormatParams {
|
||||
return FormatParams{
|
||||
CustomEmojis: emojis,
|
||||
GetUserInfo: fp.GetUserInfo,
|
||||
NormalizeURL: fp.NormalizeURL,
|
||||
CustomEmojis: emojis,
|
||||
GetUserInfoByUsername: fp.GetUserInfoByUsername,
|
||||
GetUserInfoByID: fp.GetUserInfoByID,
|
||||
NormalizeURL: fp.NormalizeURL,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +72,7 @@ func (ctx formatContext) TextToHTML(text string) string {
|
||||
}
|
||||
|
||||
func Parse(ctx context.Context, message string, entities []tg.MessageEntityClass, params FormatParams) (*event.MessageEventContent, error) {
|
||||
log := zerolog.Ctx(ctx).With().Str("func", "Parse").Logger()
|
||||
content := &event.MessageEventContent{
|
||||
MsgType: event.MsgText,
|
||||
Body: message,
|
||||
@@ -90,8 +93,14 @@ func Parse(ctx context.Context, message string, entities []tg.MessageEntityClass
|
||||
}.TruncateEnd(maxLength)
|
||||
switch entity := e.(type) {
|
||||
case *tg.MessageEntityMention:
|
||||
// TODO
|
||||
fmt.Printf("mention = %+v\n", entity)
|
||||
username := utf16Message[e.GetOffset()+1 : e.GetOffset()+e.GetLength()].String()
|
||||
userInfo, err := params.GetUserInfoByUsername(ctx, username)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("username", username).Msg("Failed to get user info for mention")
|
||||
continue // Skip this mention
|
||||
}
|
||||
mentions[userInfo.MXID] = struct{}{}
|
||||
br.Value = Mention{UserInfo: userInfo, Username: username}
|
||||
case *tg.MessageEntityHashtag:
|
||||
br.Value = Style{Type: StyleHashtag}
|
||||
case *tg.MessageEntityBotCommand:
|
||||
@@ -111,13 +120,13 @@ func Parse(ctx context.Context, message string, entities []tg.MessageEntityClass
|
||||
case *tg.MessageEntityTextURL:
|
||||
br.Value = Style{Type: StyleURL, URL: params.NormalizeURL(ctx, entity.URL)}
|
||||
case *tg.MessageEntityMentionName:
|
||||
userID := ids.MakeUserID(entity.UserID)
|
||||
userInfo, err := params.GetUserInfo(ctx, userID)
|
||||
userInfo, err := params.GetUserInfoByID(ctx, entity.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
log.Warn().Err(err).Int64("user_id", entity.UserID).Msg("Failed to get user info for mention")
|
||||
continue // Skip this mention
|
||||
}
|
||||
mentions[userInfo.MXID] = struct{}{}
|
||||
br.Value = Mention{UserInfo: userInfo, UserID: userID}
|
||||
br.Value = Mention{UserInfo: userInfo}
|
||||
case *tg.MessageEntityPhone:
|
||||
br.Value = Style{Type: StylePhone}
|
||||
case *tg.MessageEntityCashtag:
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
@@ -33,15 +32,15 @@ import (
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
formatParams := telegramfmt.FormatParams{
|
||||
GetUserInfo: func(ctx context.Context, userID networkid.UserID) (telegramfmt.UserInfo, error) {
|
||||
if userID == "real" {
|
||||
GetUserInfoByID: func(ctx context.Context, userID int64) (telegramfmt.UserInfo, error) {
|
||||
if userID == 1 {
|
||||
return telegramfmt.UserInfo{
|
||||
MXID: "@test:example.com",
|
||||
Name: "Matrix User",
|
||||
}, nil
|
||||
} else {
|
||||
return telegramfmt.UserInfo{
|
||||
MXID: id.UserID(fmt.Sprintf("@telegram_%s:example.com", userID)),
|
||||
MXID: id.UserID(fmt.Sprintf("@telegram_%d:example.com", userID)),
|
||||
Name: "Signal User",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -23,7 +23,11 @@ import (
|
||||
)
|
||||
|
||||
func (m Mention) Format(message string) string {
|
||||
return fmt.Sprintf(`<a href="%s">%s</a>`, m.MXID.URI().MatrixToURL(), m.Name)
|
||||
if m.Username != "" {
|
||||
return fmt.Sprintf(`<a href="%s">@%s</a>`, m.MXID.URI().MatrixToURL(), m.Username)
|
||||
} else {
|
||||
return fmt.Sprintf(`<a href="%s">%s</a>`, m.MXID.URI().MatrixToURL(), m.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func (s Style) Format(message string) string {
|
||||
|
||||
@@ -31,13 +31,15 @@ type BodyRangeValue interface {
|
||||
|
||||
type Mention struct {
|
||||
UserInfo
|
||||
UserID networkid.UserID
|
||||
UserID networkid.UserID
|
||||
AccessHash int64
|
||||
Username string
|
||||
}
|
||||
|
||||
var _ BodyRangeValue = Mention{}
|
||||
|
||||
func (m Mention) String() string {
|
||||
return fmt.Sprintf("Mention{MXID: id.UserID(%q), Name: %q}", m.MXID, m.Name)
|
||||
return fmt.Sprintf("Mention{MXID: id.UserID(%q), Username: %q, Name: %q}", m.MXID, m.Username, m.Name)
|
||||
}
|
||||
|
||||
func (m Mention) IsCode() bool {
|
||||
|
||||
Reference in New Issue
Block a user