package handlers import ( "log" "net/http" "strconv" "threadr/models" "github.com/gorilla/sessions" ) 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) 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.URL.Query().Get("to") replyTo := 0 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 } if thread.Type == "chat" { for i, j := 0, len(posts)-1; i < j; i, j = i+1, j-1 { posts[i], posts[j] = posts[j], posts[i] } } data := struct { PageData Thread models.Thread Posts []models.Post }{ PageData: PageData{ Title: "ThreadR - " + thread.Title, Navbar: "boards", LoggedIn: loggedIn, ShowCookieBanner: true, BasePath: app.Config.ThreadrDir, StaticPath: app.Config.ThreadrDir + "/static", CurrentURL: r.URL.Path, }, Thread: *thread, Posts: posts, } 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 } } }