2024-08-21 04:26:53 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-08-22 04:53:35 +00:00
|
|
|
// Explore all existing forums.
|
|
|
|
func Explore() http.HandlerFunc {
|
2024-08-21 04:26:53 +00:00
|
|
|
// This page shares a template with the board index (Categories) page.
|
|
|
|
tmpl := templates.Must("forum/index.html")
|
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
2024-08-22 06:06:06 +00:00
|
|
|
"created_at desc",
|
|
|
|
"created_at asc",
|
2024-08-23 04:57:14 +00:00
|
|
|
"title asc",
|
|
|
|
"title desc",
|
2024-08-27 03:47:14 +00:00
|
|
|
|
|
|
|
// Special sort handlers.
|
|
|
|
// See PaginateForums for expanded handlers for these.
|
2024-08-27 04:36:48 +00:00
|
|
|
"by_followers",
|
2024-08-27 03:47:14 +00:00
|
|
|
"by_latest",
|
|
|
|
"by_threads",
|
|
|
|
"by_posts",
|
|
|
|
"by_users",
|
2024-08-21 04:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
searchTerm = r.FormValue("q")
|
|
|
|
search = models.ParseSearchString(searchTerm)
|
|
|
|
|
|
|
|
show = r.FormValue("show")
|
|
|
|
categories = []string{}
|
|
|
|
|
|
|
|
subscribed = r.FormValue("show") == "followed"
|
|
|
|
|
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
|
|
|
sort = sortWhitelist[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set of forum categories to filter for.
|
|
|
|
switch show {
|
|
|
|
case "official":
|
|
|
|
categories = config.ForumCategories
|
|
|
|
case "community":
|
|
|
|
categories = []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeBrowseForums,
|
|
|
|
Sort: sort,
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
|
|
|
// Browse all forums (no category filter for official)
|
|
|
|
forums, err := models.PaginateForums(currentUser, categories, search, subscribed, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate forums: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bucket the forums into their categories for easy front-end.
|
|
|
|
categorized := models.CategorizeForums(forums, nil)
|
|
|
|
|
|
|
|
// Map statistics for these forums.
|
|
|
|
forumMap := models.MapForumStatistics(forums)
|
|
|
|
followMap := models.MapForumMemberships(currentUser, forums)
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
2024-08-22 04:53:35 +00:00
|
|
|
"CurrentForumTab": "explore",
|
|
|
|
"IsExploreTab": true,
|
2024-08-21 04:26:53 +00:00
|
|
|
"Pager": pager,
|
|
|
|
"Categories": categorized,
|
|
|
|
"ForumMap": forumMap,
|
|
|
|
"FollowMap": followMap,
|
2024-08-27 04:36:48 +00:00
|
|
|
"FollowersMap": models.MapForumFollowers(forums),
|
2024-08-21 04:26:53 +00:00
|
|
|
|
|
|
|
// Search filters
|
|
|
|
"SearchTerm": searchTerm,
|
|
|
|
"Show": show,
|
|
|
|
"Sort": sort,
|
2024-08-23 04:57:14 +00:00
|
|
|
|
|
|
|
// Current viewer's forum quota.
|
|
|
|
"ForumQuota": models.ComputeForumQuota(currentUser),
|
2024-08-21 04:26:53 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|