From b0a0c3614161f3b6c64a9ba5f034ec3a25b2b782 Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 28 Apr 2024 15:41:47 -0700 Subject: [PATCH] Make sidebar poll for notifications every 10s --- internal/webserver/handler_login.go | 4 +--- internal/webserver/handler_sidebar.go | 18 ++++++++++++++++++ internal/webserver/server.go | 2 ++ .../webserver/tpl/includes/nav_sidebar.tpl | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 internal/webserver/handler_sidebar.go diff --git a/internal/webserver/handler_login.go b/internal/webserver/handler_login.go index 0f1f9cb..074d3e2 100644 --- a/internal/webserver/handler_login.go +++ b/internal/webserver/handler_login.go @@ -115,8 +115,6 @@ func (app *Application) ChangeSession(w http.ResponseWriter, r *http.Request) { app.error_400_with_message(w, fmt.Sprintf("User not in database: %s", form.AccountName)) return } - data := Notifications{ - NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID)), - } + data := Notifications{NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID))} app.buffered_render_htmx(w, "nav-sidebar", PageGlobalData{}, data) } diff --git a/internal/webserver/handler_sidebar.go b/internal/webserver/handler_sidebar.go new file mode 100644 index 0000000..913b16d --- /dev/null +++ b/internal/webserver/handler_sidebar.go @@ -0,0 +1,18 @@ +package webserver + +import ( + "net/http" +) + +func (app *Application) NavSidebarPollUpdates(w http.ResponseWriter, r *http.Request) { + app.traceLog.Printf("'NavSidebarPollUpdates' handler (path: %q)", r.URL.Path) + + // Must be an HTMX request, otherwise HTTP 400 + if r.Header.Get("HX-Request") != "true" { + app.error_400_with_message(w, "This is an HTMX-only endpoint, not a page") + return + } + + data := Notifications{NumMessageNotifications: len(app.Profile.GetUnreadConversations(app.ActiveUser.ID))} + app.buffered_render_htmx(w, "nav-sidebar", PageGlobalData{}, data) +} diff --git a/internal/webserver/server.go b/internal/webserver/server.go index f8ec95a..a857039 100644 --- a/internal/webserver/server.go +++ b/internal/webserver/server.go @@ -124,6 +124,8 @@ func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.StripPrefix("/lists", http.HandlerFunc(app.Lists)).ServeHTTP(w, r) case "messages": http.StripPrefix("/messages", http.HandlerFunc(app.Messages)).ServeHTTP(w, r) + case "nav-sidebar-poll-updates": + app.NavSidebarPollUpdates(w, r) default: app.UserFeed(w, r) } diff --git a/internal/webserver/tpl/includes/nav_sidebar.tpl b/internal/webserver/tpl/includes/nav_sidebar.tpl index 6ac258e..641297a 100644 --- a/internal/webserver/tpl/includes/nav_sidebar.tpl +++ b/internal/webserver/tpl/includes/nav_sidebar.tpl @@ -1,5 +1,5 @@ {{define "nav-sidebar"}} -