Skip to content

Commit 6893438

Browse files
committed
feat: add ability hide votes if you cannot vote
1 parent b971033 commit 6893438

4 files changed

Lines changed: 67 additions & 27 deletions

File tree

‎api.go‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,31 @@ func GetHomepage(c *gin.Context) {
3838
// A user may be unable to vote but should still be able to see a list of polls
3939
user := GetUserData(c)
4040

41-
polls, err := database.GetOpenPolls(c)
41+
pollResults, err := database.GetOpenPolls(c)
4242
if err != nil {
4343
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
4444
return
4545
}
46-
sort.Slice(polls, func(i, j int) bool {
47-
return polls[i].Id > polls[j].Id
46+
sort.Slice(pollResults, func(i, j int) bool {
47+
return pollResults[i].Id > pollResults[j].Id
4848
})
4949

50+
polls := []*database.Poll{}
51+
52+
for i := range pollResults {
53+
poll := pollResults[i]
54+
55+
if poll.HideForIneligible {
56+
canVoteResult := canVote(user, *poll, poll.AllowedUsers)
57+
58+
if canVoteResult != 0 && canVoteResult != 9 {
59+
continue
60+
}
61+
}
62+
63+
polls = append(polls, pollResults[i])
64+
}
65+
5066
c.HTML(http.StatusOK, "index.tmpl", gin.H{
5167
"Polls": polls,
5268
"Username": user.Username,
@@ -151,17 +167,18 @@ func CreatePoll(c *gin.Context) {
151167
quorum = quorum / 100
152168

153169
poll := &database.Poll{
154-
Id: "",
155-
CreatedBy: user.Username,
156-
Title: c.PostForm("title"),
157-
Description: c.PostForm("description"),
158-
VoteType: database.POLL_TYPE_SIMPLE,
159-
OpenedTime: time.Now(),
160-
Open: true,
161-
QuorumType: quorum,
162-
Gatekeep: c.PostForm("gatekeep") == "true",
163-
AllowWriteIns: c.PostForm("allowWriteIn") == "true",
164-
Hidden: c.PostForm("hidden") == "true",
170+
Id: "",
171+
CreatedBy: user.Username,
172+
Title: c.PostForm("title"),
173+
Description: c.PostForm("description"),
174+
VoteType: database.POLL_TYPE_SIMPLE,
175+
OpenedTime: time.Now(),
176+
Open: true,
177+
QuorumType: quorum,
178+
Gatekeep: c.PostForm("gatekeep") == "true",
179+
AllowWriteIns: c.PostForm("allowWriteIn") == "true",
180+
Hidden: c.PostForm("hidden") == "true",
181+
HideForIneligible: c.PostForm("hideIneligible") == "true",
165182
}
166183
if c.PostForm("rankedChoice") == "true" {
167184
poll.VoteType = database.POLL_TYPE_RANKED
@@ -253,6 +270,19 @@ func GetPollResults(c *gin.Context) {
253270

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

273+
if poll.HideForIneligible {
274+
canVoteResult := canVote(user, *poll, poll.AllowedUsers)
275+
276+
if canVoteResult != 0 && canVoteResult != 9 {
277+
c.HTML(http.StatusForbidden, "unauthorized.tmpl", gin.H{
278+
"Username": user.Username,
279+
"FullName": user.FullName,
280+
"EBoard": IsEboard(user),
281+
})
282+
return
283+
}
284+
}
285+
256286
if poll.Hidden && poll.Open {
257287
c.HTML(http.StatusUnauthorized, "hidden.tmpl", gin.H{
258288
"Id": poll.Id,

‎database/poll.go‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ import (
1616
)
1717

1818
type Poll struct {
19-
Id string `bson:"_id,omitempty"`
20-
CreatedBy string `bson:"createdBy"`
21-
Title string `bson:"title"`
22-
Description string `bson:"description"`
23-
VoteType string `bson:"voteType"`
24-
Options []string `bson:"options"`
25-
OpenedTime time.Time `bson:"openedTime"`
26-
Open bool `bson:"open"`
27-
Gatekeep bool `bson:"gatekeep"`
28-
QuorumType float64 `bson:"quorumType"`
29-
AllowedUsers []string `bson:"allowedUsers"`
30-
AllowWriteIns bool `bson:"writeins"`
19+
Id string `bson:"_id,omitempty"`
20+
CreatedBy string `bson:"createdBy"`
21+
Title string `bson:"title"`
22+
Description string `bson:"description"`
23+
VoteType string `bson:"voteType"`
24+
Options []string `bson:"options"`
25+
OpenedTime time.Time `bson:"openedTime"`
26+
Open bool `bson:"open"`
27+
Gatekeep bool `bson:"gatekeep"`
28+
HideForIneligible bool `bson:"hideIneligible"`
29+
QuorumType float64 `bson:"quorumType"`
30+
AllowedUsers []string `bson:"allowedUsers"`
31+
AllowWriteIns bool `bson:"writeins"`
3132

3233
// Prevent this poll from having progress displayed
3334
// This is important for events like elections where the results shouldn't be visible mid vote

‎templates/create.tmpl‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@
110110
placeholder="Comma separated list of usernames"
111111
>
112112
</div>
113+
<div class="form-check form-switch fs-5">
114+
<input
115+
class="form-check-input"
116+
type="checkbox"
117+
name="hideIneligible"
118+
value="true"
119+
>
120+
<label for="hidden" class="form-check-label">Hide Results for Members Who Cannot Vote</label>
121+
</div>
113122
</div>
114123
{{ end }}
115124

‎templates/unauthorized.tmpl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

0 commit comments

Comments
 (0)