diff --git a/.gitignore b/.gitignore index fac86fa..20e2001 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env* vote -.idea/ \ No newline at end of file +.idea/ +.vscode/ \ No newline at end of file diff --git a/constitutional.go b/constitutional.go index 7528b34..4fc412c 100644 --- a/constitutional.go +++ b/constitutional.go @@ -49,8 +49,8 @@ func InitConstitution() { startOfDay := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) nextMidnight := startOfDay.AddDate(0, 0, 1) fmt.Println(nextMidnight) - fmt.Println(nextMidnight.Sub(time.Now())) - ticker := time.NewTicker(nextMidnight.Sub(time.Now())) + fmt.Println(time.Until(nextMidnight)) + ticker := time.NewTicker(time.Until(nextMidnight)) first := true go func() { for { diff --git a/database/database.go b/database/database.go index d03bc42..238fe85 100644 --- a/database/database.go +++ b/database/database.go @@ -21,7 +21,7 @@ const ( Updated UpsertResult = 1 ) -var Client *mongo.Client = Connect() +var Client *mongo.Client var db = "" func Connect() *mongo.Client { @@ -33,7 +33,7 @@ func Connect() *mongo.Client { logging.Logger.WithFields(logrus.Fields{"module": "database", "method": "Connect"}).Info("beginning database connection") - ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() uri := os.Getenv("VOTE_MONGODB_URI") @@ -54,7 +54,7 @@ func Connect() *mongo.Client { } func Disconnect() { - ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := Client.Disconnect(ctx); err != nil { diff --git a/go.mod b/go.mod index c16c793..2f6a5d5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.1 require ( github.com/computersciencehouse/csh-auth v0.1.0 github.com/gin-gonic/gin v1.11.0 + github.com/joho/godotenv v1.5.1 github.com/sirupsen/logrus v1.9.3 github.com/slack-go/slack v0.17.3 github.com/stretchr/testify v1.11.1 diff --git a/go.sum b/go.sum index b325fdf..778f6f7 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co= diff --git a/main.go b/main.go index a5f1a2a..cccf7dc 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/computersciencehouse/vote/logging" "github.com/computersciencehouse/vote/sse" "github.com/gin-gonic/gin" + "github.com/joho/godotenv" "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson/primitive" "mvdan.cc/xurls/v2" @@ -57,6 +58,8 @@ func MakeLinks(s string) template.HTML { var oidcClient = OIDCClient{} func main() { + godotenv.Load() + database.Client = database.Connect() r := gin.Default() r.StaticFS("/static", http.Dir("static")) r.SetFuncMap(template.FuncMap{ @@ -126,7 +129,34 @@ func main() { closedPolls = uniquePolls(closedPolls) c.HTML(200, "index.tmpl", gin.H{ - "Polls": polls, + "Polls": polls, + "Username": claims.UserInfo.Username, + "FullName": claims.UserInfo.FullName, + }) + })) + + r.GET("/closed", csh.AuthWrapper(func(c *gin.Context) { + cl, _ := c.Get("cshauth") + claims := cl.(cshAuth.CSHClaims) + + closedPolls, err := database.GetClosedVotedPolls(c, claims.UserInfo.Username) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + ownedPolls, err := database.GetClosedOwnedPolls(c, claims.UserInfo.Username) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + closedPolls = append(closedPolls, ownedPolls...) + + sort.Slice(closedPolls, func(i, j int) bool { + return closedPolls[i].Id > closedPolls[j].Id + }) + closedPolls = uniquePolls(closedPolls) + + c.HTML(200, "closed.tmpl", gin.H{ "ClosedPolls": closedPolls, "Username": claims.UserInfo.Username, "FullName": claims.UserInfo.FullName, @@ -430,6 +460,7 @@ func main() { "IsOpen": poll.Open, "IsHidden": poll.Hidden, "CanModify": canModify, + "CanVote": canVote(claims.UserInfo, *poll, poll.AllowedUsers), "Username": claims.UserInfo.Username, "FullName": claims.UserInfo.FullName, "Gatekeep": poll.Gatekeep, diff --git a/templates/closed.tmpl b/templates/closed.tmpl new file mode 100644 index 0000000..c0a02f9 --- /dev/null +++ b/templates/closed.tmpl @@ -0,0 +1,51 @@ + + + + CSH Vote + + + + + + + + + + + {{ template "nav" . }} +
+

+
Closed Polls
+

+
+
+ +
+
+ + diff --git a/templates/create.tmpl b/templates/create.tmpl index 1fbeeba..e034c19 100644 --- a/templates/create.tmpl +++ b/templates/create.tmpl @@ -4,21 +4,14 @@ CSH Vote + + + - + {{ template "nav" . }} +

Create Poll

diff --git a/templates/hidden.tmpl b/templates/hidden.tmpl index e7a0969..c3f88a5 100644 --- a/templates/hidden.tmpl +++ b/templates/hidden.tmpl @@ -8,6 +8,9 @@ href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css" media="screen" /> + + + - - + {{ template "nav" . }} +
+ + + - - + {{ template "nav" . }} +

Active Polls
@@ -47,43 +39,16 @@ class="list-group-item list-group-item-action" href="/poll/{{ $poll.Id }}" > - {{ - $poll.ShortDescription - }} - - (created by {{ $poll.CreatedBy }}) - - - {{ - end - }} - -

-
-

Closed Polls

-
-
-
diff --git a/templates/nav.tmpl b/templates/nav.tmpl new file mode 100644 index 0000000..0942983 --- /dev/null +++ b/templates/nav.tmpl @@ -0,0 +1,36 @@ +{{ define "nav" }} + + +{{ end }} \ No newline at end of file diff --git a/templates/poll.tmpl b/templates/poll.tmpl index a501a09..95196be 100644 --- a/templates/poll.tmpl +++ b/templates/poll.tmpl @@ -4,6 +4,9 @@ CSH Vote + + + - + {{ template "nav" . }}

{{ .ShortDescription }}

diff --git a/templates/result.tmpl b/templates/result.tmpl index 9e81f54..0ec7944 100644 --- a/templates/result.tmpl +++ b/templates/result.tmpl @@ -8,23 +8,48 @@ href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css" media="screen" /> + + + + + - + {{ template "nav" . }}
+ {{ if eq .CanVote 9 }} + + {{ else if eq .CanVote 1 }} + + {{ else if gt .CanVote 1 }} + + {{ end }} +

{{ .ShortDescription }}

{{ if .LongDescription }}

{{ .LongDescription | MakeLinks }}

@@ -38,6 +63,7 @@

Results will be available once the poll closes

{{ else }} + {{/* Displays information about required quorum and number of voters */}}
Number of Eligible Voters: {{ len .EligibleVoters }}
diff --git a/templates/unauthorized.tmpl b/templates/unauthorized.tmpl index 8d18de1..2134be7 100644 --- a/templates/unauthorized.tmpl +++ b/templates/unauthorized.tmpl @@ -8,6 +8,9 @@ href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css" media="screen" /> + + + - + {{ template "nav" . }}