From 756054039203bac4741121bb2036a25e66834890 Mon Sep 17 00:00:00 2001 From: Volodymyr Kozieiev Date: Mon, 7 Aug 2023 13:54:00 +0100 Subject: [PATCH] Regularly update the list of curated communities (#3827) Regularly update the list of curated communities --- protocol/messenger.go | 1 + protocol/messenger_communities.go | 53 ++++++++++++++++++++++++++++--- protocol/messenger_config.go | 1 + services/ext/signal.go | 4 +++ signal/events_messenger.go | 7 ++++ 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/protocol/messenger.go b/protocol/messenger.go index e2225f11f..9509614fb 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -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 diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 1466c9093..b5af457d3 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -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 } diff --git a/protocol/messenger_config.go b/protocol/messenger_config.go index 4246e7c77..a33aa918f 100644 --- a/protocol/messenger_config.go +++ b/protocol/messenger_config.go @@ -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 { diff --git a/services/ext/signal.go b/services/ext/signal.go index 452c545ab..31e1d8672 100644 --- a/services/ext/signal.go +++ b/services/ext/signal.go @@ -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) +} diff --git a/signal/events_messenger.go b/signal/events_messenger.go index 3278f5a39..b6070491d 100644 --- a/signal/events_messenger.go +++ b/signal/events_messenger.go @@ -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) +}