Shorten loop on waiting for community

This commit is contained in:
Andrea Maria Piana 2021-09-17 10:02:04 +01:00
parent bd314b8adc
commit 3daba8c247
3 changed files with 29 additions and 26 deletions

View File

@ -593,7 +593,7 @@ func (m *Messenger) ImportCommunity(ctx context.Context, key *ecdsa.PrivateKey)
//request info already stored on mailserver, but its success is not crucial //request info already stored on mailserver, but its success is not crucial
// for import // for import
_ = m.RequestCommunityInfoFromMailserver(community.IDString()) _, _ = m.RequestCommunityInfoFromMailserver(community.IDString())
// We add ourselves // We add ourselves
_, err = m.communitiesManager.InviteUsersToCommunity(community.ID(), []*ecdsa.PublicKey{&m.identity.PublicKey}) _, err = m.communitiesManager.InviteUsersToCommunity(community.ID(), []*ecdsa.PublicKey{&m.identity.PublicKey})
@ -733,20 +733,19 @@ func (m *Messenger) BanUserFromCommunity(request *requests.BanUserFromCommunity)
// RequestCommunityInfoFromMailserver installs filter for community and requests its details // RequestCommunityInfoFromMailserver installs filter for community and requests its details
// from mailserver. When response received it will be passed through signals handler // from mailserver. When response received it will be passed through signals handler
func (m *Messenger) RequestCommunityInfoFromMailserver(communityID string) error { func (m *Messenger) RequestCommunityInfoFromMailserver(communityID string) (*communities.Community, error) {
_, err := m.requestCommunityInfoFromMailserver(communityID, true) return m.requestCommunityInfoFromMailserver(communityID)
return err
} }
// RequestCommunityInfoFromMailserverSync installs filter for community and requests its details // RequestCommunityInfoFromMailserverSync installs filter for community and requests its details
// from mailserver. It will wait for a response and return it if any community is found // from mailserver. It will wait for a response and return it if any community is found
func (m *Messenger) RequestCommunityInfoFromMailserverSync(communityID string) (*communities.Community, error) { func (m *Messenger) RequestCommunityInfoFromMailserverSync(communityID string) (*communities.Community, error) {
return m.requestCommunityInfoFromMailserver(communityID, false) return m.requestCommunityInfoFromMailserver(communityID)
} }
// RequestCommunityInfoFromMailserver installs filter for community and requests its details // RequestCommunityInfoFromMailserver installs filter for community and requests its details
// from mailserver. When response received it will be passed through signals handler // from mailserver. When response received it will be passed through signals handler
func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, async bool) (*communities.Community, error) { func (m *Messenger) requestCommunityInfoFromMailserver(communityID string) (*communities.Community, error) {
if _, ok := m.requestedCommunities[communityID]; ok { if _, ok := m.requestedCommunities[communityID]; ok {
return nil, nil return nil, nil
@ -779,40 +778,43 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, async
nil, nil,
nil, nil,
filter, filter,
false) true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if async { ctx := context.Background()
//It is possible that we already processed last existing message for community ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
//and won't get any updates, so send stored info in this case after timeout defer cancel()
go func() { var community *communities.Community
time.Sleep(15 * time.Second)
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.requestedCommunities[communityID]; ok { fetching := true
m.passStoredCommunityInfoToSignalHandler(communityID)
}
}()
return nil, nil
}
time.Sleep(15 * time.Second)
for fetching {
select {
case <-time.After(200 * time.Millisecond):
//send signal to client that message status updated //send signal to client that message status updated
community, err := m.communitiesManager.GetByIDString(communityID) community, err = m.communitiesManager.GetByIDString(communityID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if community != nil && community.Name() != "" && community.DescriptionText() != "" {
fetching = false
}
case <-ctx.Done():
fetching = false
}
}
if community == nil { if community == nil {
return nil, nil return nil, nil
} }
//if there is no info helpful for client, we don't post it //if there is no info helpful for client, we don't post it
if community.Name() == "" && community.DescriptionText() == "" && community.MembersCount() == 0 { if community.Name() == "" && community.DescriptionText() == "" {
return nil, nil return nil, nil
} }

View File

@ -846,7 +846,7 @@ func (api *PublicAPI) EnsVerified(pk, ensName string) error {
return api.service.messenger.ENSVerified(pk, ensName) return api.service.messenger.ENSVerified(pk, ensName)
} }
func (api *PublicAPI) RequestCommunityInfoFromMailserver(communityID string) error { func (api *PublicAPI) RequestCommunityInfoFromMailserver(communityID string) (*communities.Community, error) {
return api.service.messenger.RequestCommunityInfoFromMailserver(communityID) return api.service.messenger.RequestCommunityInfoFromMailserver(communityID)
} }

View File

@ -2,3 +2,4 @@ CREATE TABLE envelopes (id BYTEA NOT NULL UNIQUE, data BYTEA NOT NULL, topic BYT
CREATE INDEX id_bloom_idx ON envelopes (id DESC, bloom); CREATE INDEX id_bloom_idx ON envelopes (id DESC, bloom);
CREATE INDEX id_topic_idx ON envelopes (id DESC, topic); CREATE INDEX id_topic_idx ON envelopes (id DESC, topic);
CREATE INDEX topic_idx ON envelopes(topic);