Skip to content

Commit

Permalink
feat(tweet): increment point if task is validated
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Dec 13, 2022
1 parent 1f083a0 commit e37c48e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/actor/subscription/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,19 @@ func (a *Actor) handleGenTXSubmittedEvent(when time.Time, data map[string]interf
func (a *Actor) handleNewTweetEvent(data map[string]interface{}) {
log.Info().Interface("event", data).Msg("Handle NewTweet event")

_, err := tweet.Unmarshall(data)
e, err := tweet.Unmarshall(data)
if err != nil {
log.Panic().Err(err).Msg("❌ Failed unmarshall event to NewTweetEvent")
return
}

for _, task := range a.store.GetCurrentPhase().Tasks {
if task.Type == nemeton.TaskTypeTweetNemeton &&
task.InProgress() {
// Query validators
phase := a.store.GetCurrentPhase()
for _, task := range phase.Tasks {
if task.Type == nemeton.TaskTypeTweetNemeton && task.InProgress() {
err := a.store.CompleteTweetTask(a.ctx, e.User.Username, phase, &task)
if err != nil {
log.Panic().Err(err).Msg("❌ Could not complete tweet task")
}
return // We consider that there is only one tweet task by phase
}
}
}
31 changes: 31 additions & 0 deletions app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,34 @@ func (s *Store) UpdateValidatorUptime(ctx context.Context, consensusAddrs []type
_, err := s.db.Collection(validatorsCollectionName).BulkWrite(ctx, model, opts)
return err
}

func (s *Store) CompleteTweetTask(ctx context.Context, username string, phase *Phase, task *Task) error {
filter := bson.M{"twitter": username}
return s.completeTask(ctx, filter, phase, task)
}

func (s *Store) completeTask(ctx context.Context, filter bson.M, phase *Phase, task *Task) error {
if !phase.InProgress() || !task.InProgress() {
return errors.New("could not complete task since task or phase is not in progress")
}

c, err := s.db.Collection(validatorsCollectionName).UpdateOne(ctx, filter, bson.M{
"$set": bson.M{
fmt.Sprintf("tasks.%d.%s.completed", phase.Number, task.ID): true,
fmt.Sprintf("tasks.%d.%s.points", phase.Number, task.ID): task.Rewards,
},
})
if err != nil {
return err
}

if c.ModifiedCount == 1 {
_, err = s.db.Collection(validatorsCollectionName).UpdateOne(ctx, filter, bson.M{
"$inc": bson.M{
"points": task.Rewards,
},
})
}

return err
}

0 comments on commit e37c48e

Please sign in to comment.