2023-11-14 15:52:28 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-11-14 21:06:33 +00:00
|
|
|
curatedCommunitiesUpdateInterval = 2 * time.Minute
|
2023-11-14 15:52:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regularly gets list of curated communities and signals them to client
|
|
|
|
func (m *Messenger) startCuratedCommunitiesUpdateLoop() {
|
|
|
|
logger := m.logger.Named("startCuratedCommunitiesUpdateLoop")
|
|
|
|
|
|
|
|
go func() {
|
2023-11-14 21:06:33 +00:00
|
|
|
// Initialize interval to 0 for immediate execution
|
|
|
|
var interval time.Duration = 0
|
2023-11-14 15:52:28 +00:00
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
cache, err := m.communitiesManager.GetCuratedCommunities()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to start curated communities loop", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2023-11-14 15:52:28 +00:00
|
|
|
|
|
|
|
for {
|
2023-11-14 21:06:33 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(interval):
|
|
|
|
// Immediate execution on first run, then set to regular interval
|
|
|
|
interval = curatedCommunitiesUpdateInterval
|
|
|
|
|
|
|
|
curatedCommunities, err := m.getCuratedCommunitiesFromContract()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to get curated communities from contract", zap.Error(err))
|
|
|
|
continue
|
2023-11-14 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
if reflect.DeepEqual(cache.ContractCommunities, curatedCommunities.ContractCommunities) &&
|
|
|
|
reflect.DeepEqual(cache.ContractFeaturedCommunities, curatedCommunities.ContractFeaturedCommunities) {
|
|
|
|
// nothing changed
|
|
|
|
continue
|
|
|
|
}
|
2023-11-14 15:52:28 +00:00
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
err = m.communitiesManager.SetCuratedCommunities(curatedCommunities)
|
|
|
|
if err == nil {
|
|
|
|
cache = curatedCommunities
|
2023-11-14 15:52:28 +00:00
|
|
|
} else {
|
2023-11-14 21:06:33 +00:00
|
|
|
logger.Error("failed to save curated communities", zap.Error(err))
|
2023-11-14 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
response, err := m.fetchCuratedCommunities(curatedCommunities)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to fetch curated communities", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2023-11-14 15:52:28 +00:00
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
m.config.messengerSignalsHandler.SendCuratedCommunitiesUpdate(response)
|
2023-11-14 15:52:28 +00:00
|
|
|
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
func (m *Messenger) getCuratedCommunitiesFromContract() (*communities.CuratedCommunities, error) {
|
2023-11-14 15:52:28 +00:00
|
|
|
if m.contractMaker == nil {
|
|
|
|
m.logger.Warn("contract maker not initialized")
|
|
|
|
return nil, errors.New("contract maker not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
testNetworksEnabled, err := m.settings.GetTestNetworksEnabled()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := uint64(10) // Optimism Mainnet
|
|
|
|
if testNetworksEnabled {
|
|
|
|
chainID = 420 // Optimism Goerli
|
|
|
|
}
|
|
|
|
|
|
|
|
directory, err := m.contractMaker.NewDirectory(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
callOpts := &bind.CallOpts{Context: context.Background(), Pending: false}
|
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
contractCommunities, err := directory.GetCommunities(callOpts)
|
2023-11-14 15:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-14 21:06:33 +00:00
|
|
|
var contractCommunityIDs []string
|
|
|
|
for _, c := range contractCommunities {
|
|
|
|
contractCommunityIDs = append(contractCommunityIDs, types.HexBytes(c).String())
|
2023-11-14 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
featuredContractCommunities, err := directory.GetFeaturedCommunities(callOpts)
|
2023-11-14 15:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-14 21:06:33 +00:00
|
|
|
var contractFeaturedCommunityIDs []string
|
|
|
|
for _, c := range featuredContractCommunities {
|
|
|
|
contractFeaturedCommunityIDs = append(contractFeaturedCommunityIDs, types.HexBytes(c).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &communities.CuratedCommunities{
|
|
|
|
ContractCommunities: contractCommunityIDs,
|
|
|
|
ContractFeaturedCommunities: contractFeaturedCommunityIDs,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-11-14 15:52:28 +00:00
|
|
|
|
2023-11-14 21:06:33 +00:00
|
|
|
func (m *Messenger) fetchCuratedCommunities(curatedCommunities *communities.CuratedCommunities) (*communities.KnownCommunitiesResponse, error) {
|
|
|
|
response, err := m.communitiesManager.GetStoredDescriptionForCommunities(curatedCommunities.ContractCommunities)
|
2023-11-14 15:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-14 21:06:33 +00:00
|
|
|
response.ContractFeaturedCommunities = curatedCommunities.ContractFeaturedCommunities
|
2023-11-14 15:52:28 +00:00
|
|
|
|
|
|
|
// TODO: use mechanism to obtain shard from community ID (https://github.com/status-im/status-desktop/issues/12585)
|
|
|
|
var unknownCommunities []communities.CommunityShard
|
|
|
|
for _, u := range response.UnknownCommunities {
|
|
|
|
unknownCommunities = append(unknownCommunities, communities.CommunityShard{
|
|
|
|
CommunityID: u,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
go m.requestCommunitiesFromMailserver(unknownCommunities)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
2023-11-14 21:06:33 +00:00
|
|
|
|
|
|
|
func (m *Messenger) CuratedCommunities() (*communities.KnownCommunitiesResponse, error) {
|
|
|
|
curatedCommunities, err := m.communitiesManager.GetCuratedCommunities()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m.fetchCuratedCommunities(curatedCommunities)
|
|
|
|
}
|