2018-04-29 19:56:37 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2018-05-12 18:48:18 +00:00
|
|
|
"fmt"
|
2018-04-29 19:56:37 +00:00
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/kirsle/blog/internal/log"
|
|
|
|
"github.com/kirsle/blog/internal/middleware/auth"
|
|
|
|
"github.com/kirsle/blog/internal/render"
|
|
|
|
"github.com/kirsle/blog/internal/responses"
|
2018-05-12 18:48:18 +00:00
|
|
|
"github.com/kirsle/blog/models/comments"
|
2018-04-29 19:56:37 +00:00
|
|
|
"github.com/kirsle/blog/models/events"
|
|
|
|
"github.com/urfave/negroni"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register the blog routes to the app.
|
|
|
|
func Register(r *mux.Router, loginError http.HandlerFunc) {
|
|
|
|
// Login-required routers.
|
|
|
|
loginRouter := mux.NewRouter()
|
|
|
|
loginRouter.HandleFunc("/e/admin/edit", editHandler)
|
2018-05-01 00:50:39 +00:00
|
|
|
loginRouter.HandleFunc("/e/admin/invite/{id}", inviteHandler)
|
2018-04-29 19:56:37 +00:00
|
|
|
loginRouter.HandleFunc("/e/admin/", indexHandler)
|
|
|
|
r.PathPrefix("/e/admin").Handler(
|
|
|
|
negroni.New(
|
|
|
|
negroni.HandlerFunc(auth.LoginRequired(loginError)),
|
|
|
|
negroni.Wrap(loginRouter),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Public routes
|
|
|
|
r.HandleFunc("/e/{fragment}", viewHandler)
|
2018-05-12 04:29:18 +00:00
|
|
|
r.HandleFunc("/c/logout", contactLogoutHandler)
|
|
|
|
r.HandleFunc("/c/{secret}", contactAuthHandler)
|
2018-04-29 19:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Admin index to view all events.
|
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-12 03:15:16 +00:00
|
|
|
result, err := events.All()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error listing all events: %s", err)
|
2018-04-29 19:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(events.ByDate(result)))
|
|
|
|
|
|
|
|
render.Template(w, r, "events/index", map[string]interface{}{
|
|
|
|
"events": result,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// User handler to view a single event page.
|
|
|
|
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := mux.Vars(r)
|
|
|
|
fragment, ok := params["fragment"]
|
|
|
|
if !ok {
|
|
|
|
responses.NotFound(w, r, "Not Found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
event, err := events.LoadFragment(fragment)
|
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndRedirect(w, r, "/", "Event Not Found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:29:18 +00:00
|
|
|
// Template variables.
|
|
|
|
v := map[string]interface{}{
|
|
|
|
"event": event,
|
|
|
|
"authedRSVP": events.RSVP{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the guest list.
|
2018-05-12 03:15:16 +00:00
|
|
|
sort.Sort(events.ByName(event.RSVP))
|
|
|
|
|
2018-05-12 04:29:18 +00:00
|
|
|
// Is the browser session authenticated as a contact?
|
|
|
|
authedContact, err := AuthedContact(r)
|
|
|
|
if err == nil {
|
|
|
|
v["authedContact"] = authedContact
|
|
|
|
|
|
|
|
// Do they have an RSVP?
|
|
|
|
for _, rsvp := range event.RSVP {
|
|
|
|
if rsvp.ContactID == authedContact.ID {
|
|
|
|
v["authedRSVP"] = rsvp
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 19:56:37 +00:00
|
|
|
}
|
2018-05-12 04:29:18 +00:00
|
|
|
|
2018-05-12 18:48:18 +00:00
|
|
|
// Count up the RSVP statuses, and also look for the authed contact's RSVP.
|
|
|
|
var (
|
|
|
|
countGoing int
|
|
|
|
countMaybe int
|
|
|
|
countNotGoing int
|
|
|
|
countInvited int
|
|
|
|
)
|
|
|
|
for _, rsvp := range event.RSVP {
|
|
|
|
if authedContact.ID != 0 && rsvp.ContactID == authedContact.ID {
|
|
|
|
v["authedRVSP"] = rsvp
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rsvp.Status {
|
|
|
|
case events.StatusGoing:
|
|
|
|
countGoing++
|
|
|
|
case events.StatusMaybe:
|
|
|
|
countMaybe++
|
|
|
|
case events.StatusNotGoing:
|
|
|
|
countNotGoing++
|
|
|
|
default:
|
|
|
|
countInvited++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v["countGoing"] = countGoing
|
|
|
|
v["countMaybe"] = countMaybe
|
|
|
|
v["countNotGoing"] = countNotGoing
|
|
|
|
v["countInvited"] = countInvited
|
|
|
|
|
2018-05-12 04:29:18 +00:00
|
|
|
// If we're posting, are we RSVPing?
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
action := r.PostFormValue("action")
|
|
|
|
switch action {
|
|
|
|
case "answer-rsvp":
|
2018-05-12 18:48:18 +00:00
|
|
|
// Subscribe them to the comment thread on this page if we have an email.
|
|
|
|
if authedContact.Email != "" {
|
|
|
|
thread := fmt.Sprintf("event-%d", event.ID)
|
|
|
|
log.Info("events.viewHandler: subscribe email %s to thread %s", authedContact.Email, thread)
|
|
|
|
ml := comments.LoadMailingList()
|
|
|
|
ml.Subscribe(thread, authedContact.Email)
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:29:18 +00:00
|
|
|
answer := r.PostFormValue("submit")
|
|
|
|
for _, rsvp := range event.RSVP {
|
|
|
|
if rsvp.ContactID == authedContact.ID {
|
|
|
|
log.Info("Mark RSVP status %s for contact %s", answer, authedContact.Name())
|
|
|
|
rsvp.Status = answer
|
|
|
|
rsvp.Save()
|
|
|
|
responses.FlashAndReload(w, r, "You have confirmed '%s' for your RSVP.", answer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
responses.FlashAndReload(w, r, "Invalid form action.")
|
|
|
|
}
|
|
|
|
responses.FlashAndReload(w, r, "Unknown error.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:56:37 +00:00
|
|
|
render.Template(w, r, "events/view", v)
|
|
|
|
}
|