connector: save channel access hashes in more places
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
+28
-7
@@ -7,26 +7,47 @@ import (
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
type hasUpdates interface {
|
||||
type hasUserUpdates interface {
|
||||
GetUsers() []tg.UserClass
|
||||
}
|
||||
|
||||
// Wrapper for API calls that return a response with updates.
|
||||
func APICallWithUpdates[U hasUpdates](ctx context.Context, t *TelegramClient, fn func() (U, error)) (U, error) {
|
||||
type hasUpdates interface {
|
||||
hasUserUpdates
|
||||
GetChats() []tg.ChatClass
|
||||
}
|
||||
|
||||
func APICallWithOnlyUserUpdates[U hasUserUpdates](ctx context.Context, t *TelegramClient, fn func() (U, error)) (U, error) {
|
||||
resp, err := fn()
|
||||
if err != nil {
|
||||
return resp, err
|
||||
return *new(U), err
|
||||
}
|
||||
|
||||
// TODO do we also need to expand this to chats and messages?
|
||||
for _, user := range resp.GetUsers() {
|
||||
user, ok := user.(*tg.User)
|
||||
if !ok {
|
||||
return resp, fmt.Errorf("user is %T not *tg.User", user)
|
||||
return *new(U), fmt.Errorf("user is %T not *tg.User", user)
|
||||
}
|
||||
_, err := t.updateGhost(ctx, user.ID, user)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
return *new(U), err
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Wrapper for API calls that return a response with updates.
|
||||
func APICallWithUpdates[U hasUpdates](ctx context.Context, t *TelegramClient, fn func() (U, error)) (U, error) {
|
||||
resp, err := APICallWithOnlyUserUpdates(ctx, t, fn)
|
||||
if err != nil {
|
||||
return *new(U), err
|
||||
}
|
||||
|
||||
for _, c := range resp.GetChats() {
|
||||
if channel, ok := c.(*tg.Channel); ok {
|
||||
if err := t.ScopedStore.SetAccessHash(ctx, channel.ID, channel.AccessHash); err != nil {
|
||||
return *new(U), err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user