updates: add wrapper for API calls to update users

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-08-21 13:45:45 -06:00
parent 284178df65
commit 0670c2b2bc
6 changed files with 124 additions and 73 deletions
+34
View File
@@ -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
}