2022-08-10 05:10:47 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// User dashboard or landing page (/me).
|
|
|
|
func Dashboard() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("account/dashboard.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-25 04:17:34 +00:00
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Couldn't get currentUser", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all notifications read?
|
|
|
|
if r.FormValue("intent") == "read-notifications" {
|
|
|
|
models.MarkNotificationsRead(currentUser)
|
|
|
|
session.Flash(w, r, "All of your notifications have been marked as 'read!'")
|
|
|
|
templates.Redirect(w, "/me")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our notifications.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeDashboardNotifications,
|
|
|
|
Sort: "created_at desc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
notifs, err := models.PaginateNotifications(currentUser, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get your notifications: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map our notifications.
|
|
|
|
notifMap := models.MapNotifications(notifs)
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Notifications": notifs,
|
|
|
|
"NotifMap": notifMap,
|
|
|
|
"Pager": pager,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
2022-08-10 05:10:47 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|