From bf3e0ec8ab8f64ba66fbf9b16d4f689844edc04c Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 2 Dec 2024 15:53:43 -0700 Subject: [PATCH] connector: simplify some of the dispatcher handlers Signed-off-by: Sumner Evans --- pkg/connector/client.go | 24 ++++++------------------ pkg/connector/telegram.go | 10 +++++----- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 4d8c678a..d9878c9c 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -152,12 +152,10 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge dispatcher.OnNewMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewMessage) error { return client.onUpdateNewMessage(ctx, e.Channels, update) }) - dispatcher.OnChannel(func(ctx context.Context, e tg.Entities, update *tg.UpdateChannel) error { - return client.onUpdateChannel(ctx, update) - }) dispatcher.OnNewChannelMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewChannelMessage) error { return client.onUpdateNewMessage(ctx, e.Channels, update) }) + dispatcher.OnChannel(client.onUpdateChannel) dispatcher.OnUserName(client.onUserName) dispatcher.OnDeleteMessages(func(ctx context.Context, e tg.Entities, update *tg.UpdateDeleteMessages) error { return client.onDeleteMessages(ctx, 0, update) @@ -184,27 +182,17 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge dispatcher.OnChannelUserTyping(func(ctx context.Context, e tg.Entities, update *tg.UpdateChannelUserTyping) error { return client.handleTyping(client.makePortalKeyFromID(ids.PeerTypeChannel, update.ChannelID), client.getPeerSender(update.FromID), update.Action) }) - dispatcher.OnReadHistoryOutbox(func(ctx context.Context, e tg.Entities, update *tg.UpdateReadHistoryOutbox) error { - return client.updateReadReceipt(update) - }) + dispatcher.OnReadHistoryOutbox(client.updateReadReceipt) dispatcher.OnReadHistoryInbox(func(ctx context.Context, e tg.Entities, update *tg.UpdateReadHistoryInbox) error { return client.onOwnReadReceipt(client.makePortalKeyFromPeer(update.Peer), update.MaxID) }) dispatcher.OnReadChannelInbox(func(ctx context.Context, e tg.Entities, update *tg.UpdateReadChannelInbox) error { return client.onOwnReadReceipt(client.makePortalKeyFromID(ids.PeerTypeChannel, update.ChannelID), update.MaxID) }) - dispatcher.OnNotifySettings(func(ctx context.Context, e tg.Entities, update *tg.UpdateNotifySettings) error { - return client.onNotifySettings(ctx, update) - }) - dispatcher.OnPinnedDialogs(func(ctx context.Context, e tg.Entities, update *tg.UpdatePinnedDialogs) error { - return client.onPinnedDialogs(ctx, update) - }) - dispatcher.OnChatDefaultBannedRights(func(ctx context.Context, e tg.Entities, update *tg.UpdateChatDefaultBannedRights) error { - return client.onChatDefaultBannedRights(ctx, e, update) - }) - dispatcher.OnPeerBlocked(func(ctx context.Context, e tg.Entities, update *tg.UpdatePeerBlocked) error { - return client.onPeerBlocked(ctx, update) - }) + dispatcher.OnNotifySettings(client.onNotifySettings) + dispatcher.OnPinnedDialogs(client.onPinnedDialogs) + dispatcher.OnChatDefaultBannedRights(client.onChatDefaultBannedRights) + dispatcher.OnPeerBlocked(client.onPeerBlocked) dispatcher.OnChat(client.onChat) dispatcher.OnPhoneCall(client.onPhoneCall) diff --git a/pkg/connector/telegram.go b/pkg/connector/telegram.go index a46d9ea8..b78ab0b9 100644 --- a/pkg/connector/telegram.go +++ b/pkg/connector/telegram.go @@ -50,7 +50,7 @@ func (t *TelegramClient) selfLeaveChat(portalKey networkid.PortalKey) { }) } -func (t *TelegramClient) onUpdateChannel(ctx context.Context, update *tg.UpdateChannel) error { +func (t *TelegramClient) onUpdateChannel(ctx context.Context, e tg.Entities, update *tg.UpdateChannel) error { log := zerolog.Ctx(ctx).With(). Str("handler", "on_update_channel"). Int64("channel_id", update.ChannelID). @@ -779,7 +779,7 @@ func (t *TelegramClient) handleTyping(portal networkid.PortalKey, sender bridgev return nil } -func (t *TelegramClient) updateReadReceipt(update *tg.UpdateReadHistoryOutbox) error { +func (t *TelegramClient) updateReadReceipt(ctx context.Context, e tg.Entities, update *tg.UpdateReadHistoryOutbox) error { user, ok := update.Peer.(*tg.PeerUser) if !ok { // Read receipts from other users are meaningless in chats/channels @@ -927,7 +927,7 @@ func (t *TelegramClient) transferEmojisToMatrix(ctx context.Context, customEmoji return } -func (t *TelegramClient) onNotifySettings(ctx context.Context, update *tg.UpdateNotifySettings) error { +func (t *TelegramClient) onNotifySettings(ctx context.Context, e tg.Entities, update *tg.UpdateNotifySettings) error { if update.Peer.TypeID() != tg.NotifyPeerTypeID { return fmt.Errorf("unsupported peer type %s", update.Peer.TypeName()) } @@ -968,7 +968,7 @@ func (t *TelegramClient) HandleMute(ctx context.Context, msg *bridgev2.MatrixMut return err } -func (t *TelegramClient) onPinnedDialogs(ctx context.Context, msg *tg.UpdatePinnedDialogs) error { +func (t *TelegramClient) onPinnedDialogs(ctx context.Context, e tg.Entities, msg *tg.UpdatePinnedDialogs) error { needsUnpinning := map[networkid.PortalKey]struct{}{} for _, portalID := range t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs { pt, id, err := ids.ParsePortalID(portalID) @@ -1050,7 +1050,7 @@ func (t *TelegramClient) onChatDefaultBannedRights(ctx context.Context, entities return nil } -func (t *TelegramClient) onPeerBlocked(ctx context.Context, update *tg.UpdatePeerBlocked) error { +func (t *TelegramClient) onPeerBlocked(ctx context.Context, e tg.Entities, update *tg.UpdatePeerBlocked) error { var userID networkid.UserID if peer, ok := update.PeerID.(*tg.PeerUser); ok { userID = ids.MakeUserID(peer.UserID)