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")
|
2024-08-21 02:31:56 +00:00
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
|
|
|
"updated_at desc",
|
|
|
|
"created_at desc",
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-21 02:31:56 +00:00
|
|
|
var (
|
|
|
|
searchTerm = r.FormValue("q")
|
2024-08-23 04:57:14 +00:00
|
|
|
show = r.FormValue("show")
|
|
|
|
categories = []string{}
|
2024-08-21 02:31:56 +00:00
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
|
|
|
sort = sortWhitelist[0]
|
|
|
|
}
|
|
|
|
|
2024-08-23 04:57:14 +00:00
|
|
|
// Show options.
|
|
|
|
if show == "official" {
|
|
|
|
categories = config.ForumCategories
|
|
|
|
} else if show == "community" {
|
|
|
|
categories = []string{""}
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-08-21 02:31:56 +00:00
|
|
|
// Parse their search term.
|
|
|
|
var search = models.ParseSearchString(searchTerm)
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Get forums the user owns or can manage.
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeForumAdmin,
|
2024-08-21 02:31:56 +00:00
|
|
|
Sort: sort,
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
2024-08-24 05:30:59 +00:00
|
|
|
forums, err := models.PaginateOwnedForums(
|
|
|
|
currentUser.ID,
|
|
|
|
currentUser.HasAdminScope(config.ScopeForumAdmin),
|
|
|
|
categories,
|
|
|
|
search,
|
|
|
|
pager,
|
|
|
|
)
|
2022-08-24 05:55:19 +00:00
|
|
|
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,
|
2024-08-21 02:31:56 +00:00
|
|
|
|
2024-08-23 04:57:14 +00:00
|
|
|
// Quote settings.
|
|
|
|
"QuotaLimit": models.ComputeForumQuota(currentUser),
|
|
|
|
"QuotaCount": models.CountOwnedUserForums(currentUser),
|
|
|
|
|
2024-08-21 02:31:56 +00:00
|
|
|
// Search filters.
|
|
|
|
"SearchTerm": searchTerm,
|
2024-08-23 04:57:14 +00:00
|
|
|
"Show": show,
|
2024-08-21 02:31:56 +00:00
|
|
|
"Sort": sort,
|
2022-08-24 05:55:19 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|