2022-08-24 05:55:19 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
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-24 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manage page for forums -- admin only for now but may open up later.
|
|
|
|
func Manage() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("forum/admin.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Get the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get current user: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get forums the user owns or can manage.
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeForumAdmin,
|
|
|
|
Sort: "updated_at desc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
|
|
|
forums, err := models.PaginateOwnedForums(currentUser.ID, currentUser.IsAdmin, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate owned forums: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Pager": pager,
|
|
|
|
"Forums": forums,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|