Regularly update the list of curated communities (#3827)

Regularly update the list of curated communities
This commit is contained in:
Volodymyr Kozieiev 2023-08-07 13:54:00 +01:00 committed by GitHub
parent ce82a8c0e5
commit 7560540392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 4 deletions

View File

@ -795,6 +795,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
}
m.startSyncSettingsLoop()
m.startCommunityRekeyLoop()
m.startCuratedCommunitiesUpdateLoop()
if err := m.cleanTopics(); err != nil {
return nil, err

View File

@ -403,6 +403,46 @@ func (m *Messenger) SpectatedCommunities() ([]*communities.Community, error) {
return m.communitiesManager.Spectated()
}
// Regularly gets list of curated communities and signals them to client
func (m *Messenger) startCuratedCommunitiesUpdateLoop() {
logger := m.logger.Named("startCuratedCommunitiesUpdateLoop")
const errorTimeout = 10 * time.Second
const successTimeout = 120 * time.Second
const unknownCommunitiesFoundTimeout = 3 * time.Second
go func() {
for {
var timeTillNextUpdate time.Duration
response, err := m.CuratedCommunities()
if err != nil {
timeTillNextUpdate = errorTimeout
} else {
m.config.messengerSignalsHandler.SendCuratedCommunitiesUpdate(response)
if len(response.UnknownCommunities) == 0 {
//next update shouldn't happen soon
timeTillNextUpdate = successTimeout
} else {
//unknown communities already requested from mailserver, so we wait just a bit before
//next attempt to get their info
timeTillNextUpdate = unknownCommunitiesFoundTimeout
}
}
logger.Debug("Next curated communities update will happen in", zap.Duration("timeTillNextUpdate", timeTillNextUpdate))
select {
case <-time.After(timeTillNextUpdate):
case <-m.quit:
return
}
}
}()
}
func (m *Messenger) CuratedCommunities() (*communities.KnownCommunitiesResponse, error) {
// Revert code to https://github.com/status-im/status-go/blob/e6a3f63ec7f2fa691878ed35f921413dc8acfc66/protocol/messenger_communities.go#L211-L226 once the curated communities contract is deployed to mainnet
@ -415,17 +455,22 @@ func (m *Messenger) CuratedCommunities() (*communities.KnownCommunitiesResponse,
if err != nil {
return nil, err
}
var backend *ethclient.Client
var ethClient *ethclient.Client
for _, n := range nodeConfig.Networks {
if n.ChainID == chainID {
b, err := ethclient.Dial(n.RPCURL)
e, err := ethclient.Dial(n.RPCURL)
if err != nil {
return nil, err
}
backend = b
ethClient = e
}
}
directory, err := m.contractMaker.NewDirectoryWithBackend(chainID, backend)
if ethClient == nil {
return nil, errors.New("failed to initialize backend before requesting curated communities")
}
directory, err := m.contractMaker.NewDirectoryWithBackend(chainID, ethClient)
if err != nil {
return nil, err
}

View File

@ -59,6 +59,7 @@ type MessengerSignalsHandler interface {
SendWakuBackedUpSettings(response *wakusync.WakuBackedUpDataResponse)
SendWakuBackedUpKeypair(response *wakusync.WakuBackedUpDataResponse)
SendWakuBackedUpWatchOnlyAccount(response *wakusync.WakuBackedUpDataResponse)
SendCuratedCommunitiesUpdate(response *communities.KnownCommunitiesResponse)
}
type config struct {

View File

@ -164,3 +164,7 @@ func (m *MessengerSignalsHandler) SendWakuBackedUpKeypair(response *wakusync.Wak
func (m *MessengerSignalsHandler) SendWakuBackedUpWatchOnlyAccount(response *wakusync.WakuBackedUpDataResponse) {
signal.SendWakuBackedUpWatchOnlyAccount(response)
}
func (m *MessengerSignalsHandler) SendCuratedCommunitiesUpdate(response *communities.KnownCommunitiesResponse) {
signal.SendCuratedCommunitiesUpdate(response)
}

View File

@ -13,6 +13,9 @@ const (
// EventStatusUpdatesTimedOut Event Automatic Status Updates Timed out
EventStatusUpdatesTimedOut = "status.updates.timedout"
// EventCuratedCommunitiesUpdate triggered when it is time to refresh the list of curated communities
EventCuratedCommunitiesUpdate = "curated.communities.update"
)
// MessageDeliveredSignal specifies chat and message that was delivered
@ -52,3 +55,7 @@ func SendCommunityInfoFound(community interface{}) {
func SendStatusUpdatesTimedOut(statusUpdates interface{}) {
send(EventStatusUpdatesTimedOut, statusUpdates)
}
func SendCuratedCommunitiesUpdate(curatedCommunitiesUpdate interface{}) {
send(EventCuratedCommunitiesUpdate, curatedCommunitiesUpdate)
}