From 48059a3a5141021f65c979c78f2c6439c61f27ac Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Thu, 10 Oct 2024 10:04:48 -0600 Subject: [PATCH] logout: delete user-specific state Signed-off-by: Sumner Evans --- pkg/connector/client.go | 26 ++++++++++++++++++++++++-- pkg/connector/store/scoped_store.go | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 94967f6c..d9dbbca3 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -580,10 +580,32 @@ func (t *TelegramClient) IsLoggedIn() bool { } func (t *TelegramClient) LogoutRemote(ctx context.Context) { - _, err := t.client.API().AuthLogOut(ctx) + log := zerolog.Ctx(ctx).With(). + Str("action", "logout_remote"). + Int64("user_id", t.telegramUserID). + Logger() + + err := t.ScopedStore.DeleteUserState(ctx) if err != nil { - zerolog.Ctx(ctx).Err(err).Msg("failed to logout on Telegram") + log.Err(err).Msg("failed to delete user state") } + + err = t.ScopedStore.DeleteChannelStateForUser(ctx) + if err != nil { + log.Err(err).Msg("failed to delete channel state for user") + } + + err = t.ScopedStore.DeleteAccessHashesForUser(ctx) + if err != nil { + log.Err(err).Msg("failed to delete access hashes for user") + } + + _, err = t.client.API().AuthLogOut(ctx) + if err != nil { + log.Err(err).Msg("failed to logout on Telegram") + } + + log.Info().Msg("successfully logged out and deleted user state") } func (t *TelegramClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool { diff --git a/pkg/connector/store/scoped_store.go b/pkg/connector/store/scoped_store.go index ea8280f0..11e5abe1 100644 --- a/pkg/connector/store/scoped_store.go +++ b/pkg/connector/store/scoped_store.go @@ -44,12 +44,16 @@ const ( setSeqQuery = "UPDATE telegram_user_state SET seq=$1 WHERE user_id=$2" setDateSeqQuery = "UPDATE telegram_user_state SET date=$1, seq=$2 WHERE user_id=$3" + deleteChannelStateForUserQuery = "DELETE FROM telegram_channel_state WHERE user_id=$1" + deleteUserStateForUserQuery = "DELETE FROM telegram_user_state WHERE user_id=$1" + getAccessHashQuery = "SELECT access_hash FROM telegram_access_hash WHERE user_id=$1 AND entity_type=$2 AND entity_id=$3" setAccessHashQuery = ` INSERT INTO telegram_access_hash (user_id, entity_type, entity_id, access_hash) VALUES ($1, $2, $3, $4) ON CONFLICT (user_id, entity_type, entity_id) DO UPDATE SET access_hash=excluded.access_hash ` + deleteAccessHashesForUserQuery = "DELETE FROM telegram_access_hash WHERE user_id=$1" // User Username Queries getUsernameQuery = "SELECT username FROM telegram_username WHERE entity_type=$1 AND entity_id=$2" @@ -153,6 +157,16 @@ func (s *ScopedStore) SetDateSeq(ctx context.Context, userID int64, date int, se return } +func (s *ScopedStore) DeleteUserState(ctx context.Context) (err error) { + _, err = s.db.Exec(ctx, deleteUserStateForUserQuery, s.telegramUserID) + return +} + +func (s *ScopedStore) DeleteChannelStateForUser(ctx context.Context) (err error) { + _, err = s.db.Exec(ctx, deleteChannelStateForUserQuery, s.telegramUserID) + return +} + var _ updates.ChannelAccessHasher = (*ScopedStore)(nil) // Deprecated: only for interface, don't use directly. Use GetAccessHash instead @@ -184,6 +198,11 @@ func (s *ScopedStore) SetAccessHash(ctx context.Context, entityType ids.PeerType return } +func (s *ScopedStore) DeleteAccessHashesForUser(ctx context.Context) (err error) { + _, err = s.db.Exec(ctx, deleteAccessHashesForUserQuery, s.telegramUserID) + return +} + func (s *ScopedStore) GetUsername(ctx context.Context, entityType ids.PeerType, userID int64) (username string, err error) { err = s.db.QueryRow(ctx, getUsernameQuery, entityType, userID).Scan(&username) if errors.Is(err, sql.ErrNoRows) {