106 lines
4.0 KiB
Go
106 lines
4.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"threadr/models"
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
func BoardsHandler(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
|
|
cookie, _ := r.Cookie("threadr_cookie_banner")
|
|
userID, _ := session.Values["user_id"].(int)
|
|
isAdmin := false
|
|
|
|
if loggedIn {
|
|
user, err := models.GetUserByID(app.DB, userID)
|
|
if err != nil {
|
|
log.Printf("Error fetching user: %v", err)
|
|
} else if user != nil {
|
|
isAdmin = models.HasGlobalPermission(user, models.PermCreateBoard)
|
|
}
|
|
}
|
|
|
|
if r.Method == http.MethodPost && loggedIn && isAdmin {
|
|
name := r.FormValue("name")
|
|
description := r.FormValue("description")
|
|
if name == "" {
|
|
http.Error(w, "Board name is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
board := models.Board{
|
|
Name: name,
|
|
Description: description,
|
|
Private: false,
|
|
PublicVisible: true,
|
|
}
|
|
query := "INSERT INTO boards (name, description, private, public_visible) VALUES (?, ?, ?, ?)"
|
|
result, err := app.DB.Exec(query, board.Name, board.Description, board.Private, board.PublicVisible)
|
|
if err != nil {
|
|
log.Printf("Error creating board: %v", err)
|
|
http.Error(w, "Failed to create board", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
boardID, _ := result.LastInsertId()
|
|
http.Redirect(w, r, app.Config.ThreadrDir+"/board/?id="+strconv.FormatInt(boardID, 10), http.StatusFound)
|
|
return
|
|
}
|
|
|
|
publicBoards, err := models.GetAllBoards(app.DB, false)
|
|
if err != nil {
|
|
log.Printf("Error fetching public boards: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var privateBoards []models.Board
|
|
if loggedIn {
|
|
privateBoards, err = models.GetAllBoards(app.DB, true)
|
|
if err != nil {
|
|
log.Printf("Error fetching private boards: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var accessiblePrivateBoards []models.Board
|
|
for _, board := range privateBoards {
|
|
hasPerm, err := models.HasBoardPermission(app.DB, userID, board.ID, models.PermViewBoard)
|
|
if err != nil {
|
|
log.Printf("Error checking permission: %v", err)
|
|
continue
|
|
}
|
|
if hasPerm {
|
|
accessiblePrivateBoards = append(accessiblePrivateBoards, board)
|
|
}
|
|
}
|
|
privateBoards = accessiblePrivateBoards
|
|
}
|
|
|
|
data := struct {
|
|
PageData
|
|
PublicBoards []models.Board
|
|
PrivateBoards []models.Board
|
|
IsAdmin bool
|
|
}{
|
|
PageData: PageData{
|
|
Title: "ThreadR - Boards",
|
|
Navbar: "boards",
|
|
LoggedIn: loggedIn,
|
|
ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
|
|
BasePath: app.Config.ThreadrDir,
|
|
StaticPath: app.Config.ThreadrDir + "/static",
|
|
CurrentURL: r.URL.Path,
|
|
},
|
|
PublicBoards: publicBoards,
|
|
PrivateBoards: privateBoards,
|
|
IsAdmin: isAdmin,
|
|
}
|
|
if err := app.Tmpl.ExecuteTemplate(w, "boards", data); err != nil {
|
|
log.Printf("Error executing template in BoardsHandler: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
} |