2022-08-24 05:55:19 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"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"
|
2022-08-24 05:55:19 +00:00
|
|
|
)
|
|
|
|
|
2022-08-30 03:00:15 +00:00
|
|
|
// Thread view for the comment thread body of a forum post.
|
2022-08-24 05:55:19 +00:00
|
|
|
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 (
|
2024-02-11 00:17:15 +00:00
|
|
|
idStr = r.PathValue("id")
|
2022-08-24 05:55:19 +00:00
|
|
|
forum *models.Forum
|
|
|
|
thread *models.Thread
|
|
|
|
)
|
|
|
|
|
2024-02-11 00:17:15 +00:00
|
|
|
if idStr == "" {
|
2022-08-24 05:55:19 +00:00
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
} else {
|
2024-02-11 00:17:15 +00:00
|
|
|
if threadID, err := strconv.Atoi(idStr); err != nil {
|
2022-08-24 05:55:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
// Can we moderate this forum? (from a user-owned forum perspective,
|
|
|
|
// e.g. can we delete threads and posts, not edit them)
|
2024-08-22 05:25:59 +00:00
|
|
|
var canModerate = forum.CanBeModeratedBy(currentUser)
|
2024-08-21 04:26:53 +00:00
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
// Ping the view count on this thread.
|
2022-08-31 05:13:57 +00:00
|
|
|
if err := thread.View(currentUser.ID); err != nil {
|
2022-08-24 05:55:19 +00:00
|
|
|
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)
|
|
|
|
|
2024-08-29 01:42:49 +00:00
|
|
|
comments, err := models.PaginateComments(currentUser, "threads", thread.ID, canModerate, pager)
|
2022-08-24 05:55:19 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't paginate comments: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 03:00:15 +00:00
|
|
|
// Get the like map for these comments.
|
|
|
|
commentIDs := []uint64{}
|
|
|
|
for _, com := range comments {
|
|
|
|
commentIDs = append(commentIDs, com.ID)
|
|
|
|
}
|
|
|
|
commentLikeMap := models.MapLikes(currentUser, "comments", commentIDs)
|
|
|
|
|
2022-10-21 04:02:30 +00:00
|
|
|
// Get any photo attachments for these comments.
|
|
|
|
photos, err := models.MapCommentPhotos(comments)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't MapCommentPhotos: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-08-27 18:42:48 +00:00
|
|
|
// Is the current user subscribed to notifications on this thread?
|
|
|
|
_, isSubscribed := models.IsSubscribed(currentUser, "threads", thread.ID)
|
|
|
|
|
2022-08-24 05:55:19 +00:00
|
|
|
var vars = map[string]interface{}{
|
2024-08-22 04:53:35 +00:00
|
|
|
"Forum": forum,
|
|
|
|
"Thread": thread,
|
|
|
|
"Comments": comments,
|
|
|
|
"LikeMap": commentLikeMap,
|
|
|
|
"PhotoMap": photos,
|
|
|
|
"Pager": pager,
|
|
|
|
"CanModerate": canModerate,
|
|
|
|
"IsSubscribed": isSubscribed,
|
|
|
|
"IsForumSubscribed": models.IsForumSubscribed(currentUser, forum),
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|