From 615421bb99d10db2a0e0860f2bfe8063271e986a Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Mon, 29 Jan 2024 19:20:28 +0300 Subject: [PATCH] refactor(client): add AuthTransferHandler type --- telegram/client.go | 4 ++-- telegram/options.go | 10 +++------- telegram/transfer.go | 8 ++++++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/telegram/client.go b/telegram/client.go index 564264b30a..7476c001b3 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -129,8 +129,8 @@ type Client struct { // Tracing. tracer trace.Tracer - // onTransfer is called on authorization transfer. - onTransfer func(ctx context.Context, client *Client, tx func(ctx context.Context) error) error + // onTransfer is called in transfer. + onTransfer AuthTransferHandler } // NewClient creates new unstarted client. diff --git a/telegram/options.go b/telegram/options.go index 724a581ed7..af80cb06dd 100644 --- a/telegram/options.go +++ b/telegram/options.go @@ -95,13 +95,9 @@ type Options struct { // OpenTelemetry. TracerProvider trace.TracerProvider - // OnTransfer is called on authorization transfer to acquire external lock. - // - // The function should call tx function to perform transfer, serializing it - // with external lock. - // - // The function must return error if tx function returned error. - OnTransfer func(ctx context.Context, c *Client, tx func(context.Context) error) error + // OnTransfer is called during authorization transfer. + // See [AuthTransferHandler] for details. + OnTransfer AuthTransferHandler } func (opt *Options) setDefaults() { diff --git a/telegram/transfer.go b/telegram/transfer.go index f33a25cd63..ec77228348 100644 --- a/telegram/transfer.go +++ b/telegram/transfer.go @@ -17,6 +17,14 @@ func (c *Client) exportAuth(ctx context.Context, dcID int) (*tg.AuthExportedAuth return export, nil } +// AuthTransferHandler is a function that is called during authorization transfer. +// +// The fn callback should be serialized by user id via external locking. +// You can call [Client.Self] to acquire current user id. +// +// The fn callback must return fn error if any. +type AuthTransferHandler func(ctx context.Context, client *Client, fn func(context.Context) error) error + func noopOnTransfer(ctx context.Context, _ *Client, fn func(context.Context) error) error { return fn(ctx) }