matrixfmt: convert matrix.to links in other direction too
This commit is contained in:
+3
-24
@@ -40,7 +40,6 @@ import (
|
|||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
"maunium.net/go/mautrix/bridgev2/simplevent"
|
"maunium.net/go/mautrix/bridgev2/simplevent"
|
||||||
"maunium.net/go/mautrix/bridgev2/status"
|
"maunium.net/go/mautrix/bridgev2/status"
|
||||||
"maunium.net/go/mautrix/id"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/humanise"
|
"go.mau.fi/mautrix-telegram/pkg/connector/humanise"
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||||
@@ -348,29 +347,9 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
client.matrixParser = &matrixfmt.HTMLParser{
|
client.matrixParser = &matrixfmt.HTMLParser{
|
||||||
Store: tc.Store,
|
Store: tc.Store,
|
||||||
GetGhostDetails: func(ctx context.Context, portal *bridgev2.Portal, ui id.UserID) (networkid.UserID, string, int64, bool) {
|
Bridge: tc.Bridge,
|
||||||
userID, ok := tc.Bridge.Matrix.ParseGhostMXID(ui)
|
ScopedStore: client.ScopedStore,
|
||||||
if !ok {
|
|
||||||
user, err := tc.Bridge.GetExistingUserByMXID(ctx, ui)
|
|
||||||
if err != nil || user == nil {
|
|
||||||
return "", "", 0, false
|
|
||||||
} else if login, _, _ := portal.FindPreferredLogin(ctx, user, false); login != nil {
|
|
||||||
userID = ids.UserLoginIDToUserID(login.ID)
|
|
||||||
} else {
|
|
||||||
return "", "", 0, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if peerType, telegramUserID, err := ids.ParseUserID(userID); err != nil {
|
|
||||||
return "", "", 0, false
|
|
||||||
} else if accessHash, err := client.ScopedStore.GetAccessHash(ctx, peerType, telegramUserID); err != nil || accessHash == 0 {
|
|
||||||
return "", "", 0, false
|
|
||||||
} else if username, err := client.main.Store.Username.Get(ctx, peerType, telegramUserID); err != nil {
|
|
||||||
return "", "", 0, false
|
|
||||||
} else {
|
|
||||||
return userID, username, accessHash, true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client, err
|
return &client, err
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
@@ -31,6 +32,7 @@ import (
|
|||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
|
"go.mau.fi/mautrix-telegram/pkg/connector/emojis"
|
||||||
|
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/store"
|
"go.mau.fi/mautrix-telegram/pkg/connector/store"
|
||||||
"go.mau.fi/mautrix-telegram/pkg/connector/telegramfmt"
|
"go.mau.fi/mautrix-telegram/pkg/connector/telegramfmt"
|
||||||
)
|
)
|
||||||
@@ -258,8 +260,9 @@ func (ctx Context) WithIncrementedListDepth() Context {
|
|||||||
|
|
||||||
// HTMLParser is a somewhat customizable Matrix HTML parser.
|
// HTMLParser is a somewhat customizable Matrix HTML parser.
|
||||||
type HTMLParser struct {
|
type HTMLParser struct {
|
||||||
GetGhostDetails func(context.Context, *bridgev2.Portal, id.UserID) (networkid.UserID, string, int64, bool)
|
Bridge *bridgev2.Bridge
|
||||||
Store *store.Container
|
Store *store.Container
|
||||||
|
ScopedStore *store.ScopedStore
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaggedString is a string that also contains a HTML tag.
|
// TaggedString is a string that also contains a HTML tag.
|
||||||
@@ -373,13 +376,38 @@ func (parser *HTMLParser) headerToString(node *html.Node, ctx Context) *EntitySt
|
|||||||
return NewEntityString(prefix).Append(parser.nodeToString(node.FirstChild, ctx)).Format(telegramfmt.Style{Type: telegramfmt.StyleBold})
|
return NewEntityString(prefix).Append(parser.nodeToString(node.FirstChild, ctx)).Format(telegramfmt.Style{Type: telegramfmt.StyleBold})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (parser *HTMLParser) getGhostDetails(ctx context.Context, portal *bridgev2.Portal, ui id.UserID) (networkid.UserID, string, int64, bool) {
|
||||||
|
userID, ok := parser.Bridge.Matrix.ParseGhostMXID(ui)
|
||||||
|
if !ok {
|
||||||
|
user, err := parser.Bridge.GetExistingUserByMXID(ctx, ui)
|
||||||
|
if err != nil || user == nil {
|
||||||
|
return "", "", 0, false
|
||||||
|
} else if login, _, _ := portal.FindPreferredLogin(ctx, user, false); login != nil {
|
||||||
|
userID = ids.UserLoginIDToUserID(login.ID)
|
||||||
|
} else {
|
||||||
|
return "", "", 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if peerType, telegramUserID, err := ids.ParseUserID(userID); err != nil {
|
||||||
|
return "", "", 0, false
|
||||||
|
} else if accessHash, err := parser.ScopedStore.GetAccessHash(ctx, peerType, telegramUserID); err != nil || accessHash == 0 {
|
||||||
|
return "", "", 0, false
|
||||||
|
} else if username, err := parser.Store.Username.Get(ctx, peerType, telegramUserID); err != nil {
|
||||||
|
return "", "", 0, false
|
||||||
|
} else {
|
||||||
|
return userID, username, accessHash, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (parser *HTMLParser) linkToString(node *html.Node, ctx Context) *EntityString {
|
func (parser *HTMLParser) linkToString(node *html.Node, ctx Context) *EntityString {
|
||||||
str := parser.nodeToTagAwareString(node.FirstChild, ctx)
|
str := parser.nodeToTagAwareString(node.FirstChild, ctx)
|
||||||
href := parser.getAttribute(node, "href")
|
href := parser.getAttribute(node, "href")
|
||||||
if len(href) == 0 {
|
if len(href) == 0 {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
ent := NewEntityString(str.String.String())
|
linkText := str.String.String()
|
||||||
|
linkTextEnt := NewEntityString(linkText)
|
||||||
|
isRawLink := linkText == href
|
||||||
|
|
||||||
parsedMatrix, err := id.ParseMatrixURIOrMatrixToURL(href)
|
parsedMatrix, err := id.ParseMatrixURIOrMatrixToURL(href)
|
||||||
if err == nil && parsedMatrix != nil && parsedMatrix.Sigil1 == '@' {
|
if err == nil && parsedMatrix != nil && parsedMatrix.Sigil1 == '@' {
|
||||||
@@ -388,19 +416,37 @@ func (parser *HTMLParser) linkToString(node *html.Node, ctx Context) *EntityStri
|
|||||||
// Mention not allowed, use name as-is
|
// Mention not allowed, use name as-is
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
userID, username, accessHash, ok := parser.GetGhostDetails(ctx.Ctx, ctx.Portal, mxid)
|
userID, username, accessHash, ok := parser.getGhostDetails(ctx.Ctx, ctx.Portal, mxid)
|
||||||
if !ok {
|
if !ok {
|
||||||
return str
|
return str
|
||||||
} else if username == "" {
|
} else if username == "" {
|
||||||
return ent.Format(telegramfmt.Mention{UserID: userID, AccessHash: accessHash})
|
return linkTextEnt.Format(telegramfmt.Mention{UserID: userID, AccessHash: accessHash})
|
||||||
} else {
|
} else {
|
||||||
return NewEntityString("@" + username).Format(telegramfmt.Mention{UserID: userID, Username: username})
|
return NewEntityString("@" + username).Format(telegramfmt.Mention{UserID: userID, Username: username})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if str.String.String() == href {
|
if parsedMatrix != nil && parsedMatrix.Sigil1 == '!' && parsedMatrix.Sigil2 == '$' {
|
||||||
return ent.Format(telegramfmt.Style{Type: telegramfmt.StyleURL, URL: href})
|
msg, err := parser.Bridge.DB.Message.GetPartByMXID(ctx.Ctx, parsedMatrix.EventID())
|
||||||
|
if err != nil {
|
||||||
|
zerolog.Ctx(ctx.Ctx).Err(err).Msg("Failed to get message for event ID in link")
|
||||||
|
} else if msg != nil {
|
||||||
|
_, chatID, topicID, _ := ids.ParsePortalID(msg.Room.ID)
|
||||||
|
_, msgID, _ := ids.ParseMessageID(msg.ID)
|
||||||
|
if msgID != 0 && chatID != 0 {
|
||||||
|
href = fmt.Sprintf("https://t.me/c/%d/%d", chatID, msgID)
|
||||||
|
if topicID > 0 {
|
||||||
|
href = fmt.Sprintf("https://t.me/c/%d/%d/%d", chatID, topicID, msgID)
|
||||||
|
}
|
||||||
|
if isRawLink {
|
||||||
|
linkTextEnt = NewEntityString(href)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isRawLink {
|
||||||
|
return linkTextEnt.Format(telegramfmt.Style{Type: telegramfmt.StyleURL, URL: href})
|
||||||
} else {
|
} else {
|
||||||
return ent.Format(telegramfmt.Style{Type: telegramfmt.StyleTextURL, URL: href})
|
return linkTextEnt.Format(telegramfmt.Style{Type: telegramfmt.StyleTextURL, URL: href})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user