Noah Petherbridge
a663462e27
Adds initial code for basically functional forums: * Forums landing page shows hard-coded list of Categories along with any forums in the DB that use those categories. * Admin: Create, Edit forums and view forums you own or have admin rights to modify. * Landing page forums list shows the title/description and dynamic count of number of Topics and total number of Posts in each forum. TODO: distinct count of Users who posted in each forum. * Board Index page shows list of Threads (posts) with a Replies count and Views count on each thread. * Thread view is basically an array of Comments. Users can post, edit and delete (their own) comments. Deleting the first comment removes the entire Thread - the thread points to a first Comment to provide its body. * Reply and Quote-Reply options working.
89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package forum
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
|
"git.kirsle.net/apps/gosocial/pkg/session"
|
|
"git.kirsle.net/apps/gosocial/pkg/templates"
|
|
)
|
|
|
|
var ThreadPathRegexp = regexp.MustCompile(`^/forum/thread/(\d+)$`)
|
|
|
|
// Thread view for a specific board index.
|
|
func Thread() http.HandlerFunc {
|
|
tmpl := templates.Must("forum/thread.html")
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Parse the path parameters
|
|
var (
|
|
forum *models.Forum
|
|
thread *models.Thread
|
|
)
|
|
|
|
if m := ThreadPathRegexp.FindStringSubmatch(r.URL.Path); m == nil {
|
|
log.Error("Regexp failed to parse: %s", r.URL.Path)
|
|
templates.NotFoundPage(w, r)
|
|
return
|
|
} else {
|
|
if threadID, err := strconv.Atoi(m[1]); err != nil {
|
|
session.FlashError(w, r, "Invalid thread ID in the address bar.")
|
|
templates.Redirect(w, "/forum")
|
|
return
|
|
} else {
|
|
// Load the thread.
|
|
if found, err := models.GetThread(uint64(threadID)); err != nil {
|
|
session.FlashError(w, r, "That thread does not exist.")
|
|
templates.Redirect(w, "/forum")
|
|
return
|
|
} else {
|
|
thread = found
|
|
forum = &thread.Forum
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// Ping the view count on this thread.
|
|
if err := thread.View(); err != nil {
|
|
log.Error("Couldn't ping view count on thread %d: %s", thread.ID, err)
|
|
}
|
|
|
|
// Paginate the comments on this thread.
|
|
var pager = &models.Pagination{
|
|
Page: 1,
|
|
PerPage: config.PageSizeThreadList,
|
|
Sort: "created_at asc",
|
|
}
|
|
pager.ParsePage(r)
|
|
|
|
comments, err := models.PaginateComments(currentUser, "threads", thread.ID, pager)
|
|
if err != nil {
|
|
session.FlashError(w, r, "Couldn't paginate comments: %s", err)
|
|
templates.Redirect(w, "/")
|
|
return
|
|
}
|
|
|
|
var vars = map[string]interface{}{
|
|
"Forum": forum,
|
|
"Thread": thread,
|
|
"Comments": comments,
|
|
"Pager": pager,
|
|
}
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|