From e611c87342b958b0d11a2821a9992906ab085093 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 22 Aug 2024 14:47:30 +0300 Subject: [PATCH] all: add some todos and fix small issues --- cmd/mautrix-telegram/main.go | 1 - pkg/connector/chatinfo.go | 5 +++- pkg/connector/client.go | 4 ++- pkg/connector/config.go | 1 + pkg/connector/connector.go | 13 ++++----- pkg/connector/directdownload.go | 2 ++ pkg/connector/geouri.go | 2 ++ pkg/connector/ids/ids.go | 1 - pkg/connector/login.go | 2 ++ pkg/connector/matrixfmt/html.go | 1 + pkg/connector/media/transfer.go | 1 + pkg/connector/store/upgrades/00-latest.sql | 31 +++++++++++----------- pkg/connector/telegram.go | 1 + 13 files changed, 40 insertions(+), 25 deletions(-) diff --git a/cmd/mautrix-telegram/main.go b/cmd/mautrix-telegram/main.go index c40d360e..b190b58a 100644 --- a/cmd/mautrix-telegram/main.go +++ b/cmd/mautrix-telegram/main.go @@ -17,7 +17,6 @@ package main import ( - _ "go.mau.fi/util/dbutil/litestream" "maunium.net/go/mautrix/bridgev2/matrix/mxmain" "go.mau.fi/mautrix-telegram/pkg/connector" diff --git a/pkg/connector/chatinfo.go b/pkg/connector/chatinfo.go index 85abd7cb..896b46e9 100644 --- a/pkg/connector/chatinfo.go +++ b/pkg/connector/chatinfo.go @@ -16,6 +16,7 @@ import ( ) func (t *TelegramClient) getDMChatInfo(ctx context.Context, userID int64) (*bridgev2.ChatInfo, error) { + // FIXME there's no reason for this function to fetch the user info chatInfo := bridgev2.ChatInfo{ Type: ptr.Ptr(database.RoomTypeDM), Members: &bridgev2.ChatMemberList{IsFull: true}, @@ -74,7 +75,7 @@ func (t *TelegramClient) getGroupChatInfo(fullChat *tg.MessagesChatFull, chatID chatInfo := bridgev2.ChatInfo{ Name: name, - Type: ptr.Ptr(database.RoomTypeGroupDM), // TODO Is this correct for channels? + Type: ptr.Ptr(database.RoomTypeDefault), Members: &bridgev2.ChatMemberList{ IsFull: true, MemberMap: map[networkid.UserID]bridgev2.ChatMember{}, @@ -138,6 +139,8 @@ func (t *TelegramClient) filterChannelParticipants(chatParticipants []tg.Channel } func (t *TelegramClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) { + // FIXME GetFullChat should be avoided. Using only bundled info should be preferred whenever possible + // (e.g. when syncing dialogs, only use the data in the dialog list, don't fetch each chat info separately). peerType, id, err := ids.ParsePortalID(portal.ID) if err != nil { return nil, err diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 63ed6663..b875e611 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -90,7 +90,7 @@ func (u UpdateDispatcher) Handle(ctx context.Context, updates tg.UpdatesClass) e return u.UpdateDispatcher.Handle(ctx, updates) } -var messageLinkRegex = regexp.MustCompile(`^https?:\/\/t(?:elegram)?\.(?:me|dog)\/([A-Za-z][A-Za-z0-9_]{3,31}[A-Za-z0-9]|[Cc]\/[0-9]{1,20})\/([0-9]{1,20})$`) +var messageLinkRegex = regexp.MustCompile(`^https?://t(?:elegram)?\.(?:me|dog)/([A-Za-z][A-Za-z0-9_]{3,31}[A-Za-z0-9]|[Cc]/[0-9]{1,20})/([0-9]{1,20})$`) func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridgev2.UserLogin) (*TelegramClient, error) { telegramUserID, err := ids.ParseUserLoginID(login.ID) @@ -197,12 +197,14 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge return telegramfmt.UserInfo{}, err } userInfo := telegramfmt.UserInfo{MXID: ghost.Intent.GetMXID(), Name: ghost.Name} + // FIXME this should look for user logins by ID, not hardcode the current user if id == client.telegramUserID { userInfo.MXID = client.userLogin.UserMXID } return userInfo, nil }, GetUserInfoByUsername: func(ctx context.Context, username string) (telegramfmt.UserInfo, error) { + // FIXME this should just query telegram_user_metadata by username ghosts, err := tc.Bridge.DB.Ghost.GetByMetadata(ctx, "username", username) if err != nil { return telegramfmt.UserInfo{}, err diff --git a/pkg/connector/config.go b/pkg/connector/config.go index 00c5f7a9..aee09ceb 100644 --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -29,6 +29,7 @@ func (c MemberListConfig) NormalizedMaxInitialSync() int { } type TelegramConfig struct { + // FIXME these should be called api_id and api_hash AppID int `yaml:"app_id"` AppHash string `yaml:"app_hash"` diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index 1fcda746..e746913e 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -65,12 +65,13 @@ func (tc *TelegramConnector) LoadUserLogin(ctx context.Context, login *bridgev2. 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, + DisplayName: "Telegram", + NetworkURL: "https://telegram.org/", + NetworkIcon: "mxc://maunium.net/tJCRmUyJDsgRNgqhOgoiHWbX", + NetworkID: "telegram", + BeeperBridgeType: "telegram", + DefaultPort: 29317, + DefaultCommandPrefix: "!tg", } } diff --git a/pkg/connector/directdownload.go b/pkg/connector/directdownload.go index 49f64b0a..10eae2d2 100644 --- a/pkg/connector/directdownload.go +++ b/pkg/connector/directdownload.go @@ -30,6 +30,8 @@ func (tc *TelegramConnector) Download(ctx context.Context, mediaID networkid.Med ctx = log.WithContext(ctx) log.Info().Msg("handling direct download") + // TODO have an in-memory cache for media references? + userLogin, err := tc.Bridge.GetExistingUserLoginByID(ctx, ids.MakeUserLoginID(info.ReceiverID)) if err != nil { if info.PeerType != ids.PeerTypeChannel { diff --git a/pkg/connector/geouri.go b/pkg/connector/geouri.go index 8c4e848b..872505a3 100644 --- a/pkg/connector/geouri.go +++ b/pkg/connector/geouri.go @@ -6,6 +6,8 @@ import ( "strings" ) +// TODO this should probably be moved to mautrix-go + type GeoURI struct { Lat float64 Long float64 diff --git a/pkg/connector/ids/ids.go b/pkg/connector/ids/ids.go index f7e64146..422a7d87 100644 --- a/pkg/connector/ids/ids.go +++ b/pkg/connector/ids/ids.go @@ -25,7 +25,6 @@ func MakeUserLoginID(userID int64) networkid.UserLoginID { return networkid.UserLoginID(strconv.FormatInt(userID, 10)) } -// TODO: add space ID func MakeMessageID(messageID int) networkid.MessageID { return networkid.MessageID(strconv.Itoa(messageID)) } diff --git a/pkg/connector/login.go b/pkg/connector/login.go index f0e96549..d9621bf2 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -35,6 +35,8 @@ import ( "go.mau.fi/mautrix-telegram/pkg/connector/util" ) +// TODO QR login support + const LoginFlowIDPhone = "phone" func (tg *TelegramConnector) GetLoginFlows() []bridgev2.LoginFlow { diff --git a/pkg/connector/matrixfmt/html.go b/pkg/connector/matrixfmt/html.go index 3a0b6ed8..c52c55c6 100644 --- a/pkg/connector/matrixfmt/html.go +++ b/pkg/connector/matrixfmt/html.go @@ -367,6 +367,7 @@ func (parser *HTMLParser) linkToString(node *html.Node, ctx Context) *EntityStri // Mention not allowed, use name as-is return str } + // FIXME this or GetGhostDetails needs to support non-ghost users too userID, username, accessHash, ok := parser.GetGhostDetails(ctx.Ctx, mxid) if !ok { return str diff --git a/pkg/connector/media/transfer.go b/pkg/connector/media/transfer.go index fdbc742b..87078751 100644 --- a/pkg/connector/media/transfer.go +++ b/pkg/connector/media/transfer.go @@ -29,6 +29,7 @@ func getLargestPhotoSize(sizes []tg.PhotoSizeClass) (width, height int, largest panic("cannot get largest size from empty list of sizes") } + // FIXME this max size seems to be confusing bytes and dimensions. var maxSize int for _, s := range sizes { var currentSize int diff --git a/pkg/connector/store/upgrades/00-latest.sql b/pkg/connector/store/upgrades/00-latest.sql index 54cb3a15..9b3214d4 100644 --- a/pkg/connector/store/upgrades/00-latest.sql +++ b/pkg/connector/store/upgrades/00-latest.sql @@ -1,22 +1,22 @@ -- v0 -> v3: Latest revision CREATE TABLE telegram_session ( - user_id INTEGER PRIMARY KEY, + user_id BIGINT PRIMARY KEY, session_data BYTEA NOT NULL ); CREATE TABLE telegram_user_state ( - user_id INTEGER PRIMARY KEY, - pts INTEGER NOT NULL, - qts INTEGER NOT NULL, - date INTEGER NOT NULL, - seq INTEGER NOT NULL + user_id BIGINT PRIMARY KEY, + pts BIGINT NOT NULL, + qts BIGINT NOT NULL, + date BIGINT NOT NULL, + seq BIGINT NOT NULL ); CREATE TABLE telegram_channel_state ( - user_id INTEGER, - channel_id INTEGER, - pts INTEGER NOT NULL, + user_id BIGINT, + channel_id BIGINT, + pts BIGINT NOT NULL, PRIMARY KEY (user_id, channel_id) ); @@ -24,18 +24,18 @@ CREATE TABLE telegram_channel_state ( CREATE INDEX idx_telegram_channel_state_user_id ON telegram_channel_state (user_id); CREATE TABLE telegram_channel_access_hashes ( - user_id INTEGER, - channel_id INTEGER, - access_hash INTEGER NOT NULL, + user_id BIGINT, + channel_id BIGINT, + access_hash BIGINT NOT NULL, PRIMARY KEY (user_id, channel_id) ); CREATE TABLE telegram_user_metadata ( - receiver_id INTEGER, - user_id INTEGER, + receiver_id BIGINT, + user_id BIGINT, - access_hash INTEGER NOT NULL, + access_hash BIGINT NOT NULL, username TEXT, PRIMARY KEY (receiver_id, user_id) @@ -48,4 +48,5 @@ CREATE TABLE telegram_file ( size BIGINT ); +-- TODO this will be unnecessary once the queries switch to reading telegram_user_metadata CREATE INDEX idx_ghost_username ON ghost ((metadata->>'username')); diff --git a/pkg/connector/telegram.go b/pkg/connector/telegram.go index 19908c06..b7301054 100644 --- a/pkg/connector/telegram.go +++ b/pkg/connector/telegram.go @@ -317,6 +317,7 @@ func (t *TelegramClient) handleTyping(portal networkid.PortalKey, userID int64, if action.TypeID() != tg.SendMessageTypingActionTypeID { timeout = 0 } + // TODO send proper TypingTypes t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.Typing{ EventMeta: simplevent.EventMeta{ Type: bridgev2.RemoteEventTyping,