/* Copyright 2017-2022 CAcert Inc. SPDX-License-Identifier: Apache-2.0 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "html/template" "net/http" "strings" "time" "git.cacert.org/cacert-boardvoting/internal/models" "github.com/Masterminds/sprig/v3" "github.com/gorilla/csrf" ) const motionsPerPage = 10 func (app *application) motionList(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/motions/" { app.notFound(w) return } var err error ctx := r.Context() listOptions := &models.MotionListOptions{Limit: motionsPerPage} const queryParamBefore = "before" const queryParamAfter = "after" if r.URL.Query().Has(queryParamAfter) { var after time.Time err := after.UnmarshalText([]byte(r.URL.Query().Get(queryParamAfter))) if err != nil { app.clientError(w, http.StatusBadRequest) return } listOptions.After = &after } else if r.URL.Query().Has(queryParamBefore) { var before time.Time err := before.UnmarshalText([]byte(r.URL.Query().Get(queryParamBefore))) if err != nil { app.clientError(w, http.StatusBadRequest) return } listOptions.Before = &before } motions, err := app.motions.GetMotions(ctx, listOptions) if err != nil { app.serverError(w, err) return } first, last, err := app.motions.TimestampRange(ctx) if err != nil { app.serverError(w, err) return } files := []string{ "./ui/html/base.html", "./ui/html/partials/nav.html", "./ui/html/partials/pagination.html", "./ui/html/partials/motion_actions.html", "./ui/html/partials/motion_display.html", "./ui/html/partials/motion_status_class.html", "./ui/html/pages/motions.html", } funcMaps := sprig.FuncMap() funcMaps["nl2br"] = func(text string) template.HTML { // #nosec G203 input is sanitized return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "
")) } funcMaps["canMangageUsers"] = func(*models.Voter) bool { return false } funcMaps[csrf.TemplateTag] = func() template.HTML { return csrf.TemplateField(r) } ts, err := template.New("").Funcs(funcMaps).ParseFiles(files...) if err != nil { app.serverError(w, err) return } templateCtx := struct { Voter *models.Voter Flashes []string Params struct { Flags struct { Unvoted bool } } PrevPage, NextPage string Motions []*models.MotionForDisplay }{Motions: motions} if len(motions) > 0 && first.Before(motions[len(motions)-1].Proposed) { marshalled, err := motions[len(motions)-1].Proposed.MarshalText() if err != nil { app.serverError(w, err) return } templateCtx.NextPage = string(marshalled) } if len(motions) > 0 && last.After(motions[0].Proposed) { marshalled, err := motions[0].Proposed.MarshalText() if err != nil { app.serverError(w, err) return } templateCtx.PrevPage = string(marshalled) } err = ts.ExecuteTemplate(w, "base", templateCtx) if err != nil { app.serverError(w, err) } } func (app *application) home(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } if r.Method != "GET" && r.Method != "HEAD" { w.Header().Set("Allow", "GET,HEAD") app.clientError(w, http.StatusMethodNotAllowed) return } http.Redirect(w, r, "/motions/", http.StatusMovedPermanently) }