startchat: don't allow spamming get contact list request
This commit is contained in:
@@ -102,6 +102,8 @@ type TelegramClient struct {
|
|||||||
|
|
||||||
cachedContacts *tg.ContactsContacts
|
cachedContacts *tg.ContactsContacts
|
||||||
cachedContactsHash int64
|
cachedContactsHash int64
|
||||||
|
contactsLock sync.Mutex
|
||||||
|
lastContactReq time.Time
|
||||||
|
|
||||||
takeoutLock sync.Mutex
|
takeoutLock sync.Mutex
|
||||||
takeoutAccepted *exsync.Event
|
takeoutAccepted *exsync.Event
|
||||||
|
|||||||
+26
-16
@@ -23,6 +23,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"go.mau.fi/util/ptr"
|
"go.mau.fi/util/ptr"
|
||||||
@@ -200,25 +201,34 @@ func (t *TelegramClient) SearchUsers(ctx context.Context, query string) (resp []
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) GetContactList(ctx context.Context) (resp []*bridgev2.ResolveIdentifierResponse, err error) {
|
func (t *TelegramClient) GetContactList(ctx context.Context) (resp []*bridgev2.ResolveIdentifierResponse, err error) {
|
||||||
contacts, err := APICallWithOnlyUserUpdates(ctx, t, func() (*tg.ContactsContacts, error) {
|
t.contactsLock.Lock()
|
||||||
c, err := t.client.API().ContactsGetContacts(ctx, t.cachedContactsHash)
|
defer t.contactsLock.Unlock()
|
||||||
|
var contacts *tg.ContactsContacts
|
||||||
|
if time.Since(t.lastContactReq) > 10*time.Minute {
|
||||||
|
contacts, err = APICallWithOnlyUserUpdates(ctx, t, func() (*tg.ContactsContacts, error) {
|
||||||
|
c, err := t.client.API().ContactsGetContacts(ctx, t.cachedContactsHash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
switch typedResp := c.(type) {
|
||||||
|
case *tg.ContactsContacts:
|
||||||
|
t.cachedContacts = typedResp
|
||||||
|
var h hasher.Hasher
|
||||||
|
for _, contact := range t.cachedContacts.Contacts {
|
||||||
|
h.Update(uint32(contact.UserID))
|
||||||
|
}
|
||||||
|
t.cachedContactsHash = h.Sum()
|
||||||
|
case *tg.ContactsContactsNotModified:
|
||||||
|
// No changes
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unexpected contacts type: %T", c)
|
||||||
|
}
|
||||||
|
return t.cachedContacts, nil
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if c.TypeID() == tg.ContactsContactsTypeID {
|
t.lastContactReq = time.Now()
|
||||||
t.cachedContacts = c.(*tg.ContactsContacts)
|
|
||||||
var h hasher.Hasher
|
|
||||||
for _, contact := range t.cachedContacts.Contacts {
|
|
||||||
h.Update(uint32(contact.UserID))
|
|
||||||
}
|
|
||||||
t.cachedContactsHash = h.Sum()
|
|
||||||
} else if c.TypeID() != tg.ContactsContactsNotModifiedTypeID {
|
|
||||||
return nil, fmt.Errorf("unexpected contacts type: %T", c)
|
|
||||||
}
|
|
||||||
return t.cachedContacts, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
users := map[int64]tg.UserClass{}
|
users := map[int64]tg.UserClass{}
|
||||||
for _, user := range contacts.GetUsers() {
|
for _, user := range contacts.GetUsers() {
|
||||||
|
|||||||
Reference in New Issue
Block a user