client: better logging on connection state changes

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-09-25 13:58:03 -06:00
parent 5c23e9695f
commit 81c913bdd3
+27 -19
View File
@@ -196,9 +196,9 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
Logger: zaplog, Logger: zaplog,
UpdateHandler: client.updatesManager, UpdateHandler: client.updatesManager,
OnDead: client.onDead, OnDead: client.onDead,
OnSession: client.onConnectionStateChange, OnSession: client.onConnectionStateChange("session"),
OnConnected: client.onConnectionStateChange, OnConnected: client.onConnectionStateChange("connected"),
PingCallback: client.onConnectionStateChange, PingCallback: client.onConnectionStateChange("ping"),
OnAuthError: client.onAuthError, OnAuthError: client.onAuthError,
PingTimeout: time.Duration(tc.Config.Ping.TimeoutSeconds) * time.Second, PingTimeout: time.Duration(tc.Config.Ping.TimeoutSeconds) * time.Second,
PingInterval: time.Duration(tc.Config.Ping.IntervalSeconds) * time.Second, PingInterval: time.Duration(tc.Config.Ping.IntervalSeconds) * time.Second,
@@ -354,22 +354,30 @@ func (t *TelegramClient) onDead() {
}) })
} }
func (t *TelegramClient) onConnectionStateChange() { func (t *TelegramClient) onConnectionStateChange(reason string) func() {
authStatus, err := t.client.Auth().Status(context.Background()) return func() {
if err != nil { t.main.Bridge.Log.Info().
t.userLogin.BridgeState.Send(status.BridgeState{ Str("component", "telegram_client").
StateEvent: status.StateUnknownError, Str("user_login_id", string(t.userLogin.ID)).
Error: "tg-not-authenticated", Str("reason", reason).
Message: err.Error(), Msg("Connection state changed")
})
} else if authStatus.Authorized { authStatus, err := t.client.Auth().Status(context.Background())
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) if err != nil {
} else { t.userLogin.BridgeState.Send(status.BridgeState{
t.userLogin.BridgeState.Send(status.BridgeState{ StateEvent: status.StateUnknownError,
StateEvent: status.StateBadCredentials, Error: "tg-not-authenticated",
Error: "tg-no-auth", Message: err.Error(),
Message: "You're not logged in", })
}) } 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",
})
}
} }
} }