2024-01-07 00:44:05 +00:00
|
|
|
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")
|
2024-08-24 05:53:40 +00:00
|
|
|
inForum = r.FormValue("in")
|
|
|
|
categories = []string{}
|
2024-01-07 00:44:05 +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]
|
|
|
|
}
|
|
|
|
|
|
|
|
// All comments, or threads only?
|
|
|
|
if postType != "threads" {
|
|
|
|
postType = "all"
|
|
|
|
}
|
|
|
|
|
2024-08-24 05:53:40 +00:00
|
|
|
// In forums
|
|
|
|
switch inForum {
|
|
|
|
case "official":
|
|
|
|
categories = config.ForumCategories
|
|
|
|
case "community":
|
|
|
|
categories = []string{""}
|
|
|
|
}
|
|
|
|
|
2024-01-07 00:44:05 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-08-24 05:53:40 +00:00
|
|
|
posts, err := models.SearchForum(currentUser, categories, search, filters, pager)
|
2024-01-07 00:44:05 +00:00
|
|
|
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{}{
|
2024-08-21 02:31:56 +00:00
|
|
|
"CurrentForumTab": "search",
|
|
|
|
"Pager": pager,
|
|
|
|
"Comments": posts,
|
|
|
|
"ThreadMap": threadMap,
|
|
|
|
"PhotoMap": photos,
|
2024-01-07 00:44:05 +00:00
|
|
|
|
|
|
|
"SearchTerm": searchTerm,
|
|
|
|
"ByUsername": byUsername,
|
|
|
|
"Type": postType,
|
2024-08-24 05:53:40 +00:00
|
|
|
"InForum": inForum,
|
2024-01-07 00:44:05 +00:00
|
|
|
"Sort": sort,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|