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"
|
2024-08-22 04:53:35 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-26 04:21:46 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// Forum view for a specific board index.
|
|
|
|
func Forum() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("forum/board_index.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the path parameters
|
|
|
|
var (
|
2024-02-11 00:17:15 +00:00
|
|
|
fragment = r.PathValue("fragment")
|
|
|
|
forum *models.Forum
|
2022-08-24 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
2024-02-11 00:17:15 +00:00
|
|
|
// Look up the forum by its fragment.
|
|
|
|
if found, err := models.ForumByFragment(fragment); err != nil {
|
2022-08-24 05:55:19 +00:00
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
} else {
|
2024-02-11 00:17:15 +00:00
|
|
|
forum = found
|
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-05-11 19:23:06 +00:00
|
|
|
// Is it a private forum?
|
2024-08-22 05:25:59 +00:00
|
|
|
if !forum.CanBeSeenBy(currentUser) {
|
2024-05-11 19:23:06 +00:00
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Get the pinned threads.
|
|
|
|
pinned, err := models.PinnedThreads(forum)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get pinned threads: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Get all the categorized index forums.
|
|
|
|
// XXX: we get a large page size to get ALL official forums
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeThreadList,
|
|
|
|
Sort: "updated_at desc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
|
|
|
threads, err := models.PaginateThreads(currentUser, forum, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate threads: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Inject pinned threads on top.
|
|
|
|
threads = append(pinned, threads...)
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Map the statistics (replies, views) of these threads.
|
|
|
|
threadMap := models.MapThreadStatistics(threads)
|
|
|
|
|
2024-08-22 04:53:35 +00:00
|
|
|
// Load the forum's moderators.
|
|
|
|
mods, err := forum.GetModerators()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Getting forum moderators: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
var vars = map[string]interface{}{
|
2024-08-21 04:26:53 +00:00
|
|
|
"Forum": forum,
|
2024-08-22 04:53:35 +00:00
|
|
|
"ForumModerators": mods,
|
2024-08-21 04:26:53 +00:00
|
|
|
"IsForumSubscribed": models.IsForumSubscribed(currentUser, forum),
|
|
|
|
"Threads": threads,
|
|
|
|
"ThreadMap": threadMap,
|
|
|
|
"Pager": pager,
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|