website/pkg/controller/forum/search.go
Noah Petherbridge 1bf846e78b Forum Admin Page for Regular Users
Allow regular (non-admin) users access to the Manage Forums page so they can
create and manage their own forums.

Things that were already working:

* The admin forum page was already anticipating regular LoginRequired credential
* Users only see their owned forums, while admins can see and manage ALL forums

Improvements made to the Forum Admin page:

* Change the title color from admin-red to user-blue.
* Add ability to search (filter) and sort the forums.

Other changes:

* Turn the Forum tab bar into a reusable component.
2024-08-23 22:56:40 -07:00

120 lines
2.8 KiB
Go

package forum
import (
"net/http"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/templates"
)
// Search the forums.
func Search() http.HandlerFunc {
tmpl := templates.Must("forum/search.html")
// Whitelist for ordering options.
var sortWhitelist = []string{
"created_at desc",
"created_at asc",
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
searchTerm = r.FormValue("q")
byUsername = r.FormValue("username")
postType = r.FormValue("type")
sort = r.FormValue("sort")
sortOK bool
)
// Sort options.
for _, v := range sortWhitelist {
if sort == v {
sortOK = true
break
}
}
if !sortOK {
sort = sortWhitelist[0]
}
// All comments, or threads only?
if postType != "threads" {
postType = "all"
}
// 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
}
// Filters: find the user ID.
var filterUserID uint64
if byUsername != "" {
user, err := models.FindUser(byUsername)
if err != nil {
session.FlashError(w, r, "Couldn't search posts by that username: no such username found.")
templates.Redirect(w, r.URL.Path)
return
}
filterUserID = user.ID
}
// Parse their search term.
var (
search = models.ParseSearchString(searchTerm)
filters = models.ForumSearchFilters{
UserID: filterUserID,
ThreadsOnly: postType == "threads",
}
pager = &models.Pagination{
Page: 1,
PerPage: config.PageSizeThreadList,
Sort: sort,
}
)
pager.ParsePage(r)
posts, err := models.SearchForum(currentUser, search, filters, pager)
if err != nil {
session.FlashError(w, r, "Couldn't search the forums: %s", err)
templates.Redirect(w, "/")
return
}
// Map the originating threads to each comment.
threadMap, err := models.MapForumCommentThreads(posts)
if err != nil {
log.Error("Couldn't map forum threads to comments: %s", err)
}
// Get any photo attachments for these comments.
photos, err := models.MapCommentPhotos(posts)
if err != nil {
log.Error("Couldn't MapCommentPhotos: %s", err)
}
var vars = map[string]interface{}{
"CurrentForumTab": "search",
"Pager": pager,
"Comments": posts,
"ThreadMap": threadMap,
"PhotoMap": photos,
"SearchTerm": searchTerm,
"ByUsername": byUsername,
"Type": postType,
"Sort": sort,
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}