Noah
aa8d719fc4
* Add ability to (un)subscribe from comment threads on Forums and Photos. * Creating a forum post, replying to a post or adding a comment to a photo automatically subscribes you to be notified when somebody else adds a comment to the thing later. * At the top of each comment thread is a link to disable or re-enable your subscription. You can join a subscription without even needing to comment. If you click to disable notifications, they stay disabled even if you add another comment later.
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package forum
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"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"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
// Is the current user subscribed to notifications on this thread?
|
|
_, isSubscribed := models.IsSubscribed(currentUser, "threads", thread.ID)
|
|
|
|
var vars = map[string]interface{}{
|
|
"Forum": forum,
|
|
"Thread": thread,
|
|
"Comments": comments,
|
|
"Pager": pager,
|
|
"IsSubscribed": isSubscribed,
|
|
}
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|