diff --git a/pkg/connector/backfill.go b/pkg/connector/backfill.go index 28399755..42249d74 100644 --- a/pkg/connector/backfill.go +++ b/pkg/connector/backfill.go @@ -47,7 +47,7 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err } log := zerolog.Ctx(ctx).With().Str("function", "getTakeoutID").Logger() - if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID != 0 { + if t.metadata.TakeoutID != 0 { // Resume fetching dialogs using takeout and enqueueing them for // backfill. go t.takeoutDialogsOnce.Do(func() { @@ -55,7 +55,7 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err log.Err(err).Msg("Failed to takeout dialogs") } }) - return t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID, nil + return t.metadata.TakeoutID, nil } t.stopTakeoutTimer = time.AfterFunc(max(time.Hour, time.Duration(t.main.Bridge.Config.Backfill.Queue.BatchDelay*2)), sync.OnceFunc(func() { t.stopTakeout(ctx) })) @@ -89,14 +89,14 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err } }) - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID = accountTakeout.ID + t.metadata.TakeoutID = accountTakeout.ID return accountTakeout.ID, t.userLogin.Save(ctx) } } func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) error { log := zerolog.Ctx(ctx).With().Str("loop", "chat_fetch").Logger() - if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone { + if t.metadata.TakeoutDialogCrawlDone { log.Debug().Msg("Dialogs already crawled") return nil } @@ -105,9 +105,9 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er Limit: 100, OffsetPeer: &tg.InputPeerEmpty{}, } - if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor != "" { + if t.metadata.TakeoutDialogCrawlCursor != "" { var err error - req.OffsetPeer, _, err = t.inputPeerForPortalID(ctx, t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor) + req.OffsetPeer, _, err = t.inputPeerForPortalID(ctx, t.metadata.TakeoutDialogCrawlCursor) if err != nil { return fmt.Errorf("failed to get input peer for pagination: %w", err) } @@ -130,7 +130,7 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er if err != nil { return fmt.Errorf("failed to get dialogs: %w", err) } else if len(dialogs.GetDialogs()) == 0 { - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone = true + t.metadata.TakeoutDialogCrawlDone = true if err = t.userLogin.Save(ctx); err != nil { return fmt.Errorf("failed to save user login: %w", err) } @@ -153,13 +153,13 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er portalKey := t.makePortalKeyFromPeer(dialogs.GetDialogs()[len(dialogs.GetDialogs())-1].GetPeer(), 0) - if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor == portalKey.ID { - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone = true - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor = "" + if t.metadata.TakeoutDialogCrawlCursor == portalKey.ID { + t.metadata.TakeoutDialogCrawlDone = true + t.metadata.TakeoutDialogCrawlCursor = "" log.Debug().Msg("No more dialogs found") return nil } else { - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor = portalKey.ID + t.metadata.TakeoutDialogCrawlCursor = portalKey.ID } if err = t.userLogin.Save(ctx); err != nil { return fmt.Errorf("failed to save user login: %w", err) @@ -180,7 +180,7 @@ func (t *TelegramClient) stopTakeout(ctx context.Context) error { if err != nil { return err } - t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID = 0 + t.metadata.TakeoutID = 0 return t.userLogin.Save(ctx) } diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 31579a49..d1f9d908 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -76,6 +76,7 @@ type TelegramClient struct { loginID networkid.UserLoginID userID networkid.UserID userLogin *bridgev2.UserLogin + metadata *UserLoginMetadata client *telegram.Client updatesManager *updates.Manager clientCtx context.Context @@ -184,6 +185,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge loginID: login.ID, userID: networkid.UserID(login.ID), userLogin: login, + metadata: login.Metadata.(*UserLoginMetadata), takeoutAccepted: exsync.NewEvent(), @@ -526,7 +528,7 @@ func (t *TelegramClient) onSession() { func (t *TelegramClient) onAuthError(err error) { t.sendBadCredentialsOrUnknownError(err) - t.userLogin.Metadata.(*UserLoginMetadata).ResetOnLogout() + t.metadata.ResetOnLogout() go func() { if err := t.userLogin.Save(context.Background()); err != nil { t.main.Bridge.Log.Err(err).Msg("failed to save user login") @@ -543,7 +545,7 @@ func (t *TelegramClient) Connect(_ context.Context) { log := zerolog.Ctx(context.Background()).With().Int64("user_id", t.telegramUserID).Logger() ctx = log.WithContext(ctx) - if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() { + if !t.metadata.Session.HasAuthKey() { log.Warn().Msg("user does not have an auth key, sending bad credentials state") t.sendBadCredentialsOrUnknownError(ErrNoAuthKey) return @@ -637,7 +639,7 @@ func (t *TelegramClient) getSingleChannel(ctx context.Context, id int64) (*tg.Ch func (t *TelegramClient) IsLoggedIn() bool { // TODO use less hacky check than context cancellation return t != nil && t.clientCtx != nil && t.client != nil && t.clientCtx.Err() == nil && - t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() + t.metadata.Session.HasAuthKey() } func (t *TelegramClient) LogoutRemote(ctx context.Context) { @@ -648,7 +650,7 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) { log.Info().Msg("Logging out and disconnecting") - if t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() { + if t.metadata.Session.HasAuthKey() { log.Info().Msg("User has an auth key, logging out") // logging out is best effort, we want to logout even if we can't call the endpoint diff --git a/pkg/connector/handletelegram.go b/pkg/connector/handletelegram.go index 7ea9d692..56cb1d3d 100644 --- a/pkg/connector/handletelegram.go +++ b/pkg/connector/handletelegram.go @@ -1172,14 +1172,14 @@ func (t *TelegramClient) onNotifySettings(ctx context.Context, e tg.Entities, up 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 { + for _, portalID := range t.metadata.PinnedDialogs { pt, id, _, err := ids.ParsePortalID(portalID) if err != nil { return err } needsUnpinning[t.makePortalKeyFromID(pt, id, 0)] = struct{}{} } - t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = nil + t.metadata.PinnedDialogs = nil for _, d := range msg.Order { dialog, ok := d.(*tg.DialogPeer) @@ -1188,7 +1188,7 @@ func (t *TelegramClient) onPinnedDialogs(ctx context.Context, e tg.Entities, msg } portalKey := t.makePortalKeyFromPeer(dialog.Peer, 0) delete(needsUnpinning, portalKey) - t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = append(t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs, portalKey.ID) + t.metadata.PinnedDialogs = append(t.metadata.PinnedDialogs, portalKey.ID) res := t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatInfoChange{ ChatInfoChange: &bridgev2.ChatInfoChange{ diff --git a/pkg/connector/push.go b/pkg/connector/push.go index b8561da2..9fc96478 100644 --- a/pkg/connector/push.go +++ b/pkg/connector/push.go @@ -402,7 +402,7 @@ UserLoop: } func (t *TelegramClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error { - meta := t.userLogin.Metadata.(*UserLoginMetadata) + meta := t.metadata if meta.PushEncryptionKey == nil { meta.PushEncryptionKey = random.Bytes(256) err := t.userLogin.Save(ctx) diff --git a/pkg/connector/sync.go b/pkg/connector/sync.go index 207b1633..d456c4fe 100644 --- a/pkg/connector/sync.go +++ b/pkg/connector/sync.go @@ -66,11 +66,11 @@ func (t *TelegramClient) SyncChats(ctx context.Context) error { } func (t *TelegramClient) resetPinnedDialogs(ctx context.Context, dialogs []tg.DialogClass) error { - t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = nil + t.metadata.PinnedDialogs = nil for _, dialog := range dialogs { if dialog.GetPinned() { portalKey := t.makePortalKeyFromPeer(dialog.GetPeer(), 0) - t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = append(t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs, portalKey.ID) + t.metadata.PinnedDialogs = append(t.metadata.PinnedDialogs, portalKey.ID) } } return t.userLogin.Save(ctx)