Skip to content

Commit

Permalink
fix vote credit redeem
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Aug 13, 2024
1 parent 0098b89 commit 38bcc1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion routes/votes/endpoints/redeem_vote_credits/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
}
}

votesParam := chi.URLParam(r, "votes")
votesParam := r.URL.Query().Get("votes")

votesInt, err := strconv.Atoi(votesParam)

Expand Down
14 changes: 8 additions & 6 deletions types/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ type EntityVote struct {

// Vote Info
type VoteInfo struct {
PerUser int `json:"per_user" description:"The amount of votes a single vote creates on this entity"`
VoteTime uint16 `json:"vote_time" description:"The amount of time in hours until a user can vote again"`
VoteCredits bool `json:"vote_credits" description:"Whether or not the entity supports vote credits"`
MultipleVotes bool `json:"multiple_votes" description:"Whether or not the entity supports multiple votes per time interval"`
SupportsUpvotes bool `json:"supports_upvotes" description:"Whether or not the entity supports upvotes"`
SupportsDownvotes bool `json:"supports_downvotes" description:"Whether or not the entity supports downvotes"`
PerUser int `json:"per_user" description:"The amount of votes a single vote creates on this entity"`
VoteTime uint16 `json:"vote_time" description:"The amount of time in hours until a user can vote again"`
VoteCredits bool `json:"vote_credits" description:"Whether or not the entity supports vote credits"`
MultipleVotes bool `json:"multiple_votes" description:"Whether or not the entity supports multiple votes per time interval"`
SupportsUpvotes bool `json:"supports_upvotes" description:"Whether or not the entity supports upvotes"`
SupportsDownvotes bool `json:"supports_downvotes" description:"Whether or not the entity supports downvotes"`
SupportsPartialVoteCreditsRedeem bool `json:"supports_partial_vote_credits_redeem" description:"Whether or not the entity supports partial vote credit redemption"`
}

// Stores the hours, minutes and seconds until the user can vote again
Expand Down Expand Up @@ -71,6 +72,7 @@ type VoteCreditTierRedeemSummary struct {
Votes int `json:"votes" description:"The amount of votes the entity has"`
SlabOverview []int `json:"slab_overview" description:"Slab-based overview with each index, i, representing the amount of votes in Tiers[i]"`
TotalCredits int `json:"total_credits" description:"The total amount of credits the user would get, in cents"`
VoteInfo *VoteInfo `json:"vote_info" description:"Some information about the vote"`
}

// Represents a vote credit redeem log
Expand Down
34 changes: 30 additions & 4 deletions votes/votecredits.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func EntityGetVoteCreditsSummary(
return nil, fmt.Errorf("could not fetch vote count: %w", err)
}

voteInfo, err := EntityVoteInfo(ctx, c, targetId, targetType)

if err != nil {
return nil, fmt.Errorf("could not fetch vote info: %w", err)
}

slabOverview := SlabSplitVotes(voteCount, vcts)
totalCredits := SlabCalculateCredits(vcts, slabOverview)

Expand All @@ -53,6 +59,7 @@ func EntityGetVoteCreditsSummary(
Votes: voteCount,
SlabOverview: slabOverview,
TotalCredits: totalCredits,
VoteInfo: voteInfo,
}, nil
}

Expand Down Expand Up @@ -92,6 +99,10 @@ func EntityRedeemVoteCredits(
return fmt.Errorf("could not fetch vote count: %w", err)
}

if !vi.SupportsPartialVoteCreditsRedeem {
votesToRedeem = voteCount // If the entity does not support partial vote credit redemption, then redeem all votes
}

// Check if the votes to redeem exceeds the total votes to protect against malicious input for votes to redeem
if votesToRedeem > voteCount {
return errors.New("votes to redeem exceeds total votes")
Expand All @@ -105,16 +116,31 @@ func EntityRedeemVoteCredits(
}

var id pgtype.UUID
err = c.QueryRow(ctx, "INSERT INTO entity_vote_redeem_logs (target_id, target_id, credits) VALUES ($1, $2, $3) RETURNING id", targetId, targetType, totalCredits).Scan(&id)
err = c.QueryRow(ctx, "INSERT INTO entity_vote_redeem_logs (target_id, target_type, credits) VALUES ($1, $2, $3) RETURNING id", targetId, targetType, totalCredits).Scan(&id)

if err != nil {
return fmt.Errorf("could not log vote credit redemption: %w", err)
}

_, err = c.Exec(ctx, "UPDATE entity_votes SET credit_redeem = $1, void = true, void_reason = 'Vote credits redeemed' WHERE target_id = $2 AND target_type = $3 AND void = false LIMIT $4", id, targetId, targetType, votesToRedeem)
if vi.SupportsPartialVoteCreditsRedeem {
var ids []pgtype.UUID
err = c.QueryRow(ctx, "SELECT array(SELECT itag FROM entity_votes WHERE target_id = $1 AND target_type = $2 AND void = false ORDER BY created_at ASC LIMIT $3)", targetId, targetType, votesToRedeem).Scan(&ids)

if err != nil {
return fmt.Errorf("could not redeem vote credits: %w", err)
if err != nil {
return fmt.Errorf("could not fetch vote ids: %w", err)
}

_, err = c.Exec(ctx, "UPDATE entity_votes SET credit_redeem = $1, void = true, void_reason = 'Vote credits redeemed' WHERE itag = ANY($2)", id, ids)

if err != nil {
return fmt.Errorf("could not redeem vote credits: %w", err)
}
} else {
_, err = c.Exec(ctx, "UPDATE entity_votes SET credit_redeem = $1, void = true, void_reason = 'Vote credits redeemed' WHERE target_id = $2 AND target_type = $3 AND void = false", id, targetId, targetType)

if err != nil {
return fmt.Errorf("could not redeem vote credits: %w", err)
}
}

return nil
Expand Down

0 comments on commit 38bcc1b

Please sign in to comment.