From 0ef8581764e6077b4c1fd1d2b6f205d09b899697 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Tue, 20 Aug 2024 08:55:11 -0600 Subject: [PATCH] connector/client: cleanup Signed-off-by: Sumner Evans --- pkg/connector/client.go | 72 ++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 7eee5617..17a129a2 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -170,39 +170,11 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge SessionStorage: client.ScopedStore, Logger: zaplog, UpdateHandler: client.updatesManager, - OnDead: func() { - login.BridgeState.Send(status.BridgeState{ - StateEvent: status.StateTransientDisconnect, - Message: "Telegram client disconnected", - }) - }, - OnSession: func() { - authStatus, err := client.client.Auth().Status(ctx) - if err != nil { - login.BridgeState.Send(status.BridgeState{ - StateEvent: status.StateUnknownError, - Error: "tg-not-authenticated", - Message: err.Error(), - }) - } else if authStatus.Authorized { - login.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) - } else { - login.BridgeState.Send(status.BridgeState{ - StateEvent: status.StateBadCredentials, - Error: "tg-no-auth", - Message: "You're not logged in", - }) - } - }, - OnAuthError: func(err error) { - login.BridgeState.Send(status.BridgeState{ - StateEvent: status.StateBadCredentials, - Error: "tg-no-auth", - Message: err.Error(), - }) - }, - PingTimeout: time.Duration(tc.Config.Ping.TimeoutSeconds) * time.Second, - PingInterval: time.Duration(tc.Config.Ping.IntervalSeconds) * time.Second, + OnDead: client.onDead, + OnSession: client.onSession, + OnAuthError: client.onAuthError, + PingTimeout: time.Duration(tc.Config.Ping.TimeoutSeconds) * time.Second, + PingInterval: time.Duration(tc.Config.Ping.IntervalSeconds) * time.Second, }) client.telegramFmtParams = &telegramfmt.FormatParams{ @@ -342,6 +314,40 @@ func connectTelegramClient(ctx context.Context, client *telegram.Client) (contex return cancel, nil } +func (t *TelegramClient) onDead() { + t.userLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateTransientDisconnect, + Message: "Telegram client disconnected", + }) +} + +func (t *TelegramClient) onSession() { + authStatus, err := t.client.Auth().Status(context.Background()) + if err != nil { + t.userLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateUnknownError, + Error: "tg-not-authenticated", + Message: err.Error(), + }) + } else if authStatus.Authorized { + t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) + } else { + t.userLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateBadCredentials, + Error: "tg-no-auth", + Message: "You're not logged in", + }) + } +} + +func (t *TelegramClient) onAuthError(err error) { + t.userLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateBadCredentials, + Error: "tg-no-auth", + Message: err.Error(), + }) +} + func (t *TelegramClient) Connect(ctx context.Context) (err error) { t.clientCancel, err = connectTelegramClient(ctx, t.client) if err != nil {