Messages now sending/loading and populating on message dropdown
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user