updates: add wrapper for API calls to update users
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
type hasUpdates 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) {
|
||||
resp, err := fn()
|
||||
if err != nil {
|
||||
return resp, 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)
|
||||
}
|
||||
err := t.updateGhost(ctx, user.ID, user)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user