-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
transfer.go
36 lines (28 loc) · 934 Bytes
/
transfer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package telegram
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/tg"
)
func (c *Client) exportAuth(ctx context.Context, dcID int) (*tg.AuthExportedAuthorization, error) {
export, err := c.tg.AuthExportAuthorization(ctx, dcID)
if err != nil {
return nil, xerrors.Errorf("export auth to %d: %w", dcID, err)
}
return export, nil
}
// transfer exports current authorization and imports it to another DC.
// See https://core.telegram.org/api/datacenter#authorization-transfer.
func (c *Client) transfer(ctx context.Context, to *tg.Client, dc int) (tg.AuthAuthorizationClass, error) {
auth, err := c.exportAuth(ctx, dc)
if err != nil {
return nil, xerrors.Errorf("export to %d: %w", dc, err)
}
req := &tg.AuthImportAuthorizationRequest{}
req.FillFrom(auth)
r, err := to.AuthImportAuthorization(ctx, req)
if err != nil {
return nil, xerrors.Errorf("import from %d: %w", dc, err)
}
return r, nil
}