use more structured json for page updates
Ted Unangst tedu@tedunangst.com
Thu, 27 Jan 2022 21:58:37 -0500
3 files changed,
36 insertions(+),
21 deletions(-)
M
views/honkfrags.html
→
views/honkfrags.html
@@ -1,11 +1,7 @@
-<div>{{ .TopHID }}</div> {{ $BonkCSRF := .HonkCSRF }} {{ $MapLink := .MapLink }} {{ $Badonk := .User.Options.Reaction }} {{ $OmitImages := .User.Options.OmitImages }} -<div><p>{{ .ServerMessage }}</div> -<div> {{ range .Honks }} {{ template "honk.html" map "Honk" . "MapLink" $MapLink "BonkCSRF" $BonkCSRF "Badonk" $Badonk "OmitImages" $OmitImages }} {{ end }} -</div>
M
views/honkpage.js
→
views/honkpage.js
@@ -15,7 +15,7 @@ }
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 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]) {
M
web.go
→
web.go
@@ -2194,6 +2194,12 @@ fmt.Fprintf(w, "Disallow: /%s/%s/%s/\n", userSep, u.Username, honkSep)
} } +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 @@ page := r.FormValue("page")
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 @@ <input type="hidden" name="url" value="%s">
<button tabindex=1 name="add honker" value="add honker">add honker</button> </form>`, login.GetCSRF("submithonker", r), xid) msg := templates.Sprintf(`honks by honker: <a href="%s" ref="noreferrer">%s</a>%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)