client: handle channel updates

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-09-26 11:50:11 -06:00
parent 7ccd8ab4ab
commit 332bbb8de1
3 changed files with 93 additions and 22 deletions
+49
View File
@@ -9,6 +9,7 @@ import (
"time"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
"github.com/rs/zerolog"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/bridgev2"
@@ -32,6 +33,54 @@ type IGetMessages interface {
GetMessages() []int
}
func (t *TelegramClient) onUpdateChannel(ctx context.Context, update *tg.UpdateChannel) error {
log := zerolog.Ctx(ctx).With().
Str("handler", "on_update_channel").
Int64("channel_id", update.ChannelID).
Logger()
log.Debug().Msg("Fetching channel due to UpdateChannel event")
leave := func() {
t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatDelete{
EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventChatDelete,
LogContext: func(c zerolog.Context) zerolog.Context {
return c.Int64("channel_id", update.ChannelID)
},
PortalKey: t.makePortalKeyFromID(ids.PeerTypeChannel, update.ChannelID),
Sender: t.mySender(),
},
OnlyForMe: true,
})
}
chats, err := APICallWithOnlyChatUpdates(ctx, t, func() (tg.MessagesChatsClass, error) {
if accessHash, err := t.ScopedStore.GetAccessHash(ctx, ids.PeerTypeChannel, update.ChannelID); err != nil {
return nil, err
} else {
return t.client.API().ChannelsGetChannels(ctx, []tg.InputChannelClass{
&tg.InputChannel{ChannelID: update.ChannelID, AccessHash: accessHash},
})
}
})
if err != nil {
if tgerr.Is(err, tg.ErrChannelInvalid, tg.ErrChannelPrivate) {
leave()
return nil
}
return fmt.Errorf("failed to get channel: %w", err)
} else if len(chats.GetChats()) != 1 {
return fmt.Errorf("expected 1 chat, got %d", len(chats.GetChats()))
} else if channel, ok := chats.GetChats()[0].(*tg.Channel); !ok {
log.Error().Type("chat_type", chats.GetChats()[0]).Msg("Expected channel, got something else. Leaving the channel.")
leave()
} else if channel.Left {
log.Error().Msg("Update was for a left channel. Leaving the channel.")
leave()
}
return nil
}
func (t *TelegramClient) onUpdateNewMessage(ctx context.Context, update IGetMessage) error {
log := zerolog.Ctx(ctx)
switch msg := update.GetMessage().(type) {