diff --git a/internal/webserver/handler_messages.go b/internal/webserver/handler_messages.go index 4211b76..5aa0995 100644 --- a/internal/webserver/handler_messages.go +++ b/internal/webserver/handler_messages.go @@ -39,16 +39,22 @@ func (app *Application) message_detail(w http.ResponseWriter, r *http.Request) { panic_if(err) var message_data struct { Text string `json:"text"` + LatestPollingTimestamp int `json:"latest_polling_timestamp,string"` } panic_if(json.Unmarshal(body, &message_data)) trove := scraper.SendDMMessage(room_id, message_data.Text, 0) app.Profile.SaveDMTrove(trove, false) app.buffered_render_htmx(w, "dm-composer", global_data, chat_view_data) // Wipe the chat box go app.Profile.SaveDMTrove(trove, true) + chat_view_data.LatestPollingTimestamp = message_data.LatestPollingTimestamp // We're going to return some messages + } else { + chat_view_data.LatestPollingTimestamp = -1 // TODO: why not 0? If `0` then it won't generate a SQL `where` clause } + // TODO: Why are the input (parsed out of the HTTP request) and output (computed from the messsages + // fetched and printed into the HTTP response) using the same variable in `chat_view_data`? + chat_view_data.ActiveRoomID = room_id - chat_view_data.LatestPollingTimestamp = -1 // TODO: why not 0? If `0` then it won't generate a SQL `where` clause if latest_timestamp_str := r.URL.Query().Get("latest_timestamp"); latest_timestamp_str != "" { var err error chat_view_data.LatestPollingTimestamp, err = strconv.Atoi(latest_timestamp_str) @@ -78,6 +84,9 @@ func (app *Application) message_detail(w http.ResponseWriter, r *http.Request) { // Polling for updates and sending a message should add messages at the bottom of the page (newest) if r.URL.Query().Has("poll") || is_sending { app.buffered_render_htmx(w, "messages-with-poller", global_data, chat_view_data) + + // OOB-swap the composer polling timestamp field since it may have updated + app.buffered_render_htmx(w, "composer-polling-timestamp-field", global_data, map[string]interface{}{"LatestPollingTimestamp": chat_view_data.LatestPollingTimestamp, "oob": true}) return } diff --git a/internal/webserver/tpl/tweet_page_includes/chat_view.tpl b/internal/webserver/tpl/tweet_page_includes/chat_view.tpl index 42af58a..74a0a1a 100644 --- a/internal/webserver/tpl/tweet_page_includes/chat_view.tpl +++ b/internal/webserver/tpl/tweet_page_includes/chat_view.tpl @@ -93,6 +93,18 @@ > {{end}} +{{define "composer-polling-timestamp-field"}} + +{{end}} + {{define "chat-view"}}