2022-08-31 05:13:57 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2024-01-07 00:44:05 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-31 05:13:57 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Newest posts across all of the (official) forums.
|
|
|
|
func Newest() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("forum/newest.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-02-15 05:38:20 +00:00
|
|
|
// Query parameters.
|
|
|
|
var (
|
|
|
|
allComments = r.FormValue("all") == "true"
|
2024-08-22 04:53:35 +00:00
|
|
|
whichForums = r.FormValue("which")
|
|
|
|
categories = []string{}
|
|
|
|
subscribed bool
|
2024-02-15 05:38:20 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 05:13:57 +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-22 04:53:35 +00:00
|
|
|
// Recall the user's default "Which forum:" answer if not selected.
|
|
|
|
if whichForums == "" {
|
|
|
|
whichForums = currentUser.GetProfileField("forum_newest_default")
|
|
|
|
if whichForums == "" {
|
|
|
|
whichForums = "official"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Narrow down to which set of forums?
|
|
|
|
switch whichForums {
|
|
|
|
case "official":
|
|
|
|
categories = config.ForumCategories
|
|
|
|
case "community":
|
|
|
|
categories = []string{""}
|
|
|
|
case "followed":
|
|
|
|
subscribed = true
|
|
|
|
default:
|
|
|
|
whichForums = "all"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store their "Which forums" filter to be their new default view.
|
|
|
|
currentUser.SetProfileField("forum_newest_default", whichForums)
|
|
|
|
|
2022-08-31 05:13:57 +00:00
|
|
|
// Get all the categorized index forums.
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeThreadList,
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
2024-08-22 04:53:35 +00:00
|
|
|
posts, err := models.PaginateRecentPosts(currentUser, categories, subscribed, allComments, pager)
|
2022-08-31 05:13:57 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate forums: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-07 00:44:05 +00:00
|
|
|
// Get any photo attachments for these comments.
|
|
|
|
var comments = []*models.Comment{}
|
|
|
|
for _, post := range posts {
|
|
|
|
comments = append(comments, post.Comment, &post.Thread.Comment)
|
|
|
|
}
|
|
|
|
photos, err := models.MapCommentPhotos(comments)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't MapCommentPhotos: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-08-31 05:13:57 +00:00
|
|
|
var vars = map[string]interface{}{
|
2024-08-21 02:31:56 +00:00
|
|
|
"CurrentForumTab": "newest",
|
|
|
|
"Pager": pager,
|
|
|
|
"RecentPosts": posts,
|
|
|
|
"PhotoMap": photos,
|
2024-08-22 04:53:35 +00:00
|
|
|
|
|
|
|
// Filter options.
|
|
|
|
"WhichForums": whichForums,
|
|
|
|
"AllComments": allComments,
|
2022-08-31 05:13:57 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|