threadr.lostcave.ddnss.de/handlers/thread.go

182 lines
5.1 KiB
Go

package handlers
import (
"github.com/gorilla/sessions"
"log"
"net/http"
"strconv"
"threadr/models"
)
func ThreadHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session)
loggedIn := session.Values["user_id"] != nil
userID, _ := session.Values["user_id"].(int)
cookie, _ := r.Cookie("threadr_cookie_banner")
threadIDStr := r.URL.Query().Get("id")
threadID, err := strconv.Atoi(threadIDStr)
if err != nil {
http.Error(w, "Invalid thread ID", http.StatusBadRequest)
return
}
thread, err := models.GetThreadByID(app.DB, threadID)
if err != nil {
log.Printf("Error fetching thread: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if thread == nil {
http.Error(w, "Thread not found", http.StatusNotFound)
return
}
board, err := models.GetBoardByID(app.DB, thread.BoardID)
if err != nil {
log.Printf("Error fetching board: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if board.Private {
if !loggedIn {
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
return
}
hasPerm, err := models.HasBoardPermission(app.DB, userID, board.ID, models.PermViewBoard)
if err != nil {
log.Printf("Error checking permission: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if !hasPerm {
http.Error(w, "You do not have permission to view this board", http.StatusForbidden)
return
}
}
if r.Method == http.MethodPost && loggedIn {
action := r.URL.Query().Get("action")
if action == "submit" {
content := r.FormValue("content")
replyToStr := r.FormValue("reply_to")
if replyToStr == "" {
replyToStr = r.URL.Query().Get("to")
}
replyTo := -1
if replyToStr != "" {
replyTo, err = strconv.Atoi(replyToStr)
if err != nil {
http.Error(w, "Invalid reply_to ID", http.StatusBadRequest)
return
}
}
if content == "" {
http.Error(w, "Content cannot be empty", http.StatusBadRequest)
return
}
if board.Private {
hasPerm, err := models.HasBoardPermission(app.DB, userID, board.ID, models.PermPostInBoard)
if err != nil {
log.Printf("Error checking permission: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if !hasPerm {
http.Error(w, "You do not have permission to post in this board", http.StatusForbidden)
return
}
}
post := models.Post{
ThreadID: threadID,
UserID: userID,
Content: content,
ReplyTo: replyTo,
}
err = models.CreatePost(app.DB, post)
if err != nil {
log.Printf("Error creating post: %v", err)
http.Error(w, "Failed to create post", http.StatusInternalServerError)
return
}
http.Redirect(w, r, app.Config.ThreadrDir+"/thread/?id="+threadIDStr, http.StatusFound)
return
}
}
posts, err := models.GetPostsByThreadID(app.DB, threadID)
if err != nil {
log.Printf("Error fetching posts: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Collect post IDs for bulk queries
postIDs := make([]int, len(posts))
userIDs := make(map[int]bool)
for i, p := range posts {
postIDs[i] = p.ID
userIDs[p.UserID] = true
}
// Fetch like counts for all posts
likeCounts, err := models.GetLikeCountsForPosts(app.DB, postIDs)
if err != nil {
log.Printf("Error fetching like counts: %v", err)
likeCounts = make(map[int]models.LikeCounts)
}
// Fetch current user's likes
userLikes, err := models.GetUserLikesForPosts(app.DB, userID, postIDs)
if err != nil {
log.Printf("Error fetching user likes: %v", err)
userLikes = make(map[int]string)
}
// Fetch usernames for post authors
usernames := make(map[int]string)
for uid := range userIDs {
user, err := models.GetUserByID(app.DB, uid)
if err != nil || user == nil {
usernames[uid] = "Unknown"
} else if user.DisplayName != "" {
usernames[uid] = user.DisplayName
} else {
usernames[uid] = user.Username
}
}
data := struct {
PageData
Thread models.Thread
Board models.Board
Posts []models.Post
LikeCounts map[int]models.LikeCounts
UserLikes map[int]string
Usernames map[int]string
}{
PageData: PageData{
Title: "ThreadR - " + thread.Title,
Navbar: "boards",
LoggedIn: loggedIn,
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.RequestURI(),
},
Thread: *thread,
Board: *board,
Posts: posts,
LikeCounts: likeCounts,
UserLikes: userLikes,
Usernames: usernames,
}
if err := app.Tmpl.ExecuteTemplate(w, "thread", data); err != nil {
log.Printf("Error executing template in ThreadHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
}