logout: delete user-specific state

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-10-10 10:04:48 -06:00
parent 4205047aab
commit 48059a3a51
2 changed files with 43 additions and 2 deletions
+24 -2
View File
@@ -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 {
+19
View File
@@ -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) {