From 458f4ddd7e016f4172276e4cea62e5a28e7fe20d Mon Sep 17 00:00:00 2001 From: Tyler Allen Date: Wed, 25 Nov 2026 19:40:34 -0500 Subject: [PATCH 1/4] 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. +
+
+ {{ if and (.CanModify) (not .Gatekeep) }}
@@ -91,4 +96,3 @@ - From d432c6c881e3dfc3d4796e256ccd98497606848e Mon Sep 17 00:00:00 2001 From: Tyler Allen Date: Wed, 25 Nov 2026 20:33:23 -0500 Subject: [PATCH 2/4] clarify No Confidence --- templates/poll.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/poll.tmpl b/templates/poll.tmpl index bcb951b..af8ade6 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. You must rank in increasing order from 1, you cannot skip numbers.

+ if you do not prefer it at all (you have "No Confidence" in the option(s)). You must rank in increasing order from 1, you cannot skip numbers.

{{ end }}
From 43d4639c6cdffff7ba835c0a39c4692190ac5e52 Mon Sep 17 00:00:00 2001 From: Tyler Allen Date: Fri, 27 Nov 2026 19:54:01 -0500 Subject: [PATCH 3/4] fix abstain and padding on poll template --- routes.go | 9 +++------ templates/poll.tmpl | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/routes.go b/routes.go index 8889270..e5cb218 100644 --- a/routes.go +++ b/routes.go @@ -184,20 +184,17 @@ func CreatePoll(c *gin.Context) { switch c.PostForm("options") { case "pass-fail-conditional": - poll.Options = []string{"Pass", "Fail/Conditional", "Abstain"} + poll.Options = []string{"Pass", "Fail/Conditional"} case "fail-conditional": - poll.Options = []string{"Fail", "Conditional", "Abstain"} + poll.Options = []string{"Fail", "Conditional"} case "custom": poll.Options = []string{} for opt := range strings.SplitSeq(c.PostForm("customOptions"), ",") { poll.Options = append(poll.Options, strings.TrimSpace(opt)) - if !slices.Contains(poll.Options, "Abstain") && (poll.VoteType == database.POLL_TYPE_SIMPLE) { - poll.Options = append(poll.Options, "Abstain") - } } case "pass-fail": default: - poll.Options = []string{"Pass", "Fail", "Abstain"} + poll.Options = []string{"Pass", "Fail"} } if poll.Gatekeep { if !IsEboard(user) { diff --git a/templates/poll.tmpl b/templates/poll.tmpl index af8ade6..cf383ea 100644 --- a/templates/poll.tmpl +++ b/templates/poll.tmpl @@ -6,24 +6,28 @@

{{ .Description | MakeLinks }}

{{ 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 (you have "No Confidence" in the option(s)). You must rank in increasing order from 1, you cannot skip numbers.

+

This is a Ranked Choice vote. Rank the candidates in order of your preference. + 1 is most preferred, and {{ .RankedMax }} is least preferred. + You must rank in increasing order from 1, you cannot skip numbers. + You may leave an option blank if you do not prefer it at all (you have "No Confidence" in the option(s)). + If you like none of the options, you can submit the ballot without ranking any option. + Do not write in a "No Confidence" or "I like none of the candidates" in that case.

{{ end }}

- + {{ if eq .PollType "simple" }} {{ range $i, $option := .Options }} -
+

{{ end }} {{ if .AllowWriteIns }} -
+
+
-
+
@@ -81,12 +85,13 @@ {{ 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.

- + {{ if and (.CanModify) (not .Gatekeep) }}
From e919d49fdc04e1553715c5e59fd0332a82ef08a4 Mon Sep 17 00:00:00 2001 From: Tyler Allen Date: Fri, 27 Nov 2026 19:55:10 -0500 Subject: [PATCH 4/4] add clarifying text to the poll creation template --- templates/create.tmpl | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/templates/create.tmpl b/templates/create.tmpl index 84e823e..7fdcd96 100644 --- a/templates/create.tmpl +++ b/templates/create.tmpl @@ -14,7 +14,10 @@ required >
-
+
+ The title should be short so people can quickly find it. +
+
-
+
+ The description can contain links and other relevant information. +
+
-
+
+
+ The custom options only needs to contain the options that should be on the ballot. Abstain, No Confidence, and the like shouldn't be added. +
Additional Settings
@@ -73,7 +82,7 @@ >
- + {{ if .EBoard }}
E-Board Options
@@ -135,9 +144,9 @@