feat(messenger_communities): only send updated curated communities (#4056)

Fixes #4029
This commit is contained in:
Jonathan Rainville 2023-09-26 12:47:50 -04:00 committed by GitHub
parent 475036e9f2
commit 17cef5f211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"reflect"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -503,17 +504,39 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() {
// The current contract contains communities that are no longer accessible on waku // The current contract contains communities that are no longer accessible on waku
const unknownCommunitiesFoundTimeout = 60 * time.Second const unknownCommunitiesFoundTimeout = 60 * time.Second
type curatedCommunities struct {
ContractCommunities []string
ContractFeaturedCommunities []string
UnknownCommunities []string
}
var mu = sync.RWMutex{}
var c = curatedCommunities{}
go func() { go func() {
for { for {
var timeTillNextUpdate time.Duration var timeTillNextUpdate time.Duration
response, err := m.CuratedCommunities() response, err := m.CuratedCommunities()
if err != nil { if err != nil {
timeTillNextUpdate = errorTimeout timeTillNextUpdate = errorTimeout
} else { } else {
mu.Lock()
// Check if it's the same values we had
if !reflect.DeepEqual(c.ContractCommunities, response.ContractCommunities) ||
!reflect.DeepEqual(c.ContractFeaturedCommunities, response.ContractFeaturedCommunities) ||
!reflect.DeepEqual(c.UnknownCommunities, response.UnknownCommunities) {
// One of the communities is different, send the updated response
m.config.messengerSignalsHandler.SendCuratedCommunitiesUpdate(response) m.config.messengerSignalsHandler.SendCuratedCommunitiesUpdate(response)
// Update the values
c.ContractCommunities = response.ContractCommunities
c.ContractFeaturedCommunities = response.ContractFeaturedCommunities
c.UnknownCommunities = response.UnknownCommunities
}
mu.Unlock()
if len(response.UnknownCommunities) == 0 { if len(response.UnknownCommunities) == 0 {
//next update shouldn't happen soon //next update shouldn't happen soon
timeTillNextUpdate = successTimeout timeTillNextUpdate = successTimeout