Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/scan community when avatar is uploaded #200

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions communityhub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,9 @@ func (ch *CommunityHub) ScanNewCommunities() {
log.Warnw("failed to get community ID by chain alias", "chainAlias", contract.ChainAlias, "ID", nextID)
continue
}
onchainCommunity, err := contract.Community(communityID)
if err != nil {
if err != ErrCommunityNotFound {
log.Warnw("failed to get community data", "error", err)
}
continue
}
if err := ch.validateData(onchainCommunity); err != nil {
log.Warnw("failed to validate community data", "error", err)
continue
}
// store the community in the database
if err := ch.addCommunityToDB(onchainCommunity); err != nil {
log.Warnw("failed to add community to database", "error", err)
continue
}
// register token addresses in the census3 service and set
// the strategy ID in the community
if err := ch.registerTokenAddresses(onchainCommunity); err != nil {
log.Warnw("failed to register token addresses", "error", err)
continue
// sync the new community
if err := ch.SyncNewCommunity(communityID, contract); err != nil && err != ErrCommunityNotFound {
log.Warnw("failed to sync new community", "error", err)
}
}
}
Expand Down Expand Up @@ -314,6 +296,39 @@ func (ch *CommunityHub) CommunityContract(communityID string) (*HubContract, err
return contract, nil
}

// SyncNewCommunity method gets the community data from the contract and updates
// it in the database. It validates the data of the community and stores it in
// the database. It registers the token addresses in the census3 service and
// sets the strategy ID in the community. It returns an error if the data is
// invalid, if the community cannot be stored in the database, or if the token
// addresses cannot be registered in the census3 service.
func (ch *CommunityHub) SyncNewCommunity(communityID string, contract *HubContract) error {
var err error
if contract == nil {
contract, err = ch.CommunityContract(communityID)
if err != nil {
return err
}
}
onchainCommunity, err := contract.Community(communityID)
if err != nil {
return err
}
if err := ch.validateData(onchainCommunity); err != nil {
return err
}
// store the community in the database
if err := ch.addCommunityToDB(onchainCommunity); err != nil {
return err
}
// register token addresses in the census3 service and set
// the strategy ID in the community
if err := ch.registerTokenAddresses(onchainCommunity); err != nil {
return err
}
return nil
}

// UpdateCommunity method updates a community in the contract and the database.
// It merges the new data with the current data of the community and updates it
// in the contract and the database. If something goes wrong updating the
Expand Down
5 changes: 5 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (v *vocdoniHandler) updloadAvatarHandler(msg *apirest.APIdata, ctx *httprou
if err != nil {
return fmt.Errorf("cannot marshal result: %w", err)
}
// get the community data from the contract and initialize it (scan the
// census contract if it is required)
if err := v.comhub.SyncNewCommunity(req.CommunityID, nil); err != nil {
return fmt.Errorf("cannot sync community: %w", err)
}
return ctx.Send(res, 200)
}

Expand Down