Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,29 @@ func GetHomepage(c *gin.Context) {
// A user may be unable to vote but should still be able to see a list of polls
user := GetUserData(c)

polls, err := database.GetOpenPolls(c)
pollResults, err := database.GetOpenPolls(c)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
sort.Slice(polls, func(i, j int) bool {
return polls[i].Id > polls[j].Id
sort.Slice(pollResults, func(i, j int) bool {
return pollResults[i].Id > pollResults[j].Id
})

polls := []*database.Poll{}

for _, poll := range pollResults {
if poll.HideForIneligible {
canVoteResult := canVote(user, *poll, poll.AllowedUsers)

if canVoteResult != 0 && canVoteResult != 9 {
continue
}
}

polls = append(polls, poll)
}

c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Polls": polls,
"Username": user.Username,
Expand Down Expand Up @@ -151,17 +165,18 @@ func CreatePoll(c *gin.Context) {
quorum = quorum / 100

poll := &database.Poll{
Id: "",
CreatedBy: user.Username,
Title: c.PostForm("title"),
Description: c.PostForm("description"),
VoteType: database.POLL_TYPE_SIMPLE,
OpenedTime: time.Now(),
Open: true,
QuorumType: quorum,
Gatekeep: c.PostForm("gatekeep") == "true",
AllowWriteIns: c.PostForm("allowWriteIn") == "true",
Hidden: c.PostForm("hidden") == "true",
Id: "",
CreatedBy: user.Username,
Title: c.PostForm("title"),
Description: c.PostForm("description"),
VoteType: database.POLL_TYPE_SIMPLE,
OpenedTime: time.Now(),
Open: true,
QuorumType: quorum,
Gatekeep: c.PostForm("gatekeep") == "true",
AllowWriteIns: c.PostForm("allowWriteIn") == "true",
Hidden: c.PostForm("hidden") == "true",
HideForIneligible: c.PostForm("hideIneligible") == "true",
}
if c.PostForm("rankedChoice") == "true" {
poll.VoteType = database.POLL_TYPE_RANKED
Expand Down Expand Up @@ -253,6 +268,19 @@ func GetPollResults(c *gin.Context) {

canModify := IsActiveRTP(user) || IsEboard(user) || ownsPoll(poll, user)

if poll.HideForIneligible {
canVoteResult := canVote(user, *poll, poll.AllowedUsers)

if canVoteResult != 0 && canVoteResult != 9 {
c.HTML(http.StatusForbidden, "unauthorized.tmpl", gin.H{
"Username": user.Username,
"FullName": user.FullName,
"EBoard": IsEboard(user),
})
return
}
}

if poll.Hidden && poll.Open {
c.HTML(http.StatusUnauthorized, "hidden.tmpl", gin.H{
"Id": poll.Id,
Expand Down
25 changes: 13 additions & 12 deletions database/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import (
)

type Poll struct {
Id string `bson:"_id,omitempty"`
CreatedBy string `bson:"createdBy"`
Title string `bson:"title"`
Description string `bson:"description"`
VoteType string `bson:"voteType"`
Options []string `bson:"options"`
OpenedTime time.Time `bson:"openedTime"`
Open bool `bson:"open"`
Gatekeep bool `bson:"gatekeep"`
QuorumType float64 `bson:"quorumType"`
AllowedUsers []string `bson:"allowedUsers"`
AllowWriteIns bool `bson:"writeins"`
Id string `bson:"_id,omitempty"`
CreatedBy string `bson:"createdBy"`
Title string `bson:"title"`
Description string `bson:"description"`
VoteType string `bson:"voteType"`
Options []string `bson:"options"`
OpenedTime time.Time `bson:"openedTime"`
Open bool `bson:"open"`
Gatekeep bool `bson:"gatekeep"`
HideForIneligible bool `bson:"hideIneligible"`
QuorumType float64 `bson:"quorumType"`
AllowedUsers []string `bson:"allowedUsers"`
AllowWriteIns bool `bson:"writeins"`

// Prevent this poll from having progress displayed
// This is important for events like elections where the results shouldn't be visible mid vote
Expand Down
9 changes: 9 additions & 0 deletions templates/create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
placeholder="Comma separated list of usernames"
>
</div>
<div class="form-check form-switch fs-5">
<input
class="form-check-input"
type="checkbox"
name="hideIneligible"
value="true"
>
<label for="hidden" class="form-check-label">Hide Results for Members Who Cannot Vote</label>
</div>
</div>
{{ end }}

Expand Down
2 changes: 1 addition & 1 deletion templates/unauthorized.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div class="main p-5 error-page text-center">
<img id="lockdown" src="/static/material_lock.svg" alt="Attention!" width="256"/>
<h1 class="my-4">You're not authorized to vote!</h1>
<h1 class="my-4">You're not authorized to vote or view this poll!</h1>
<p class="fs-4 my-3">
It looks like you're either not marked as active, or you're on co-op
right now
Expand Down
Loading