From 577d686ba51d19aa82e7b25a69ac3a7a899c1b6b Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Thu, 27 Jan 2022 21:58:37 -0500 Subject: [PATCH] use more structured json for page updates --- views/honkfrags.html | 4 ---- views/honkpage.js | 14 +++++++++----- web.go | 39 +++++++++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/views/honkfrags.html b/views/honkfrags.html index 5ccc2da..0ac6f75 100644 --- a/views/honkfrags.html +++ b/views/honkfrags.html @@ -1,11 +1,7 @@ -
{{ .TopHID }}
{{ $BonkCSRF := .HonkCSRF }} {{ $MapLink := .MapLink }} {{ $Badonk := .User.Options.Reaction }} {{ $OmitImages := .User.Options.OmitImages }} -

{{ .ServerMessage }}

-
{{ range .Honks }} {{ template "honk.html" map "Honk" . "MapLink" $MapLink "BonkCSRF" $BonkCSRF "Badonk" $Badonk "OmitImages" $OmitImages }} {{ end }} -
diff --git a/views/honkpage.js b/views/honkpage.js index 3ec166b..2d215ab 100644 --- a/views/honkpage.js +++ b/views/honkpage.js @@ -15,7 +15,7 @@ function post(url, data) { function get(url, whendone) { var x = new XMLHttpRequest() x.open("GET", url) - x.responseType = "document" + x.responseType = "json" x.onload = function() { whendone(x) } x.send() } @@ -82,11 +82,15 @@ function removeglow() { } function fillinhonks(xhr, glowit) { - var doc = xhr.responseXML + var resp = xhr.response var stash = curpagestate.name + ":" + curpagestate.arg - tophid[stash] = doc.children[0].children[1].children[0].innerText - var srvmsg = doc.children[0].children[1].children[1] - var honks = doc.children[0].children[1].children[2].children + tophid[stash] = resp.Tophid + var doc = document.createElement( 'div' ); + doc.innerHTML = resp.Srvmsg + var srvmsg = doc + doc = document.createElement( 'div' ); + doc.innerHTML = resp.Honks + var honks = doc.children var srvel = document.getElementById("srvmsg") while (srvel.children[0]) { diff --git a/web.go b/web.go index f583b4f..28007e7 100644 --- a/web.go +++ b/web.go @@ -2194,6 +2194,12 @@ func nomoroboto(w http.ResponseWriter, r *http.Request) { } } +type Hydration struct { + Tophid int64 + Srvmsg template.HTML + Honks string +} + func webhydra(w http.ResponseWriter, r *http.Request) { u := login.GetUserInfo(r) userid := u.UserID @@ -2203,38 +2209,40 @@ func webhydra(w http.ResponseWriter, r *http.Request) { wanted, _ := strconv.ParseInt(r.FormValue("tophid"), 10, 0) + var hydra Hydration + var honks []*Honk switch page { case "atme": honks = gethonksforme(userid, wanted) honks = osmosis(honks, userid, false) - templinfo["ServerMessage"] = "at me!" + hydra.Srvmsg = "at me!" case "longago": honks = gethonksfromlongago(userid, wanted) honks = osmosis(honks, userid, false) - templinfo["ServerMessage"] = "from long ago" + hydra.Srvmsg = "from long ago" case "home": honks = gethonksforuser(userid, wanted) honks = osmosis(honks, userid, true) - templinfo["ServerMessage"] = serverMsg + hydra.Srvmsg = serverMsg case "first": honks = gethonksforuserfirstclass(userid, wanted) honks = osmosis(honks, userid, true) - templinfo["ServerMessage"] = "first class only" + hydra.Srvmsg = "first class only" case "saved": honks = getsavedhonks(userid, wanted) templinfo["PageName"] = "saved" - templinfo["ServerMessage"] = "saved honks" + hydra.Srvmsg = "saved honks" case "combo": c := r.FormValue("c") honks = gethonksbycombo(userid, c, wanted) honks = osmosis(honks, userid, false) - templinfo["ServerMessage"] = "honks by combo: " + c + hydra.Srvmsg = templates.Sprintf("honks by combo: %s", c) case "convoy": c := r.FormValue("c") honks = gethonksbyconvoy(userid, c, wanted) honks = osmosis(honks, userid, false) - templinfo["ServerMessage"] = "honks in convoy: " + c + hydra.Srvmsg = templates.Sprintf("honks in convoy: %s", c) case "honker": xid := r.FormValue("xid") honks = gethonksbyxonker(userid, xid, wanted) @@ -2244,24 +2252,31 @@ func webhydra(w http.ResponseWriter, r *http.Request) { `, login.GetCSRF("submithonker", r), xid) msg := templates.Sprintf(`honks by honker: %s%s`, xid, xid, miniform) - templinfo["ServerMessage"] = msg + hydra.Srvmsg = msg default: http.NotFound(w, r) } + if len(honks) > 0 { - templinfo["TopHID"] = honks[0].ID + hydra.Tophid = honks[0].ID } else { - templinfo["TopHID"] = wanted + hydra.Tophid = wanted } reverbolate(userid, honks) + + var buf strings.Builder templinfo["Honks"] = honks templinfo["MapLink"] = getmaplink(u) templinfo["User"], _ = butwhatabout(u.Username) - w.Header().Set("Content-Type", "text/html; charset=utf-8") - err := readviews.Execute(w, "honkfrags.html", templinfo) + err := readviews.Execute(&buf, "honkfrags.html", templinfo) if err != nil { log.Printf("frag error: %s", err) + return } + hydra.Honks = buf.String() + w.Header().Set("Content-Type", "application/json") + j, _ := jsonify(&hydra) + io.WriteString(w, j) } var honkline = make(chan bool)