client: clean up connection code and add bad credentials handling
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -55,4 +55,4 @@ require (
|
|||||||
rsc.io/qr v0.2.0 // indirect
|
rsc.io/qr v0.2.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/gotd/td => github.com/beeper/td v0.107.1-0.20240815233135-3fc7e069705d
|
replace github.com/gotd/td => github.com/beeper/td v0.107.1-0.20240820133949-84f08733326c
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
github.com/beeper/td v0.107.1-0.20240815233135-3fc7e069705d h1:NtQqxJ9meIIGAwGo0cayqeaNy3iqBng2Sq0rZgCqsuc=
|
github.com/beeper/td v0.107.1-0.20240820133949-84f08733326c h1:PiqDd7WCO8UndBjhBmFd4fx8EyB9j0iZHXGec//ofaE=
|
||||||
github.com/beeper/td v0.107.1-0.20240815233135-3fc7e069705d/go.mod h1:rHtaG0hd4EY0ice4f9CVH/JxsA7ZICqkcH3aFSVZplg=
|
github.com/beeper/td v0.107.1-0.20240820133949-84f08733326c/go.mod h1:rHtaG0hd4EY0ice4f9CVH/JxsA7ZICqkcH3aFSVZplg=
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
||||||
|
|||||||
+36
-11
@@ -37,6 +37,7 @@ type TelegramClient struct {
|
|||||||
userID networkid.UserID
|
userID networkid.UserID
|
||||||
userLogin *bridgev2.UserLogin
|
userLogin *bridgev2.UserLogin
|
||||||
client *telegram.Client
|
client *telegram.Client
|
||||||
|
updatesManager *updates.Manager
|
||||||
clientCancel context.CancelFunc
|
clientCancel context.CancelFunc
|
||||||
|
|
||||||
appConfig map[string]any
|
appConfig map[string]any
|
||||||
@@ -155,7 +156,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
|
|
||||||
client.ScopedStore = tc.Store.GetScopedStore(telegramUserID)
|
client.ScopedStore = tc.Store.GetScopedStore(telegramUserID)
|
||||||
|
|
||||||
updatesManager := updates.New(updates.Config{
|
client.updatesManager = updates.New(updates.Config{
|
||||||
OnChannelTooLong: func(channelID int64) {
|
OnChannelTooLong: func(channelID int64) {
|
||||||
log.Warn().Int64("channel_id", channelID).Msg("channel too long")
|
log.Warn().Int64("channel_id", channelID).Msg("channel too long")
|
||||||
},
|
},
|
||||||
@@ -168,7 +169,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
client.client = telegram.NewClient(tc.Config.AppID, tc.Config.AppHash, telegram.Options{
|
client.client = telegram.NewClient(tc.Config.AppID, tc.Config.AppHash, telegram.Options{
|
||||||
SessionStorage: client.ScopedStore,
|
SessionStorage: client.ScopedStore,
|
||||||
Logger: zaplog,
|
Logger: zaplog,
|
||||||
UpdateHandler: updatesManager,
|
UpdateHandler: client.updatesManager,
|
||||||
OnDead: func() {
|
OnDead: func() {
|
||||||
login.BridgeState.Send(status.BridgeState{
|
login.BridgeState.Send(status.BridgeState{
|
||||||
StateEvent: status.StateTransientDisconnect,
|
StateEvent: status.StateTransientDisconnect,
|
||||||
@@ -176,12 +177,33 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
OnSession: func() {
|
OnSession: func() {
|
||||||
login.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
|
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,
|
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,
|
||||||
})
|
})
|
||||||
client.clientCancel, err = connectTelegramClient(ctx, client.client)
|
|
||||||
|
|
||||||
client.telegramFmtParams = &telegramfmt.FormatParams{
|
client.telegramFmtParams = &telegramfmt.FormatParams{
|
||||||
GetUserInfoByID: func(ctx context.Context, id int64) (telegramfmt.UserInfo, error) {
|
GetUserInfoByID: func(ctx context.Context, id int64) (telegramfmt.UserInfo, error) {
|
||||||
@@ -284,13 +306,6 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
|
||||||
err = updatesManager.Run(ctx, client.client.API(), telegramUserID, updates.AuthOptions{})
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("updates manager error")
|
|
||||||
client.clientCancel()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return &client, err
|
return &client, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,6 +344,16 @@ func connectTelegramClient(ctx context.Context, client *telegram.Client) (contex
|
|||||||
|
|
||||||
func (t *TelegramClient) Connect(ctx context.Context) (err error) {
|
func (t *TelegramClient) Connect(ctx context.Context) (err error) {
|
||||||
t.clientCancel, err = connectTelegramClient(ctx, t.client)
|
t.clientCancel, err = connectTelegramClient(ctx, t.client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
err = t.updatesManager.Run(ctx, t.client.API(), t.telegramUserID, updates.AuthOptions{})
|
||||||
|
if err != nil {
|
||||||
|
zerolog.Ctx(ctx).Err(err).Msg("failed to run updates manager")
|
||||||
|
t.clientCancel()
|
||||||
|
}
|
||||||
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user