2023-12-14 16:50:46 +00:00
|
|
|
package community
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-12-22 09:43:19 +00:00
|
|
|
"encoding/json"
|
2023-12-14 16:50:46 +00:00
|
|
|
|
2023-12-22 09:43:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-12-14 16:50:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-12-15 20:29:39 +00:00
|
|
|
"github.com/status-im/status-go/server"
|
2023-12-14 16:50:46 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-12-22 09:43:19 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These events are used to notify the UI of state changes
|
|
|
|
const (
|
|
|
|
EventCommmunityDataUpdated walletevent.EventType = "wallet-community-data-updated"
|
2023-12-14 16:50:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
db *DataDB
|
|
|
|
communityInfoProvider thirdparty.CommunityInfoProvider
|
2023-12-15 20:29:39 +00:00
|
|
|
mediaServer *server.MediaServer
|
2023-12-22 09:43:19 +00:00
|
|
|
feed *event.Feed
|
2023-12-14 16:50:46 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 09:43:19 +00:00
|
|
|
type Data struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Image string `json:"image,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(db *sql.DB, mediaServer *server.MediaServer, feed *event.Feed) *Manager {
|
2023-12-14 16:50:46 +00:00
|
|
|
return &Manager{
|
2023-12-15 20:29:39 +00:00
|
|
|
db: NewDataDB(db),
|
|
|
|
mediaServer: mediaServer,
|
2023-12-22 09:43:19 +00:00
|
|
|
feed: feed,
|
2023-12-14 16:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to break circular dependency, call once as soon as possible after initialization
|
|
|
|
func (cm *Manager) SetCommunityInfoProvider(communityInfoProvider thirdparty.CommunityInfoProvider) {
|
|
|
|
cm.communityInfoProvider = communityInfoProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *Manager) GetCommunityInfo(id string) (*thirdparty.CommunityInfo, *InfoState, error) {
|
2023-12-15 20:29:39 +00:00
|
|
|
communityInfo, state, err := cm.db.GetCommunityInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if cm.mediaServer != nil && communityInfo != nil && len(communityInfo.CommunityImagePayload) > 0 {
|
2024-01-04 12:22:06 +00:00
|
|
|
communityInfo.CommunityImage = cm.GetCommunityImageURL(id)
|
2023-12-15 20:29:39 +00:00
|
|
|
}
|
|
|
|
return communityInfo, state, err
|
2023-12-14 16:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *Manager) GetCommunityID(tokenURI string) string {
|
|
|
|
return cm.communityInfoProvider.GetCommunityID(tokenURI)
|
|
|
|
}
|
|
|
|
|
2024-04-10 09:21:41 +00:00
|
|
|
func (cm *Manager) FillCollectiblesMetadata(communityID string, cs []*thirdparty.FullCollectibleData) (bool, error) {
|
|
|
|
return cm.communityInfoProvider.FillCollectiblesMetadata(communityID, cs)
|
2023-12-14 16:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *Manager) setCommunityInfo(id string, c *thirdparty.CommunityInfo) (err error) {
|
|
|
|
return cm.db.SetCommunityInfo(id, c)
|
|
|
|
}
|
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
func (cm *Manager) fetchCommunityInfo(communityID string, fetcher func() (*thirdparty.CommunityInfo, error)) (*thirdparty.CommunityInfo, error) {
|
|
|
|
communityInfo, err := fetcher()
|
2023-12-14 16:50:46 +00:00
|
|
|
if err != nil {
|
|
|
|
dbErr := cm.setCommunityInfo(communityID, nil)
|
|
|
|
if dbErr != nil {
|
|
|
|
log.Error("SetCommunityInfo failed", "communityID", communityID, "err", dbErr)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = cm.setCommunityInfo(communityID, communityInfo)
|
|
|
|
return communityInfo, err
|
|
|
|
}
|
2023-12-22 09:43:19 +00:00
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
func (cm *Manager) FetchCommunityInfo(communityID string) (*thirdparty.CommunityInfo, error) {
|
|
|
|
return cm.fetchCommunityInfo(communityID, func() (*thirdparty.CommunityInfo, error) {
|
|
|
|
return cm.communityInfoProvider.FetchCommunityInfo(communityID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:43:19 +00:00
|
|
|
func (cm *Manager) FetchCommunityMetadataAsync(communityID string) {
|
|
|
|
go func() {
|
2024-01-04 12:22:06 +00:00
|
|
|
communityInfo, err := cm.FetchCommunityMetadata(communityID)
|
2023-12-22 09:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("FetchCommunityInfo failed", "communityID", communityID, "err", err)
|
|
|
|
}
|
|
|
|
cm.signalUpdatedCommunityMetadata(communityID, communityInfo)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:22:06 +00:00
|
|
|
func (cm *Manager) FetchCommunityMetadata(communityID string) (*thirdparty.CommunityInfo, error) {
|
|
|
|
communityInfo, err := cm.FetchCommunityInfo(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_ = cm.setCommunityInfo(communityID, communityInfo)
|
|
|
|
return communityInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *Manager) GetCommunityImageURL(communityID string) string {
|
|
|
|
if cm.mediaServer != nil {
|
|
|
|
return cm.mediaServer.MakeWalletCommunityImagesURL(communityID)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:43:19 +00:00
|
|
|
func (cm *Manager) signalUpdatedCommunityMetadata(communityID string, communityInfo *thirdparty.CommunityInfo) {
|
|
|
|
if communityInfo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := Data{
|
|
|
|
ID: communityID,
|
|
|
|
Name: communityInfo.CommunityName,
|
|
|
|
Color: communityInfo.CommunityColor,
|
2024-01-04 12:22:06 +00:00
|
|
|
Image: cm.GetCommunityImageURL(communityID),
|
2023-12-22 09:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error marshaling response: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event := walletevent.Event{
|
|
|
|
Type: EventCommmunityDataUpdated,
|
|
|
|
Message: string(payload),
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.feed.Send(event)
|
|
|
|
}
|