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"
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2023-11-14 15:52:28 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-02-05 13:01:59 +00:00
|
|
|
curatedCommunitiesUpdateInterval = time.Hour
|
|
|
|
communitiesUpdateFailureInterval = 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() {
|
2024-03-09 09:42:06 +00:00
|
|
|
logger := m.logger.Named("curatedCommunitiesUpdateLoop")
|
|
|
|
|
|
|
|
if m.contractMaker == nil {
|
|
|
|
logger.Warn("not starting curated communities loop: contract maker not initialized")
|
|
|
|
return
|
|
|
|
}
|
2023-11-14 15:52:28 +00:00
|
|
|
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
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 {
|
2024-02-05 13:01:59 +00:00
|
|
|
interval = communitiesUpdateFailureInterval
|
2023-11-14 21:06:33 +00:00
|
|
|
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 {
|
2024-02-05 13:01:59 +00:00
|
|
|
interval = communitiesUpdateFailureInterval
|
2023-11-14 21:06:33 +00:00
|
|
|
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 {
|
|
|
|
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 {
|
2024-10-15 07:29:21 +00:00
|
|
|
chainID = 11155420 // Optimism Sepolia
|
2023-11-14 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-15 19:50:12 +00:00
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2023-12-15 19:50:12 +00:00
|
|
|
_ = m.fetchCommunities(unknownCommunities)
|
|
|
|
}()
|
2023-11-14 15:52:28 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|