2018-04-29 19:56:37 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2018-05-01 00:50:39 +00:00
|
|
|
"strings"
|
2018-04-29 19:56:37 +00:00
|
|
|
|
2018-05-01 00:50:39 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/kirsle/blog/internal/log"
|
2018-04-29 19:56:37 +00:00
|
|
|
"github.com/kirsle/blog/internal/render"
|
|
|
|
"github.com/kirsle/blog/internal/responses"
|
2018-05-01 00:50:39 +00:00
|
|
|
"github.com/kirsle/blog/models/contacts"
|
2018-04-29 19:56:37 +00:00
|
|
|
"github.com/kirsle/blog/models/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
func inviteHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-01 00:50:39 +00:00
|
|
|
params := mux.Vars(r)
|
|
|
|
id, err := strconv.Atoi(params["id"])
|
2018-04-29 19:56:37 +00:00
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndRedirect(w, r, "/e/admin/", "Invalid ID")
|
|
|
|
return
|
|
|
|
}
|
2018-05-01 00:50:39 +00:00
|
|
|
|
|
|
|
// Load the event from its ID.
|
2018-04-29 19:56:37 +00:00
|
|
|
event, err := events.Load(id)
|
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndRedirect(w, r, "/e/admin/", "Can't load event: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:50:39 +00:00
|
|
|
// Handle POST requests.
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
action := r.FormValue("action")
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "new-contact":
|
|
|
|
c := contacts.NewContact()
|
|
|
|
c.ParseForm(r)
|
|
|
|
err = c.Validate()
|
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndReload(w, r, "Validation error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-12 03:15:16 +00:00
|
|
|
err = contacts.Add(&c)
|
2018-05-01 00:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndReload(w, r, "Error when saving address book: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-12 03:15:16 +00:00
|
|
|
err = event.InviteContactID(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
responses.Flash(w, r, "Error: couldn't invite contact: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
responses.FlashAndReload(w, r, "Added %s to the address book and added to invite list!", c.Name())
|
2018-05-01 00:50:39 +00:00
|
|
|
return
|
|
|
|
case "send-invite":
|
|
|
|
log.Error("Send Invite!")
|
|
|
|
r.ParseForm()
|
|
|
|
contactIDs, ok := r.Form["invite"]
|
|
|
|
if !ok {
|
|
|
|
responses.Error(w, r, "Missing: invite (list of IDs)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invite all the users.
|
|
|
|
var warnings []string
|
|
|
|
for _, strID := range contactIDs {
|
|
|
|
id, _ := strconv.Atoi(strID)
|
2018-05-12 03:15:16 +00:00
|
|
|
err = event.InviteContactID(id)
|
|
|
|
log.Debug("Inviting contact ID %d: err=%s", id, err)
|
2018-05-01 00:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
warnings = append(warnings, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
responses.Flash(w, r, "Warnings: %s", strings.Join(warnings, "; "))
|
|
|
|
}
|
|
|
|
responses.FlashAndReload(w, r, "Invites sent!")
|
|
|
|
return
|
2018-05-12 03:15:16 +00:00
|
|
|
case "revoke-invite":
|
|
|
|
idx, _ := strconv.Atoi(r.FormValue("index"))
|
|
|
|
err := event.Uninvite(idx)
|
|
|
|
if err != nil {
|
|
|
|
responses.FlashAndReload(w, r, "Error deleting the invite: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
responses.FlashAndReload(w, r, "Invite revoked!")
|
|
|
|
return
|
|
|
|
case "notify":
|
|
|
|
// Notify all the invited users!
|
|
|
|
for _, rsvp := range event.RSVP {
|
|
|
|
if !rsvp.Notified || true {
|
|
|
|
log.Info("Notify RSVP %s about Event %s", rsvp.GetName(), event.Title)
|
|
|
|
notifyUser(event, rsvp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
responses.FlashAndReload(w, r, "Notification emails and SMS messages sent out!")
|
|
|
|
return
|
2018-05-01 00:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 03:15:16 +00:00
|
|
|
invited := event.RSVP
|
2018-05-01 00:50:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("error getting event.Invited: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map the invited user IDs.
|
|
|
|
invitedMap := map[int]bool{}
|
|
|
|
for _, rsvp := range invited {
|
|
|
|
if rsvp.ContactID != 0 {
|
|
|
|
invitedMap[rsvp.ContactID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 03:15:16 +00:00
|
|
|
allContacts, err := contacts.All()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("contacts.All() error: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:50:39 +00:00
|
|
|
v := map[string]interface{}{
|
|
|
|
"event": event,
|
|
|
|
"invited": invited,
|
|
|
|
"invitedMap": invitedMap,
|
2018-05-12 03:15:16 +00:00
|
|
|
"contacts": allContacts,
|
2018-05-01 00:50:39 +00:00
|
|
|
}
|
2018-04-29 19:56:37 +00:00
|
|
|
render.Template(w, r, "events/invite", v)
|
|
|
|
}
|