Skip to content

Commit 41fa211

Browse files
authored
Merge pull request #1302 from gotd/feat/client-add-on-transfer
feat(clint): add OnTransfer
2 parents 10fee0b + 615421b commit 41fa211

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

telegram/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ type Client struct {
128128

129129
// Tracing.
130130
tracer trace.Tracer
131+
132+
// onTransfer is called in transfer.
133+
onTransfer AuthTransferHandler
131134
}
132135

133136
// NewClient creates new unstarted client.
@@ -161,6 +164,7 @@ func NewClient(appID int, appHash string, opt Options) *Client {
161164
migrationTimeout: opt.MigrationTimeout,
162165
noUpdatesMode: opt.NoUpdates,
163166
mw: opt.Middlewares,
167+
onTransfer: opt.OnTransfer,
164168
}
165169
if opt.TracerProvider != nil {
166170
client.tracer = opt.TracerProvider.Tracer(oteltg.Name)

telegram/client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func newTestClient(h testHandler) *Client {
8686
ctx: context.Background(),
8787
cancel: func() {},
8888
updateHandler: UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error { return nil }),
89+
onTransfer: noopOnTransfer,
8990
}
9091
client.init()
9192

telegram/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ type Options struct {
9494

9595
// OpenTelemetry.
9696
TracerProvider trace.TracerProvider
97+
98+
// OnTransfer is called during authorization transfer.
99+
// See [AuthTransferHandler] for details.
100+
OnTransfer AuthTransferHandler
97101
}
98102

99103
func (opt *Options) setDefaults() {
@@ -140,6 +144,9 @@ func (opt *Options) setDefaults() {
140144
return nil
141145
})
142146
}
147+
if opt.OnTransfer == nil {
148+
opt.OnTransfer = noopOnTransfer
149+
}
143150
}
144151

145152
func defaultBackoff(c clock.Clock) func() backoff.BackOff {

telegram/transfer.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,39 @@ func (c *Client) exportAuth(ctx context.Context, dcID int) (*tg.AuthExportedAuth
1717
return export, nil
1818
}
1919

20+
// AuthTransferHandler is a function that is called during authorization transfer.
21+
//
22+
// The fn callback should be serialized by user id via external locking.
23+
// You can call [Client.Self] to acquire current user id.
24+
//
25+
// The fn callback must return fn error if any.
26+
type AuthTransferHandler func(ctx context.Context, client *Client, fn func(context.Context) error) error
27+
28+
func noopOnTransfer(ctx context.Context, _ *Client, fn func(context.Context) error) error {
29+
return fn(ctx)
30+
}
31+
2032
// transfer exports current authorization and imports it to another DC.
2133
// See https://core.telegram.org/api/datacenter#authorization-transfer.
2234
func (c *Client) transfer(ctx context.Context, to *tg.Client, dc int) (tg.AuthAuthorizationClass, error) {
23-
auth, err := c.exportAuth(ctx, dc)
24-
if err != nil {
25-
return nil, errors.Wrapf(err, "export to %d", dc)
35+
var out tg.AuthAuthorizationClass
36+
if err := c.onTransfer(ctx, c, func(ctx context.Context) error {
37+
auth, err := c.exportAuth(ctx, dc)
38+
if err != nil {
39+
return errors.Wrapf(err, "export to %d", dc)
40+
}
41+
42+
req := &tg.AuthImportAuthorizationRequest{}
43+
req.FillFrom(auth)
44+
r, err := to.AuthImportAuthorization(ctx, req)
45+
if err != nil {
46+
return errors.Wrapf(err, "import from %d", dc)
47+
}
48+
49+
out = r
50+
return nil
51+
}); err != nil {
52+
return nil, errors.Wrap(err, "onTransfer")
2653
}
27-
28-
req := &tg.AuthImportAuthorizationRequest{}
29-
req.FillFrom(auth)
30-
r, err := to.AuthImportAuthorization(ctx, req)
31-
if err != nil {
32-
return nil, errors.Wrapf(err, "import from %d", dc)
33-
}
34-
35-
return r, nil
54+
return out, nil
3655
}

0 commit comments

Comments
 (0)