chore: improve requesting contact/community info from mailserver (#4110)

This commit is contained in:
Igor Sirotin 2023-10-06 17:30:22 +01:00 committed by GitHub
parent 01526c344a
commit da3df63eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 40 deletions

View File

@ -2443,11 +2443,12 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitF
m.requestedCommunities[communityID] = nil
}
defer m.forgetCommunityRequest(communityID)
to := uint32(m.transport.GetCurrentTime() / 1000)
from := to - oneMonthInSeconds
_, err = m.performMailserverRequest(func() (*MessengerResponse, error) {
batch := MailserverBatch{From: from, To: to, Topics: []types.TopicType{filter.ContentTopic}}
m.logger.Info("Requesting historic")
err := m.processMailserverBatch(batch)
@ -2461,43 +2462,25 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitF
return nil, nil
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
var community *communities.Community
fetching := true
for fetching {
for {
select {
case <-time.After(200 * time.Millisecond):
//send signal to client that message status updated
community, err = m.communitiesManager.GetByIDString(communityID)
community, err := m.communitiesManager.GetByIDString(communityID)
if err != nil {
return nil, err
}
if community != nil && community.Name() != "" && community.DescriptionText() != "" {
fetching = false
return community, nil
}
case <-ctx.Done():
fetching = false
return nil, fmt.Errorf("failed to request community info for id '%s' from mailserver: %w", communityID, ctx.Err())
}
}
if community == nil {
return nil, nil
}
//if there is no info helpful for client, we don't post it
if community.Name() == "" && community.DescriptionText() == "" {
return nil, nil
}
m.forgetCommunityRequest(communityID)
return community, nil
}
// RequestCommunityInfoFromMailserver installs filter for community and requests its details

View File

@ -1221,32 +1221,25 @@ func (m *Messenger) RequestContactInfoFromMailserver(pubkey string, waitForRespo
return nil, nil
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
var contact *Contact
fetching := true
defer func() {
cancel()
m.forgetContactInfoRequest(pubkey)
}()
for fetching {
for {
select {
case <-time.After(200 * time.Millisecond):
var ok bool
contact, ok = m.allContacts.Load(pubkey)
contact, ok := m.allContacts.Load(pubkey)
if ok && contact != nil && contact.DisplayName != "" {
fetching = false
m.logger.Info("contact info received", zap.String("pubkey", contact.ID))
return contact, nil
}
case <-ctx.Done():
fetching = false
return nil, fmt.Errorf("failed to request contact info from mailserver: %w", ctx.Err())
}
}
m.forgetContactInfoRequest(pubkey)
return contact, nil
}
func (m *Messenger) requestContactInfoFromMailserver(pubkey string) error {