From 458f4ddd7e016f4172276e4cea62e5a28e7fe20d Mon Sep 17 00:00:00 2001
From: Tyler Allen
Date: Wed, 25 Nov 2026 19:40:34 -0500
Subject: [PATCH 1/3] feat: Abstention button
---
README.md | 9 ++++-----
database/poll.go | 10 +++++-----
database/vote.go | 12 ++++++++++++
docker-compose.yaml | 2 +-
api.go => routes.go | 28 +++++++++++++++-------------
templates/poll.tmpl | 10 +++++++---
6 files changed, 44 insertions(+), 27 deletions(-)
rename api.go => routes.go (98%)
diff --git a/README.md b/README.md
index a692756..715dc0e 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ Implementation
## Configuration
-Copy `.env.example` to `.env` and fill out the info
+Copy `.env.example` to `.env` and fill out the fields.
### Dev Overrides
`DEV_DISABLE_ACTIVE_FILTERS="true"` will disable the requirements that you be active to vote
@@ -38,10 +38,9 @@ go vet *.go
## To-Dos
-- [ ] Don't let the user fuck it up
- [ ] Show E-Board polls with a higher priority
- [x] Move Hide Vote to create instead of after you vote :skull:
-- [X] Display the reason why a user is on the results page of a running poll
+- [x] Display the reason why a user is on the results page of a running poll
- [ ] Display minimum time left that a poll is open
-- [ ] Move routes to their own functions
-- [X] Change HTTP resposne codes to be `http.something` instead of just a number
+- [x] Move routes to their own functions
+- [x] Change HTTP resposne codes to be `http.something` instead of just a number
diff --git a/database/poll.go b/database/poll.go
index 77b9f2b..b006b9c 100644
--- a/database/poll.go
+++ b/database/poll.go
@@ -216,19 +216,19 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st
votes = append(votes, optionList)
}
+ votesCast := len(votes)
+
round := 0
// Iterate until we have a winner
for {
round = round + 1
// Contains candidates to number of votes in this round
tallied := make(map[string]int)
- voteCount := 0
for _, picks := range votes {
// Go over picks until we find a non-eliminated candidate
for _, candidate := range picks {
if !slices.Contains(eliminated, candidate) {
tallied[candidate]++
- voteCount += 1
break
}
}
@@ -249,7 +249,7 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st
finalResult = append(finalResult, tallied)
// TODO this should probably include some poll identifier
- logging.Logger.WithFields(logrus.Fields{"round": round, "tallies": tallied, "threshold": voteCount / 2}).Debug("round report")
+ logging.Logger.WithFields(logrus.Fields{"round": round, "tallies": tallied, "threshold": votesCast / 2}).Debug("round report")
// If one person has all the votes, they win
if len(tallied) == 1 {
@@ -258,8 +258,8 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st
end := true
for str, val := range tallied {
- // if any particular entry is above half remaining votes, they win and it ends
- if val > (voteCount / 2) {
+ // if any particular entry is above half of votes cast, they win and it ends
+ if val > (votesCast / 2) {
finalResult = append(finalResult, map[string]int{str: val})
end = true
break
diff --git a/database/vote.go b/database/vote.go
index 8b15257..fdc7ac3 100644
--- a/database/vote.go
+++ b/database/vote.go
@@ -28,3 +28,15 @@ func HasVoted(ctx context.Context, pollId, userId string) (bool, error) {
}
return count > 0, nil
}
+
+func CastAbstain(ctx context.Context, voter *Voter) error {
+ ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
+ defer cancel()
+
+ _, err := Client.Database(db).Collection("voters").InsertOne(ctx, voter)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 0d279f3..92dcee3 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -28,7 +28,7 @@ services:
- "MONGO_INITDB_ROOT_USERNAME=vote"
- "MONGO_INITDB_ROOT_PASSWORD=c1f66aac6b4fafbef3c659371b8a50ed"
ports:
- - "127.0.0.1:27017:27017"
+ - "27017:27017"
volumes:
- type: volume
source: mongodb
diff --git a/api.go b/routes.go
similarity index 98%
rename from api.go
rename to routes.go
index 8fe6c7c..8889270 100644
--- a/api.go
+++ b/routes.go
@@ -340,11 +340,12 @@ func VoteInPoll(c *gin.Context) {
return
}
- if poll.VoteType == database.POLL_TYPE_SIMPLE {
+ switch poll.VoteType {
+ case database.POLL_TYPE_SIMPLE:
processSimpleVote(c, poll, pId, user)
- } else if poll.VoteType == database.POLL_TYPE_RANKED {
+ case database.POLL_TYPE_RANKED:
processRankedVote(c, poll, pId, user)
- } else {
+ default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unknown Poll Type"})
return
}
@@ -462,7 +463,12 @@ func processSimpleVote(c *gin.Context, poll *database.Poll, pId primitive.Object
UserId: user.Username,
}
- if hasOption(poll, c.PostForm("option")) {
+ if c.PostForm("abstain") != "" {
+ database.CastAbstain(c, &voter)
+ return
+ }
+
+ if slices.Contains(poll.Options, c.PostForm("option")) {
vote.Option = c.PostForm("option")
} else if poll.AllowWriteIns && c.PostForm("option") == "writein" {
vote.Option = c.PostForm("writeinOption")
@@ -485,6 +491,11 @@ func processRankedVote(c *gin.Context, poll *database.Poll, pId primitive.Object
UserId: user.Username,
}
+ if c.PostForm("abstain") != "" {
+ database.CastAbstain(c, &voter)
+ return
+ }
+
// Populate vote
for _, option := range poll.Options {
optionRankStr := c.PostForm(option)
@@ -612,12 +623,3 @@ func containsPoll(polls []*database.Poll, poll *database.Poll) bool {
}
return false
}
-
-func hasOption(poll *database.Poll, option string) bool {
- for _, opt := range poll.Options {
- if opt == option {
- return true
- }
- }
- return false
-}
diff --git a/templates/poll.tmpl b/templates/poll.tmpl
index 486def9..bcb951b 100644
--- a/templates/poll.tmpl
+++ b/templates/poll.tmpl
@@ -7,7 +7,7 @@
{{ end }}
{{ if eq .PollType "ranked" }}
This is a Ranked Choice vote. Rank the candidates in order of your preference. 1 is most preferred, and {{ .RankedMax }} is least preferred. You may leave an option blank
- if you do not prefer it at all.
+ if you do not prefer it at all. You must rank in increasing order from 1, you cannot skip numbers.
{{ end }}
@@ -81,7 +81,12 @@
{{ end }}
{{ end }}
-
+
+
+ Note: Pressing Abstain will submit this page. Abstain will add you to quorum but will empty your ballot options and you will not actually vote for anyone or count as a Vote Cast.
+