Fix accept cookie button and reestyle the reply button

jocadbz
Joca 2026-02-23 20:38:03 -03:00
parent 13b0821eef
commit 6e6eba2ca1
Signed by: jocadbz
GPG Key ID: B1836DCE2F50BDF7
14 changed files with 543 additions and 523 deletions

View File

@ -1,46 +1,46 @@
package handlers package handlers
import ( import (
"io/ioutil" "github.com/gorilla/sessions"
"log" "html/template"
"net/http" "io/ioutil"
"github.com/gorilla/sessions" "log"
"html/template" "net/http"
) )
func AboutHandler(app *App) http.HandlerFunc { func AboutHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
loggedIn := session.Values["user_id"] != nil loggedIn := session.Values["user_id"] != nil
cookie, _ := r.Cookie("threadr_cookie_banner") cookie, _ := r.Cookie("threadr_cookie_banner")
aboutContent, err := ioutil.ReadFile("config/about_page.htmlbody") aboutContent, err := ioutil.ReadFile("config/about_page.htmlbody")
if err != nil { if err != nil {
log.Printf("Error reading about_page.htmlbody: %v", err) log.Printf("Error reading about_page.htmlbody: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
data := struct { data := struct {
PageData PageData
AboutContent template.HTML AboutContent template.HTML
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - About", Title: "ThreadR - About",
Navbar: "about", Navbar: "about",
LoggedIn: loggedIn, LoggedIn: loggedIn,
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
AboutContent: template.HTML(aboutContent), AboutContent: template.HTML(aboutContent),
} }
if err := app.Tmpl.ExecuteTemplate(w, "about", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "about", data); err != nil {
log.Printf("Error executing template in AboutHandler: %v", err) log.Printf("Error executing template in AboutHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -1,131 +1,131 @@
package handlers package handlers
import ( import (
"log" "github.com/gorilla/sessions"
"net/http" "log"
"strconv" "net/http"
"threadr/models" "strconv"
"github.com/gorilla/sessions" "threadr/models"
) )
func BoardHandler(app *App) http.HandlerFunc { func BoardHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
loggedIn := session.Values["user_id"] != nil loggedIn := session.Values["user_id"] != nil
userID, _ := session.Values["user_id"].(int) userID, _ := session.Values["user_id"].(int)
cookie, _ := r.Cookie("threadr_cookie_banner") cookie, _ := r.Cookie("threadr_cookie_banner")
boardIDStr := r.URL.Query().Get("id") boardIDStr := r.URL.Query().Get("id")
boardID, err := strconv.Atoi(boardIDStr) boardID, err := strconv.Atoi(boardIDStr)
if err != nil { if err != nil {
http.Error(w, "Invalid board ID", http.StatusBadRequest) http.Error(w, "Invalid board ID", http.StatusBadRequest)
return return
} }
board, err := models.GetBoardByID(app.DB, boardID) board, err := models.GetBoardByID(app.DB, boardID)
if err != nil { if err != nil {
log.Printf("Error fetching board: %v", err) log.Printf("Error fetching board: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if board == nil { if board == nil {
http.Error(w, "Board not found", http.StatusNotFound) http.Error(w, "Board not found", http.StatusNotFound)
return return
} }
if board.Type == "chat" { if board.Type == "chat" {
http.Redirect(w, r, app.Config.ThreadrDir+"/chat/?id="+boardIDStr, http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/chat/?id="+boardIDStr, http.StatusFound)
return return
} }
if board.Private { if board.Private {
if !loggedIn { if !loggedIn {
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
return return
} }
hasPerm, err := models.HasBoardPermission(app.DB, userID, boardID, models.PermViewBoard) hasPerm, err := models.HasBoardPermission(app.DB, userID, boardID, models.PermViewBoard)
if err != nil { if err != nil {
log.Printf("Error checking permission: %v", err) log.Printf("Error checking permission: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if !hasPerm { if !hasPerm {
http.Error(w, "You do not have permission to view this board", http.StatusForbidden) http.Error(w, "You do not have permission to view this board", http.StatusForbidden)
return return
} }
} }
if r.Method == http.MethodPost && loggedIn { if r.Method == http.MethodPost && loggedIn {
action := r.URL.Query().Get("action") action := r.URL.Query().Get("action")
if action == "create_thread" { if action == "create_thread" {
title := r.FormValue("title") title := r.FormValue("title")
if title == "" { if title == "" {
http.Error(w, "Thread title is required", http.StatusBadRequest) http.Error(w, "Thread title is required", http.StatusBadRequest)
return return
} }
if board.Private { if board.Private {
hasPerm, err := models.HasBoardPermission(app.DB, userID, boardID, models.PermPostInBoard) hasPerm, err := models.HasBoardPermission(app.DB, userID, boardID, models.PermPostInBoard)
if err != nil { if err != nil {
log.Printf("Error checking permission: %v", err) log.Printf("Error checking permission: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if !hasPerm { if !hasPerm {
http.Error(w, "You do not have permission to post in this board", http.StatusForbidden) http.Error(w, "You do not have permission to post in this board", http.StatusForbidden)
return return
} }
} }
thread := models.Thread{ thread := models.Thread{
BoardID: boardID, BoardID: boardID,
Title: title, Title: title,
CreatedByUserID: userID, CreatedByUserID: userID,
} }
err = models.CreateThread(app.DB, thread) err = models.CreateThread(app.DB, thread)
if err != nil { if err != nil {
log.Printf("Error creating thread: %v", err) log.Printf("Error creating thread: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
var threadID int var threadID int
err = app.DB.QueryRow("SELECT LAST_INSERT_ID()").Scan(&threadID) err = app.DB.QueryRow("SELECT LAST_INSERT_ID()").Scan(&threadID)
if err != nil { if err != nil {
log.Printf("Error getting last insert id: %v", err) log.Printf("Error getting last insert id: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
http.Redirect(w, r, app.Config.ThreadrDir+"/thread/?id="+strconv.Itoa(threadID), http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/thread/?id="+strconv.Itoa(threadID), http.StatusFound)
return return
} }
} }
threads, err := models.GetThreadsByBoardID(app.DB, boardID) threads, err := models.GetThreadsByBoardID(app.DB, boardID)
if err != nil { if err != nil {
log.Printf("Error fetching threads: %v", err) log.Printf("Error fetching threads: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
data := struct { data := struct {
PageData PageData
Board models.Board Board models.Board
Threads []models.Thread Threads []models.Thread
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - " + board.Name, Title: "ThreadR - " + board.Name,
Navbar: "boards", Navbar: "boards",
LoggedIn: loggedIn, LoggedIn: loggedIn,
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Board: *board, Board: *board,
Threads: threads, Threads: threads,
} }
if err := app.Tmpl.ExecuteTemplate(w, "board", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "board", data); err != nil {
log.Printf("Error executing template in BoardHandler: %v", err) log.Printf("Error executing template in BoardHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

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

View File

@ -237,7 +237,7 @@ func ChatHandler(app *App) http.HandlerFunc {
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
ContentTemplate: "chat-content", ContentTemplate: "chat-content",
BodyClass: "chat-page", BodyClass: "chat-page",
}, },

View File

@ -1,34 +1,34 @@
package handlers package handlers
import ( import (
"log" "github.com/gorilla/sessions"
"net/http" "log"
"path/filepath" "net/http"
"github.com/gorilla/sessions" "path/filepath"
) )
func HomeHandler(app *App) http.HandlerFunc { func HomeHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
loggedIn := session.Values["user_id"] != nil loggedIn := session.Values["user_id"] != nil
cookie, _ := r.Cookie("threadr_cookie_banner") cookie, _ := r.Cookie("threadr_cookie_banner")
data := struct { data := struct {
PageData PageData
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - Home", Title: "ThreadR - Home",
Navbar: "home", Navbar: "home",
LoggedIn: loggedIn, LoggedIn: loggedIn,
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: filepath.Join(app.Config.ThreadrDir, "static"), StaticPath: filepath.Join(app.Config.ThreadrDir, "static"),
CurrentURL: r.URL.String(), CurrentURL: r.URL.RequestURI(),
}, },
} }
if err := app.Tmpl.ExecuteTemplate(w, "home", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "home", data); err != nil {
log.Printf("Error executing template in HomeHandler: %v", err) log.Printf("Error executing template in HomeHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -52,7 +52,7 @@ func LoginHandler(app *App) http.HandlerFunc {
LoggedIn: false, LoggedIn: false,
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Error: "", Error: "",
} }

View File

@ -1,98 +1,98 @@
package handlers package handlers
import ( import (
"log" "github.com/gorilla/sessions"
"net/http" "log"
"strconv" "net/http"
"threadr/models" "strconv"
"github.com/gorilla/sessions" "threadr/models"
) )
func NewsHandler(app *App) http.HandlerFunc { func NewsHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
loggedIn := session.Values["user_id"] != nil loggedIn := session.Values["user_id"] != nil
cookie, _ := r.Cookie("threadr_cookie_banner") cookie, _ := r.Cookie("threadr_cookie_banner")
userID, _ := session.Values["user_id"].(int) userID, _ := session.Values["user_id"].(int)
isAdmin := false isAdmin := false
if loggedIn { if loggedIn {
user, err := models.GetUserByID(app.DB, userID) user, err := models.GetUserByID(app.DB, userID)
if err != nil { if err != nil {
log.Printf("Error fetching user: %v", err) log.Printf("Error fetching user: %v", err)
} else if user != nil { } else if user != nil {
isAdmin = models.HasGlobalPermission(user, models.PermManageUsers) isAdmin = models.HasGlobalPermission(user, models.PermManageUsers)
} }
} }
if r.Method == http.MethodPost && loggedIn && isAdmin { if r.Method == http.MethodPost && loggedIn && isAdmin {
if action := r.URL.Query().Get("action"); action == "delete" { if action := r.URL.Query().Get("action"); action == "delete" {
newsIDStr := r.URL.Query().Get("id") newsIDStr := r.URL.Query().Get("id")
newsID, err := strconv.Atoi(newsIDStr) newsID, err := strconv.Atoi(newsIDStr)
if err != nil { if err != nil {
http.Error(w, "Invalid news ID", http.StatusBadRequest) http.Error(w, "Invalid news ID", http.StatusBadRequest)
return return
} }
err = models.DeleteNews(app.DB, newsID) err = models.DeleteNews(app.DB, newsID)
if err != nil { if err != nil {
log.Printf("Error deleting news item: %v", err) log.Printf("Error deleting news item: %v", err)
http.Error(w, "Failed to delete news item", http.StatusInternalServerError) http.Error(w, "Failed to delete news item", http.StatusInternalServerError)
return return
} }
http.Redirect(w, r, app.Config.ThreadrDir+"/news/", http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/news/", http.StatusFound)
return return
} else { } else {
title := r.FormValue("title") title := r.FormValue("title")
content := r.FormValue("content") content := r.FormValue("content")
if title != "" && content != "" { if title != "" && content != "" {
news := models.News{ news := models.News{
Title: title, Title: title,
Content: content, Content: content,
PostedBy: userID, PostedBy: userID,
} }
err := models.CreateNews(app.DB, news) err := models.CreateNews(app.DB, news)
if err != nil { if err != nil {
log.Printf("Error creating news item: %v", err) log.Printf("Error creating news item: %v", err)
http.Error(w, "Failed to create news item", http.StatusInternalServerError) http.Error(w, "Failed to create news item", http.StatusInternalServerError)
return return
} }
http.Redirect(w, r, app.Config.ThreadrDir+"/news/", http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/news/", http.StatusFound)
return return
} else { } else {
http.Error(w, "Title and content are required", http.StatusBadRequest) http.Error(w, "Title and content are required", http.StatusBadRequest)
return return
} }
} }
} }
newsItems, err := models.GetAllNews(app.DB) newsItems, err := models.GetAllNews(app.DB)
if err != nil { if err != nil {
log.Printf("Error fetching news items: %v", err) log.Printf("Error fetching news items: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
data := struct { data := struct {
PageData PageData
News []models.News News []models.News
IsAdmin bool IsAdmin bool
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - News", Title: "ThreadR - News",
Navbar: "news", Navbar: "news",
LoggedIn: loggedIn, LoggedIn: loggedIn,
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
News: newsItems, News: newsItems,
IsAdmin: isAdmin, IsAdmin: isAdmin,
} }
if err := app.Tmpl.ExecuteTemplate(w, "news", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "news", data); err != nil {
log.Printf("Error executing template in NewsHandler: %v", err) log.Printf("Error executing template in NewsHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -68,7 +68,7 @@ func PreferencesHandler(app *App) http.HandlerFunc {
ShowCookieBanner: false, ShowCookieBanner: false,
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
ContentTemplate: "preferences-content", ContentTemplate: "preferences-content",
}, },
Preferences: prefs, Preferences: prefs,

View File

@ -1,55 +1,55 @@
package handlers package handlers
import ( import (
"log" "github.com/gorilla/sessions"
"net/http" "log"
"threadr/models" "net/http"
"github.com/gorilla/sessions" "threadr/models"
) )
func ProfileHandler(app *App) http.HandlerFunc { func ProfileHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
userID, ok := session.Values["user_id"].(int) userID, ok := session.Values["user_id"].(int)
if !ok { if !ok {
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
return return
} }
user, err := models.GetUserByID(app.DB, userID) user, err := models.GetUserByID(app.DB, userID)
if err != nil { if err != nil {
log.Printf("Error fetching user in ProfileHandler: %v", err) log.Printf("Error fetching user in ProfileHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if user == nil { if user == nil {
http.Error(w, "User not found", http.StatusNotFound) http.Error(w, "User not found", http.StatusNotFound)
return return
} }
displayName := user.DisplayName displayName := user.DisplayName
if displayName == "" { if displayName == "" {
displayName = user.Username displayName = user.Username
} }
data := struct { data := struct {
PageData PageData
User models.User User models.User
DisplayName string DisplayName string
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - Profile", Title: "ThreadR - Profile",
Navbar: "profile", Navbar: "profile",
LoggedIn: true, LoggedIn: true,
ShowCookieBanner: false, ShowCookieBanner: false,
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
User: *user, User: *user,
DisplayName: displayName, DisplayName: displayName,
} }
if err := app.Tmpl.ExecuteTemplate(w, "profile", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "profile", data); err != nil {
log.Printf("Error executing template in ProfileHandler: %v", err) log.Printf("Error executing template in ProfileHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -39,9 +39,9 @@ func ProfileEditHandler(app *App) http.HandlerFunc {
// Create file record in the database // Create file record in the database
fileRecord := models.File{ fileRecord := models.File{
OriginalName: handler.Filename, OriginalName: handler.Filename,
Hash: fileHash, Hash: fileHash,
HashAlgorithm: "sha256", HashAlgorithm: "sha256",
} }
fileID, err := models.CreateFile(app.DB, fileRecord) fileID, err := models.CreateFile(app.DB, fileRecord)
if err != nil { if err != nil {
@ -95,36 +95,36 @@ func ProfileEditHandler(app *App) http.HandlerFunc {
return return
} }
user, err := models.GetUserByID(app.DB, userID) user, err := models.GetUserByID(app.DB, userID)
if err != nil { if err != nil {
log.Printf("Error fetching user: %v", err) log.Printf("Error fetching user: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if user == nil { if user == nil {
http.Error(w, "User not found", http.StatusNotFound) http.Error(w, "User not found", http.StatusNotFound)
return return
} }
data := struct { data := struct {
PageData PageData
User models.User User models.User
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - Edit Profile", Title: "ThreadR - Edit Profile",
Navbar: "profile", Navbar: "profile",
LoggedIn: true, LoggedIn: true,
ShowCookieBanner: false, ShowCookieBanner: false,
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
User: *user, User: *user,
} }
if err := app.Tmpl.ExecuteTemplate(w, "profile_edit", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "profile_edit", data); err != nil {
log.Printf("Error executing template in ProfileEditHandler: %v", err) log.Printf("Error executing template in ProfileEditHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -30,7 +30,7 @@ func SignupHandler(app *App) http.HandlerFunc {
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Error: "Passwords do not match. Please try again.", Error: "Passwords do not match. Please try again.",
} }
@ -56,7 +56,7 @@ func SignupHandler(app *App) http.HandlerFunc {
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Error: "An error occurred during sign up. Please try again.", Error: "An error occurred during sign up. Please try again.",
} }
@ -81,7 +81,7 @@ func SignupHandler(app *App) http.HandlerFunc {
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Error: "", Error: "",
} }

View File

@ -122,7 +122,7 @@ func ThreadHandler(app *App) http.HandlerFunc {
ShowCookieBanner: cookie == nil || cookie.Value != "accepted", ShowCookieBanner: cookie == nil || cookie.Value != "accepted",
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Thread: *thread, Thread: *thread,
Board: *board, Board: *board,

View File

@ -1,49 +1,49 @@
package handlers package handlers
import ( import (
"log" "github.com/gorilla/sessions"
"net/http" "log"
"threadr/models" "net/http"
"github.com/gorilla/sessions" "threadr/models"
) )
func UserHomeHandler(app *App) http.HandlerFunc { func UserHomeHandler(app *App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session) session := r.Context().Value("session").(*sessions.Session)
userID, ok := session.Values["user_id"].(int) userID, ok := session.Values["user_id"].(int)
if !ok { if !ok {
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound) http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
return return
} }
user, err := models.GetUserByID(app.DB, userID) user, err := models.GetUserByID(app.DB, userID)
if err != nil { if err != nil {
log.Printf("Error fetching user in UserHomeHandler: %v", err) log.Printf("Error fetching user in UserHomeHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
if user == nil { if user == nil {
http.Error(w, "User not found", http.StatusNotFound) http.Error(w, "User not found", http.StatusNotFound)
return return
} }
data := struct { data := struct {
PageData PageData
Username string Username string
}{ }{
PageData: PageData{ PageData: PageData{
Title: "ThreadR - User Home", Title: "ThreadR - User Home",
Navbar: "userhome", Navbar: "userhome",
LoggedIn: true, LoggedIn: true,
ShowCookieBanner: false, ShowCookieBanner: false,
BasePath: app.Config.ThreadrDir, BasePath: app.Config.ThreadrDir,
StaticPath: app.Config.ThreadrDir + "/static", StaticPath: app.Config.ThreadrDir + "/static",
CurrentURL: r.URL.Path, CurrentURL: r.URL.RequestURI(),
}, },
Username: user.Username, Username: user.Username,
} }
if err := app.Tmpl.ExecuteTemplate(w, "userhome", data); err != nil { if err := app.Tmpl.ExecuteTemplate(w, "userhome", data); err != nil {
log.Printf("Error executing template in UserHomeHandler: %v", err) log.Printf("Error executing template in UserHomeHandler: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
} }

View File

@ -551,6 +551,26 @@ p.thread-info {
position: relative; position: relative;
} }
.chat-message .post-actions {
position: absolute;
top: -12px;
right: 0;
margin: 0;
opacity: 0;
pointer-events: none;
background-color: #fef6e4;
border: 1px solid #001858;
border-radius: 4px;
padding: 2px 4px;
z-index: 10;
transition: opacity 0.15s ease;
}
.chat-message:hover .post-actions {
opacity: 1;
pointer-events: auto;
}
.chat-message-header { .chat-message-header {
display: flex; display: flex;
align-items: center; align-items: center;
@ -622,20 +642,16 @@ p.thread-info {
font-size: 0.9em; font-size: 0.9em;
} }
.chat-message:hover .post-actions { .chat-message .post-actions a {
opacity: 1;
}
.post-actions a {
color: #001858; color: #001858;
text-decoration: none; text-decoration: none;
font-size: 0.8em; font-size: 0.8em;
padding: 2px 5px; padding: 2px 5px;
border: 1px solid #001858; border: none;
border-radius: 3px; border-radius: 3px;
} }
.post-actions a:hover { .chat-message .post-actions a:hover {
background-color: #8bd3dd; background-color: #8bd3dd;
color: #fef6e4; color: #fef6e4;
} }
@ -851,12 +867,16 @@ p.thread-info {
background-color: #555; background-color: #555;
} }
.post-actions a { .chat-message .post-actions {
color: #fef6e4; background-color: #444;
border-color: #fef6e4; border-color: #fef6e4;
} }
.post-actions a:hover { .chat-message .post-actions a {
color: #fef6e4;
}
.chat-message .post-actions a:hover {
background-color: #8bd3dd; background-color: #8bd3dd;
color: #001858; color: #001858;
} }