2022-08-24 05:55:19 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// Regular expressions
|
|
|
|
var (
|
|
|
|
FragmentPattern = `[a-z0-9._-]{1,30}`
|
|
|
|
FragmentRegexp = regexp.MustCompile(
|
|
|
|
fmt.Sprintf(`^(%s)$`, FragmentPattern),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Landing page for forums.
|
|
|
|
func Landing() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("forum/index.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// 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.
|
|
|
|
// XXX: we get a large page size to get ALL official forums
|
|
|
|
var pager = &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: config.PageSizeForums,
|
|
|
|
Sort: "title asc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
forums, err := models.PaginateForums(currentUser, config.ForumCategories, nil, false, pager)
|
2022-08-24 05:55:19 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate forums: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bucket the forums into their categories for easy front-end.
|
|
|
|
categorized := models.CategorizeForums(forums, config.ForumCategories)
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
// Inject the "My List" Category if the user subscribes to forums.
|
|
|
|
myList, err := models.PaginateForums(currentUser, nil, nil, true, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get your followed forums: %s", err)
|
|
|
|
} else {
|
|
|
|
forums = append(forums, myList...)
|
|
|
|
categorized = append([]*models.CategorizedForum{
|
|
|
|
{
|
|
|
|
Category: "My List",
|
|
|
|
Forums: myList,
|
|
|
|
},
|
|
|
|
}, categorized...)
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Map statistics for these forums.
|
|
|
|
forumMap := models.MapForumStatistics(forums)
|
2024-08-21 04:26:53 +00:00
|
|
|
followMap := models.MapForumMemberships(currentUser, forums)
|
2022-08-24 05:55:19 +00:00
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Pager": pager,
|
|
|
|
"Categories": categorized,
|
|
|
|
"ForumMap": forumMap,
|
2024-08-21 04:26:53 +00:00
|
|
|
"FollowMap": followMap,
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|