deps/mautrix: upgrade to latest bridgev2
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
+12
-110
@@ -9,18 +9,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gotd/td/telegram"
|
||||
"github.com/gotd/td/telegram/message"
|
||||
"github.com/gotd/td/telegram/message/html"
|
||||
"github.com/gotd/td/telegram/updates"
|
||||
"github.com/gotd/td/telegram/uploader"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/rs/zerolog"
|
||||
"go.mau.fi/zerozap"
|
||||
"go.uber.org/zap"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/database"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/event"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/msgconv"
|
||||
)
|
||||
@@ -34,6 +29,8 @@ type TelegramClient struct {
|
||||
msgConv *msgconv.MessageConverter
|
||||
}
|
||||
|
||||
var _ bridgev2.NetworkAPI = (*TelegramClient)(nil)
|
||||
|
||||
func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridgev2.UserLogin) (*TelegramClient, error) {
|
||||
loginID, err := strconv.ParseInt(string(login.ID), 10, 64)
|
||||
if err != nil {
|
||||
@@ -86,8 +83,6 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
||||
return &client, err
|
||||
}
|
||||
|
||||
var _ bridgev2.NetworkAPI = (*TelegramClient)(nil)
|
||||
|
||||
// connectTelegramClient blocks until client is connected, calling Run
|
||||
// internally.
|
||||
// Technique from: https://github.com/gotd/contrib/blob/master/bg/connect.go
|
||||
@@ -162,10 +157,11 @@ func (t *TelegramClient) onUpdateNewMessage(ctx context.Context, e tg.Entities,
|
||||
},
|
||||
ID: makeMessageID(msg.ID),
|
||||
Sender: sender,
|
||||
PortalID: makePortalID(msg.PeerID),
|
||||
PortalKey: makePortalID(msg.PeerID),
|
||||
Data: msg,
|
||||
CreatePortal: true,
|
||||
ConvertMessageFunc: t.msgConv.ToMatrix,
|
||||
Timestamp: time.Unix(int64(msg.Date), 0),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
@@ -180,6 +176,10 @@ func (t *TelegramClient) Connect(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (t *TelegramClient) Disconnect() {
|
||||
t.clientCancel()
|
||||
}
|
||||
|
||||
func getFullName(user *tg.User) string {
|
||||
return strings.TrimSpace(fmt.Sprintf("%s %s", user.FirstName, user.LastName))
|
||||
}
|
||||
@@ -274,116 +274,18 @@ func (t *TelegramClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.MatrixEdit) error {
|
||||
panic("unimplemented edit")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (dbMessage *database.Message, err error) {
|
||||
sender := message.NewSender(t.client.API())
|
||||
peer, err := inputPeerForPortalID(msg.Portal.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
builder := sender.To(peer)
|
||||
|
||||
// TODO handle sticker
|
||||
|
||||
var updates tg.UpdatesClass
|
||||
switch msg.Content.MsgType {
|
||||
case event.MsgText:
|
||||
updates, err = builder.Text(ctx, msg.Content.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case event.MsgImage, event.MsgFile, event.MsgAudio, event.MsgVideo:
|
||||
var filename, caption string
|
||||
if msg.Content.FileName != "" {
|
||||
filename = msg.Content.FileName
|
||||
caption = msg.Content.FormattedBody
|
||||
if caption == "" {
|
||||
caption = msg.Content.Body
|
||||
}
|
||||
} else {
|
||||
filename = msg.Content.Body
|
||||
}
|
||||
|
||||
// TODO stream this download straight into the uploader
|
||||
fileData, err := t.main.Bridge.Bot.DownloadMedia(ctx, msg.Content.URL, msg.Content.File)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download media from Matrix: %w", err)
|
||||
}
|
||||
uploader := uploader.NewUploader(t.client.API())
|
||||
upload, err := uploader.FromBytes(ctx, filename, fileData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to upload media to Telegram: %w", err)
|
||||
}
|
||||
var photo *message.UploadedPhotoBuilder
|
||||
if caption != "" {
|
||||
// TODO resolver?
|
||||
photo = message.UploadedPhoto(upload, html.String(nil, caption))
|
||||
} else {
|
||||
photo = message.UploadedPhoto(upload)
|
||||
}
|
||||
updates, err = builder.Media(ctx, photo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var tgMessageID, tgDate int
|
||||
switch sentMessage := updates.(type) {
|
||||
case *tg.UpdateShortSentMessage:
|
||||
tgMessageID = sentMessage.ID
|
||||
tgDate = sentMessage.Date
|
||||
case *tg.Updates:
|
||||
tgDate = sentMessage.Date
|
||||
for _, u := range sentMessage.Updates {
|
||||
if update, ok := u.(*tg.UpdateMessageID); ok {
|
||||
tgMessageID = update.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
if tgMessageID == 0 {
|
||||
return nil, fmt.Errorf("couldn't find update message ID update")
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown update from message response %T", updates)
|
||||
}
|
||||
|
||||
dbMessage = &database.Message{
|
||||
ID: makeMessageID(tgMessageID),
|
||||
MXID: msg.Event.ID,
|
||||
RoomID: msg.Portal.ID,
|
||||
SenderID: makeUserID(t.loginID),
|
||||
Timestamp: time.Unix(int64(tgDate), 0),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) error {
|
||||
panic("unimplemented remove")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (emojiID networkid.EmojiID, err error) {
|
||||
panic("unimplemented reaction")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error {
|
||||
panic("unimplemented reaction remove")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) IsLoggedIn() bool {
|
||||
_, err := t.client.Self(context.TODO())
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (t *TelegramClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool {
|
||||
return userID == networkid.UserID(t.userLogin.ID)
|
||||
}
|
||||
|
||||
func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
||||
_, err := t.client.API().AuthLogOut(ctx)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Err(err).Msg("failed to logout on Telegram")
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TelegramClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool {
|
||||
return userID == networkid.UserID(t.userLogin.ID)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
|
||||
up "go.mau.fi/util/configupgrade"
|
||||
"go.mau.fi/util/dbutil"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
|
||||
@@ -38,6 +41,9 @@ type TelegramConnector struct {
|
||||
}
|
||||
|
||||
var _ bridgev2.NetworkConnector = (*TelegramConnector)(nil)
|
||||
var _ bridgev2.ConfigValidatingNetwork = (*TelegramConnector)(nil)
|
||||
|
||||
// var _ bridgev2.MaxFileSizeingNetwork = (*TelegramConnector)(nil)
|
||||
|
||||
func NewConnector() *TelegramConnector {
|
||||
return &TelegramConnector{
|
||||
@@ -59,3 +65,40 @@ func (tc *TelegramConnector) LoadUserLogin(ctx context.Context, login *bridgev2.
|
||||
login.Client, err = NewTelegramClient(ctx, tc, login)
|
||||
return
|
||||
}
|
||||
|
||||
//go:embed example-config.yaml
|
||||
var ExampleConfig string
|
||||
|
||||
func upgradeConfig(helper up.Helper) {
|
||||
helper.Copy(up.Int, "app_id")
|
||||
helper.Copy(up.Str, "app_hash")
|
||||
}
|
||||
|
||||
func (tg *TelegramConnector) GetConfig() (example string, data any, upgrader up.Upgrader) {
|
||||
return ExampleConfig, tg.Config, up.SimpleUpgrader(upgradeConfig)
|
||||
}
|
||||
|
||||
func (tg *TelegramConnector) ValidateConfig() error {
|
||||
if tg.Config.AppID == 0 {
|
||||
return fmt.Errorf("app_id is required")
|
||||
}
|
||||
if tg.Config.AppHash == "" {
|
||||
return fmt.Errorf("app_hash is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO
|
||||
// func (tg *TelegramConnector) SetMaxFileSize(maxSize int64) {
|
||||
// }
|
||||
|
||||
func (tg *TelegramConnector) GetName() bridgev2.BridgeName {
|
||||
return bridgev2.BridgeName{
|
||||
DisplayName: "Telegram",
|
||||
NetworkURL: "https://telegram.org/",
|
||||
NetworkIcon: "mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX",
|
||||
NetworkID: "telegram",
|
||||
BeeperBridgeType: "telegram",
|
||||
DefaultPort: 29317,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
# Get your own API keys at https://my.telegram.org/apps
|
||||
app_id: 12345
|
||||
app_hash: tjyd5yge35lbodk1xwzw2jstp90k55qz
|
||||
# (Optional) Create your own bot at https://t.me/BotFather
|
||||
bot_token: disabled
|
||||
|
||||
# Should the bridge request missed updates from Telegram when restarting?
|
||||
catch_up: true
|
||||
# Should incoming updates be handled sequentially to make sure order is preserved on Matrix?
|
||||
sequential_updates: true
|
||||
exit_on_update_error: false
|
||||
|
||||
# Telethon connection options.
|
||||
connection:
|
||||
# The timeout in seconds to be used when connecting.
|
||||
timeout: 120
|
||||
# How many times the reconnection should retry, either on the initial connection or when
|
||||
# Telegram disconnects us. May be set to a negative or null value for infinite retries, but
|
||||
# this is not recommended, since the program can get stuck in an infinite loop.
|
||||
retries: 5
|
||||
# The delay in seconds to sleep between automatic reconnections.
|
||||
retry_delay: 1
|
||||
# The threshold below which the library should automatically sleep on flood wait errors
|
||||
# (inclusive). For instance, if a FloodWaitError for 17s occurs and flood_sleep_threshold
|
||||
# is 20s, the library will sleep automatically. If the error was for 21s, it would raise
|
||||
# the error instead. Values larger than a day (86400) will be changed to a day.
|
||||
flood_sleep_threshold: 60
|
||||
# How many times a request should be retried. Request are retried when Telegram is having
|
||||
# internal issues, when there is a FloodWaitError less than flood_sleep_threshold, or when
|
||||
# there's a migrate error. May take a negative or null value for infinite retries, but this
|
||||
# is not recommended, since some requests can always trigger a call fail (such as searching
|
||||
# for messages).
|
||||
request_retries: 5
|
||||
# Use IPv6 for Telethon connection
|
||||
use_ipv6: false
|
||||
|
||||
# Device info sent to Telegram.
|
||||
device_info:
|
||||
# "auto" = OS name+version.
|
||||
device_model: mautrix-telegram
|
||||
# "auto" = Telethon version.
|
||||
system_version: auto
|
||||
# "auto" = mautrix-telegram version.
|
||||
app_version: auto
|
||||
lang_code: en
|
||||
system_lang_code: en
|
||||
|
||||
# Custom server to connect to.
|
||||
server:
|
||||
# Set to true to use these server settings. If false, will automatically
|
||||
# use production server assigned by Telegram. Set to false in production.
|
||||
enabled: false
|
||||
# The DC ID to connect to.
|
||||
dc: 2
|
||||
# The IP to connect to.
|
||||
ip: 149.154.167.40
|
||||
# The port to connect to. 443 may not work, 80 is better and both are equally secure.
|
||||
port: 80
|
||||
|
||||
# Telethon proxy configuration.
|
||||
# You must install PySocks from pip for proxies to work.
|
||||
proxy:
|
||||
# Allowed types: disabled, socks4, socks5, http, mtproxy
|
||||
type: disabled
|
||||
# Proxy IP address and port.
|
||||
address: 127.0.0.1
|
||||
port: 1080
|
||||
# Whether or not to perform DNS resolving remotely. Only for socks/http proxies.
|
||||
rdns: true
|
||||
# Proxy authentication (optional). Put MTProxy secret in password field.
|
||||
username: ""
|
||||
password: ""
|
||||
@@ -33,14 +33,14 @@ const (
|
||||
peerTypeChannel peerType = "channel"
|
||||
)
|
||||
|
||||
func makePortalID(peer tg.PeerClass) networkid.PortalID {
|
||||
func makePortalID(peer tg.PeerClass) networkid.PortalKey {
|
||||
switch v := peer.(type) {
|
||||
case *tg.PeerUser:
|
||||
return networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeUser, v.UserID))
|
||||
return networkid.PortalKey{ID: networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeUser, v.UserID))}
|
||||
case *tg.PeerChat:
|
||||
return networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeChat, v.ChatID))
|
||||
return networkid.PortalKey{ID: networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeChat, v.ChatID))}
|
||||
case *tg.PeerChannel:
|
||||
return networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeChannel, v.ChannelID))
|
||||
return networkid.PortalKey{ID: networkid.PortalID(fmt.Sprintf("%s:%d", peerTypeChannel, v.ChannelID))}
|
||||
default:
|
||||
panic(fmt.Errorf("unknown peer class type %T", v))
|
||||
}
|
||||
|
||||
@@ -190,8 +190,10 @@ func (p *PhoneLogin) handleAuthSuccess(ctx context.Context, authorization *tg.Au
|
||||
userLoginID := makeUserLoginID(authorization.User.GetID())
|
||||
ul, err := p.user.NewLogin(ctx, &database.UserLogin{
|
||||
ID: userLoginID,
|
||||
Metadata: map[string]any{
|
||||
"phone": p.phone,
|
||||
Metadata: database.UserLoginMetadata{
|
||||
Extra: map[string]any{
|
||||
"phone": p.phone,
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gotd/td/telegram/message"
|
||||
"github.com/gotd/td/telegram/message/html"
|
||||
"github.com/gotd/td/telegram/uploader"
|
||||
"github.com/gotd/td/tg"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/database"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/event"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
|
||||
)
|
||||
|
||||
func (t *TelegramClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (resp *bridgev2.MatrixMessageResponse, err error) {
|
||||
sender := message.NewSender(t.client.API())
|
||||
peer, err := ids.InputPeerForPortalID(msg.Portal.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
builder := sender.To(peer)
|
||||
|
||||
// TODO handle sticker
|
||||
|
||||
var updates tg.UpdatesClass
|
||||
switch msg.Content.MsgType {
|
||||
case event.MsgText:
|
||||
updates, err = builder.Text(ctx, msg.Content.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case event.MsgImage, event.MsgFile, event.MsgAudio, event.MsgVideo:
|
||||
var filename, caption string
|
||||
if msg.Content.FileName != "" {
|
||||
filename = msg.Content.FileName
|
||||
caption = msg.Content.FormattedBody
|
||||
if caption == "" {
|
||||
caption = msg.Content.Body
|
||||
}
|
||||
} else {
|
||||
filename = msg.Content.Body
|
||||
}
|
||||
|
||||
// TODO stream this download straight into the uploader
|
||||
fileData, err := t.main.Bridge.Bot.DownloadMedia(ctx, msg.Content.URL, msg.Content.File)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download media from Matrix: %w", err)
|
||||
}
|
||||
uploader := uploader.NewUploader(t.client.API())
|
||||
upload, err := uploader.FromBytes(ctx, filename, fileData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to upload media to Telegram: %w", err)
|
||||
}
|
||||
var photo *message.UploadedPhotoBuilder
|
||||
if caption != "" {
|
||||
// TODO resolver?
|
||||
photo = message.UploadedPhoto(upload, html.String(nil, caption))
|
||||
} else {
|
||||
photo = message.UploadedPhoto(upload)
|
||||
}
|
||||
updates, err = builder.Media(ctx, photo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var tgMessageID, tgDate int
|
||||
switch sentMessage := updates.(type) {
|
||||
case *tg.UpdateShortSentMessage:
|
||||
tgMessageID = sentMessage.ID
|
||||
tgDate = sentMessage.Date
|
||||
case *tg.Updates:
|
||||
tgDate = sentMessage.Date
|
||||
for _, u := range sentMessage.Updates {
|
||||
if update, ok := u.(*tg.UpdateMessageID); ok {
|
||||
tgMessageID = update.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
if tgMessageID == 0 {
|
||||
return nil, fmt.Errorf("couldn't find update message ID update")
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown update from message response %T", updates)
|
||||
}
|
||||
|
||||
resp = &bridgev2.MatrixMessageResponse{
|
||||
DB: &database.Message{
|
||||
ID: ids.MakeMessageID(tgMessageID),
|
||||
MXID: msg.Event.ID,
|
||||
Room: networkid.PortalKey{ID: msg.Portal.ID},
|
||||
SenderID: ids.MakeUserID(t.loginID),
|
||||
Timestamp: time.Unix(int64(tgDate), 0),
|
||||
},
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.MatrixEdit) error {
|
||||
panic("unimplemented edit")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) error {
|
||||
panic("unimplemented remove")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) PreHandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (bridgev2.MatrixReactionPreResponse, error) {
|
||||
panic("pre handle matrix reaction")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (reaction *database.Reaction, err error) {
|
||||
panic("unimplemented reaction")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error {
|
||||
panic("unimplemented reaction remove")
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridgev2.MatrixReadReceipt) error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TelegramClient) HandleMatrixTyping(ctx context.Context, msg *bridgev2.MatrixTyping) error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gotd/td/telegram/downloader"
|
||||
"github.com/gotd/td/tg"
|
||||
@@ -20,9 +19,7 @@ func (mc *MessageConverter) ToMatrix(ctx context.Context, portal *bridgev2.Porta
|
||||
log := zerolog.Ctx(ctx).With().Str("conversion_direction", "to_matrix").Logger()
|
||||
ctx = log.WithContext(ctx)
|
||||
|
||||
cm := &bridgev2.ConvertedMessage{
|
||||
Timestamp: time.Unix(int64(msg.Date), 0),
|
||||
}
|
||||
cm := &bridgev2.ConvertedMessage{}
|
||||
if msg.Message != "" {
|
||||
cm.Parts = append(cm.Parts, &bridgev2.ConvertedMessagePart{
|
||||
ID: networkid.PartID("caption"),
|
||||
|
||||
Reference in New Issue
Block a user