27540534ad
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.
67 lines
1.7 KiB
Go
67 lines
1.7 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"
|
|
)
|
|
|
|
// 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) {
|
|
// Query parameters.
|
|
var (
|
|
allComments = r.FormValue("all") == "true"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// Get all the categorized index forums.
|
|
var pager = &models.Pagination{
|
|
Page: 1,
|
|
PerPage: config.PageSizeThreadList,
|
|
}
|
|
pager.ParsePage(r)
|
|
|
|
posts, err := models.PaginateRecentPosts(currentUser, config.ForumCategories, allComments, pager)
|
|
if err != nil {
|
|
session.FlashError(w, r, "Couldn't paginate forums: %s", err)
|
|
templates.Redirect(w, "/")
|
|
return
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
var vars = map[string]interface{}{
|
|
"CurrentForumTab": "newest",
|
|
"Pager": pager,
|
|
"RecentPosts": posts,
|
|
"PhotoMap": photos,
|
|
"AllComments": allComments,
|
|
}
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|