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 96% rename from api.go rename to routes.go index 8fe6c7c..e5cb218 100644 --- a/api.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) { @@ -340,11 +337,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 +460,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 +488,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 +620,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/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 @@