Messages now sending/loading and populating on message dropdown
This commit is contained in:
@@ -18,7 +18,3 @@ func mustUserID(c *gin.Context) int64 {
|
||||
// Fallback for stubs:
|
||||
return 1
|
||||
}
|
||||
|
||||
type strconvNumErr struct{}
|
||||
|
||||
func (e *strconvNumErr) Error() string { return "invalid number" }
|
||||
|
||||
@@ -18,14 +18,9 @@ import (
|
||||
// using ONLY session data (no DB) so 404/500 pages don't crash and still
|
||||
// look "logged in" when a session exists.
|
||||
func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
|
||||
// Synthesize minimal TemplateData from session only
|
||||
var data models.TemplateData
|
||||
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// Read minimal user snapshot from session
|
||||
var uid int64
|
||||
if v := sessions.Get(ctx, sessionkeys.UserID); v != nil {
|
||||
r := c.Request
|
||||
uid := int64(0)
|
||||
if v := sessions.Get(r.Context(), sessionkeys.UserID); v != nil {
|
||||
switch t := v.(type) {
|
||||
case int64:
|
||||
uid = t
|
||||
@@ -33,22 +28,22 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
|
||||
uid = int64(t)
|
||||
}
|
||||
}
|
||||
|
||||
// --- build minimal template data from session
|
||||
var data models.TemplateData
|
||||
if uid > 0 {
|
||||
// username and is_admin are optional but make navbar correct
|
||||
var uname string
|
||||
if v := sessions.Get(ctx, sessionkeys.Username); v != nil {
|
||||
uname := ""
|
||||
if v := sessions.Get(r.Context(), sessionkeys.Username); v != nil {
|
||||
if s, ok := v.(string); ok {
|
||||
uname = s
|
||||
}
|
||||
}
|
||||
var isAdmin bool
|
||||
if v := sessions.Get(ctx, sessionkeys.IsAdmin); v != nil {
|
||||
isAdmin := false
|
||||
if v := sessions.Get(r.Context(), sessionkeys.IsAdmin); v != nil {
|
||||
if b, ok := v.(bool); ok {
|
||||
isAdmin = b
|
||||
}
|
||||
}
|
||||
|
||||
// Build a lightweight user; avoids DB lookups in error paths
|
||||
data.User = &models.User{
|
||||
Id: uid,
|
||||
Username: uname,
|
||||
@@ -57,15 +52,11 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
|
||||
data.IsAdmin = isAdmin
|
||||
}
|
||||
|
||||
// Turn into the template context map (adds site meta, funcs, etc.)
|
||||
ctxMap := templateHelpers.TemplateContext(c.Writer, c.Request, data)
|
||||
|
||||
// Flash (SCS)
|
||||
if f := sessions.PopString(ctx, sessionkeys.Flash); f != "" {
|
||||
ctxMap := templateHelpers.TemplateContext(c.Writer, r, data)
|
||||
if f := sessions.PopString(r.Context(), sessionkeys.Flash); f != "" {
|
||||
ctxMap["Flash"] = f
|
||||
}
|
||||
|
||||
// Template paths (layout-first)
|
||||
pagePath := fmt.Sprintf("web/templates/error/%d.html", status)
|
||||
if _, err := os.Stat(pagePath); err != nil {
|
||||
c.String(status, http.StatusText(status))
|
||||
@@ -86,11 +77,13 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
|
||||
func NoRoute(sessions *scs.SessionManager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) { RenderStatus(c, sessions, http.StatusNotFound) }
|
||||
}
|
||||
|
||||
func NoMethod(sessions *scs.SessionManager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) { RenderStatus(c, sessions, http.StatusMethodNotAllowed) }
|
||||
}
|
||||
|
||||
func Recovery(sessions *scs.SessionManager) gin.RecoveryFunc {
|
||||
return func(c *gin.Context, _ interface{}) {
|
||||
return func(c *gin.Context, rec interface{}) {
|
||||
RenderStatus(c, sessions, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
26
internal/http/middleware/errorlog.go
Normal file
26
internal/http/middleware/errorlog.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// internal/http/middleware/errorlog.go
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"synlotto-website/internal/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ErrorLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
if len(c.Errors) == 0 {
|
||||
return
|
||||
}
|
||||
for _, e := range c.Errors {
|
||||
logging.Info("❌ %s %s -> %d in %v: %v",
|
||||
c.Request.Method, c.FullPath(), c.Writer.Status(),
|
||||
time.Since(start), e.Err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,8 @@ func RegisterAccountRoutes(app *bootstrap.App) {
|
||||
messages.GET("/send", msgH.SendGet)
|
||||
messages.POST("/send", msgH.SendPost)
|
||||
messages.GET("/archived", msgH.ArchivedList) // renders archived.html
|
||||
messages.GET("/:id", msgH.ReadGet) // renders read.html
|
||||
messages.GET("/read", msgH.ReadGet)
|
||||
|
||||
}
|
||||
|
||||
// Notifications (auth-required)
|
||||
|
||||
@@ -151,7 +151,7 @@ func Load(configPath string) (*App, error) {
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 10 * time.Second, // ToDo: consider moving to config
|
||||
ReadHeaderTimeout: cfg.HttpServer.ReadHeaderTimeout,
|
||||
}
|
||||
|
||||
app.Handler = handler
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
// Config represents all runtime configuration for the application.
|
||||
// Loaded from JSON and passed into bootstrap for wiring platform components.
|
||||
type Config struct {
|
||||
@@ -52,9 +54,10 @@ type Config struct {
|
||||
|
||||
// HTTP server exposure and security toggles
|
||||
HttpServer struct {
|
||||
Port int `json:"port"`
|
||||
Address string `json:"address"`
|
||||
ProductionMode bool `json:"productionMode"` // controls Secure cookie flag
|
||||
Port int `json:"port"`
|
||||
Address string `json:"address"`
|
||||
ProductionMode bool `json:"productionMode"` // controls Secure cookie flag
|
||||
ReadHeaderTimeout time.Duration `json:"readHeaderTimeout"` // config in nanoseconds
|
||||
} `json:"httpServer"`
|
||||
|
||||
// Remote licensing API service configuration
|
||||
|
||||
@@ -8,9 +8,14 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"synlotto-website/internal/logging"
|
||||
|
||||
domain "synlotto-website/internal/domain/messages"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// Service implements domain.Service.
|
||||
@@ -43,8 +48,8 @@ func (s *Service) ListInbox(userID int64) ([]domain.Message, error) {
|
||||
defer cancel()
|
||||
|
||||
q := `
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = FALSE
|
||||
ORDER BY created_at DESC`
|
||||
q = s.bind(q)
|
||||
@@ -72,8 +77,8 @@ func (s *Service) ListArchived(userID int64) ([]domain.Message, error) {
|
||||
defer cancel()
|
||||
|
||||
q := `
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = TRUE
|
||||
ORDER BY created_at DESC`
|
||||
q = s.bind(q)
|
||||
@@ -100,8 +105,8 @@ func (s *Service) GetByID(userID, id int64) (*domain.Message, error) {
|
||||
defer cancel()
|
||||
|
||||
q := `
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND id = ?`
|
||||
q = s.bind(q)
|
||||
|
||||
@@ -117,34 +122,66 @@ func (s *Service) GetByID(userID, id int64) (*domain.Message, error) {
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func (s *Service) Create(userID int64, in domain.CreateMessageInput) (int64, error) {
|
||||
func (s *Service) Create(senderID int64, in domain.CreateMessageInput) (int64, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
|
||||
defer cancel()
|
||||
|
||||
switch s.Dialect {
|
||||
case "postgres":
|
||||
const q = `
|
||||
INSERT INTO messages (id, senderId, recipientId, subject, message, is_read, is_archived, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, FALSE, FALSE, NOW())
|
||||
RETURNING id`
|
||||
var id int64
|
||||
if err := s.DB.QueryRowContext(ctx, q, userID, "", in.RecipientID, in.Subject, in.Body).Scan(&id); err != nil {
|
||||
return 0, err
|
||||
// ✅ make sure this matches your current table/column names
|
||||
const q = `
|
||||
INSERT INTO user_messages
|
||||
(senderId, recipientId, subject, body, is_read, is_archived, created_at)
|
||||
VALUES
|
||||
(?, ?, ?, ?, 0, 0, CURRENT_TIMESTAMP)
|
||||
`
|
||||
|
||||
// 👀 Log the SQL and arguments (truncate body in logs if you prefer)
|
||||
logging.Info("🧪 SQL Exec: %s | args: senderId=%d recipientId=%d subject=%q body_len=%d", compactSQL(q), senderID, in.RecipientID, in.Subject, len(in.Body))
|
||||
|
||||
res, err := s.DB.ExecContext(ctx, q, senderID, in.RecipientID, in.Subject, in.Body)
|
||||
if err != nil {
|
||||
// Surface MySQL code/message (very helpful for FK #1452 etc.)
|
||||
var me *mysql.MySQLError
|
||||
if errors.As(err, &me) {
|
||||
wrapped := fmt.Errorf("insert user_messages: mysql #%d %s | args(senderId=%d, recipientId=%d, subject=%q, body_len=%d)",
|
||||
me.Number, me.Message, senderID, in.RecipientID, in.Subject, len(in.Body))
|
||||
logging.Info("❌ %v", wrapped)
|
||||
return 0, wrapped
|
||||
}
|
||||
return id, nil
|
||||
default: // mysql/sqlite
|
||||
const q = `
|
||||
INSERT INTO messages (id, senderId, recipientId, subject, message is_read, is_archived, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, FALSE, FALSE, CURRENT_TIMESTAMP)`
|
||||
res, err := s.DB.ExecContext(ctx, q, userID, "", in.RecipientID, in.Subject, in.Body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
wrapped := fmt.Errorf("insert user_messages: %w | args(senderId=%d, recipientId=%d, subject=%q, body_len=%d)",
|
||||
err, senderID, in.RecipientID, in.Subject, len(in.Body))
|
||||
logging.Info("❌ %v", wrapped)
|
||||
return 0, wrapped
|
||||
}
|
||||
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
wrapped := fmt.Errorf("lastInsertId user_messages: %w", err)
|
||||
logging.Info("❌ %v", wrapped)
|
||||
return 0, wrapped
|
||||
}
|
||||
|
||||
logging.Info("✅ Inserted message id=%d", id)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// compactSQL removes newlines/extra spaces for cleaner logs
|
||||
func compactSQL(s string) string {
|
||||
out := make([]rune, 0, len(s))
|
||||
space := false
|
||||
for _, r := range s {
|
||||
if r == '\n' || r == '\t' || r == '\r' || r == ' ' {
|
||||
if !space {
|
||||
out = append(out, ' ')
|
||||
space = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
space = false
|
||||
out = append(out, r)
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// bind replaces '?' with '$1..' only for Postgres. For MySQL/SQLite it returns q unchanged.
|
||||
func (s *Service) bind(q string) string {
|
||||
if s.Dialect != "postgres" {
|
||||
return q
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func SendMessage(db *sql.DB, senderID, recipientID int, subject, message string) error {
|
||||
func SendMessage(db *sql.DB, senderID, recipientID int, subject, body string) error {
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO users_messages (senderId, recipientId, subject, message)
|
||||
INSERT INTO user_messages (senderId, recipientId, subject, body)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`, senderID, recipientID, subject, message)
|
||||
`, senderID, recipientID, subject, body)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func GetMessageCount(db *sql.DB, userID int) (int, error) {
|
||||
var count int
|
||||
err := db.QueryRow(`
|
||||
SELECT COUNT(*) FROM users_messages
|
||||
SELECT COUNT(*) FROM user_messages
|
||||
WHERE recipientId = ? AND is_read = FALSE AND is_archived = FALSE
|
||||
`, userID).Scan(&count)
|
||||
return count, err
|
||||
@@ -17,8 +17,8 @@ func GetMessageCount(db *sql.DB, userID int) (int, error) {
|
||||
|
||||
func GetRecentMessages(db *sql.DB, userID int, limit int) []models.Message {
|
||||
rows, err := db.Query(`
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, created_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = FALSE
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
@@ -49,8 +49,8 @@ func GetRecentMessages(db *sql.DB, userID int, limit int) []models.Message {
|
||||
|
||||
func GetMessageByID(db *sql.DB, userID, messageID int) (*models.Message, error) {
|
||||
row := db.QueryRow(`
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, created_at
|
||||
FROM user_messages
|
||||
WHERE id = ? AND recipientId = ?
|
||||
`, messageID, userID)
|
||||
|
||||
@@ -65,8 +65,8 @@ func GetMessageByID(db *sql.DB, userID, messageID int) (*models.Message, error)
|
||||
func GetArchivedMessages(db *sql.DB, userID int, page, perPage int) []models.Message {
|
||||
offset := (page - 1) * perPage
|
||||
rows, err := db.Query(`
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, created_at, archived_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, created_at, archived_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = TRUE
|
||||
ORDER BY archived_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
@@ -94,8 +94,8 @@ func GetArchivedMessages(db *sql.DB, userID int, page, perPage int) []models.Mes
|
||||
func GetInboxMessages(db *sql.DB, userID int, page, perPage int) []models.Message {
|
||||
offset := (page - 1) * perPage
|
||||
rows, err := db.Query(`
|
||||
SELECT id, senderId, recipientId, subject, message, is_read, created_at
|
||||
FROM users_messages
|
||||
SELECT id, senderId, recipientId, subject, body, is_read, created_at
|
||||
FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = FALSE
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
@@ -122,7 +122,7 @@ func GetInboxMessages(db *sql.DB, userID int, page, perPage int) []models.Messag
|
||||
func GetInboxMessageCount(db *sql.DB, userID int) int {
|
||||
var count int
|
||||
err := db.QueryRow(`
|
||||
SELECT COUNT(*) FROM users_messages
|
||||
SELECT COUNT(*) FROM user_messages
|
||||
WHERE recipientId = ? AND is_archived = FALSE
|
||||
`, userID).Scan(&count)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func ArchiveMessage(db *sql.DB, userID, messageID int) error {
|
||||
_, err := db.Exec(`
|
||||
UPDATE users_messages
|
||||
UPDATE user_messages
|
||||
SET is_archived = TRUE, archived_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND recipientId = ?
|
||||
`, messageID, userID)
|
||||
@@ -16,7 +16,7 @@ func ArchiveMessage(db *sql.DB, userID, messageID int) error {
|
||||
|
||||
func MarkMessageAsRead(db *sql.DB, messageID, userID int) error {
|
||||
result, err := db.Exec(`
|
||||
UPDATE users_messages
|
||||
UPDATE user_messages
|
||||
SET is_read = TRUE
|
||||
WHERE id = ? AND recipientId = ?
|
||||
`, messageID, userID)
|
||||
@@ -36,7 +36,7 @@ func MarkMessageAsRead(db *sql.DB, messageID, userID int) error {
|
||||
|
||||
func RestoreMessage(db *sql.DB, userID, messageID int) error {
|
||||
_, err := db.Exec(`
|
||||
UPDATE users_messages
|
||||
UPDATE user_messages
|
||||
SET is_archived = FALSE, archived_at = NULL
|
||||
WHERE id = ? AND recipientId = ?
|
||||
`, messageID, userID)
|
||||
|
||||
@@ -140,20 +140,20 @@ CREATE TABLE IF NOT EXISTS my_tickets (
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- USERS MESSAGES
|
||||
CREATE TABLE IF NOT EXISTS users_messages (
|
||||
CREATE TABLE IF NOT EXISTS user_messages (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
senderId BIGINT UNSIGNED NOT NULL,
|
||||
recipientId BIGINT UNSIGNED NOT NULL,
|
||||
subject VARCHAR(255) NOT NULL,
|
||||
message MEDIUMTEXT,
|
||||
body MEDIUMTEXT,
|
||||
is_read TINYINT(1) NOT NULL DEFAULT 0,
|
||||
is_archived TINYINT(1) NOT NULL DEFAULT 0,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
archived_at DATETIME NULL,
|
||||
CONSTRAINT fk_users_messages_sender
|
||||
CONSTRAINT fk_user_messages_sender
|
||||
FOREIGN KEY (senderId) REFERENCES users(id)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
CONSTRAINT fk_users_messages_recipient
|
||||
CONSTRAINT fk_user_messages_recipient
|
||||
FOREIGN KEY (recipientId) REFERENCES users(id)
|
||||
ON UPDATE CASCADE ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
Reference in New Issue
Block a user