use more structured json for page updates

This commit is contained in:
Ted Unangst 2022-01-27 21:58:37 -05:00
parent e9a742a132
commit 577d686ba5
3 changed files with 36 additions and 21 deletions

View File

@ -1,11 +1,7 @@
<div>{{ .TopHID }}</div>
{{ $BonkCSRF := .HonkCSRF }} {{ $BonkCSRF := .HonkCSRF }}
{{ $MapLink := .MapLink }} {{ $MapLink := .MapLink }}
{{ $Badonk := .User.Options.Reaction }} {{ $Badonk := .User.Options.Reaction }}
{{ $OmitImages := .User.Options.OmitImages }} {{ $OmitImages := .User.Options.OmitImages }}
<div><p>{{ .ServerMessage }}</div>
<div>
{{ range .Honks }} {{ range .Honks }}
{{ template "honk.html" map "Honk" . "MapLink" $MapLink "BonkCSRF" $BonkCSRF "Badonk" $Badonk "OmitImages" $OmitImages }} {{ template "honk.html" map "Honk" . "MapLink" $MapLink "BonkCSRF" $BonkCSRF "Badonk" $Badonk "OmitImages" $OmitImages }}
{{ end }} {{ end }}
</div>

View File

@ -15,7 +15,7 @@ function post(url, data) {
function get(url, whendone) { function get(url, whendone) {
var x = new XMLHttpRequest() var x = new XMLHttpRequest()
x.open("GET", url) x.open("GET", url)
x.responseType = "document" x.responseType = "json"
x.onload = function() { whendone(x) } x.onload = function() { whendone(x) }
x.send() x.send()
} }
@ -82,11 +82,15 @@ function removeglow() {
} }
function fillinhonks(xhr, glowit) { function fillinhonks(xhr, glowit) {
var doc = xhr.responseXML var resp = xhr.response
var stash = curpagestate.name + ":" + curpagestate.arg var stash = curpagestate.name + ":" + curpagestate.arg
tophid[stash] = doc.children[0].children[1].children[0].innerText tophid[stash] = resp.Tophid
var srvmsg = doc.children[0].children[1].children[1] var doc = document.createElement( 'div' );
var honks = doc.children[0].children[1].children[2].children doc.innerHTML = resp.Srvmsg
var srvmsg = doc
doc = document.createElement( 'div' );
doc.innerHTML = resp.Honks
var honks = doc.children
var srvel = document.getElementById("srvmsg") var srvel = document.getElementById("srvmsg")
while (srvel.children[0]) { while (srvel.children[0]) {

39
web.go
View File

@ -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) { func webhydra(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
userid := u.UserID userid := u.UserID
@ -2203,38 +2209,40 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
wanted, _ := strconv.ParseInt(r.FormValue("tophid"), 10, 0) wanted, _ := strconv.ParseInt(r.FormValue("tophid"), 10, 0)
var hydra Hydration
var honks []*Honk var honks []*Honk
switch page { switch page {
case "atme": case "atme":
honks = gethonksforme(userid, wanted) honks = gethonksforme(userid, wanted)
honks = osmosis(honks, userid, false) honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "at me!" hydra.Srvmsg = "at me!"
case "longago": case "longago":
honks = gethonksfromlongago(userid, wanted) honks = gethonksfromlongago(userid, wanted)
honks = osmosis(honks, userid, false) honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "from long ago" hydra.Srvmsg = "from long ago"
case "home": case "home":
honks = gethonksforuser(userid, wanted) honks = gethonksforuser(userid, wanted)
honks = osmosis(honks, userid, true) honks = osmosis(honks, userid, true)
templinfo["ServerMessage"] = serverMsg hydra.Srvmsg = serverMsg
case "first": case "first":
honks = gethonksforuserfirstclass(userid, wanted) honks = gethonksforuserfirstclass(userid, wanted)
honks = osmosis(honks, userid, true) honks = osmosis(honks, userid, true)
templinfo["ServerMessage"] = "first class only" hydra.Srvmsg = "first class only"
case "saved": case "saved":
honks = getsavedhonks(userid, wanted) honks = getsavedhonks(userid, wanted)
templinfo["PageName"] = "saved" templinfo["PageName"] = "saved"
templinfo["ServerMessage"] = "saved honks" hydra.Srvmsg = "saved honks"
case "combo": case "combo":
c := r.FormValue("c") c := r.FormValue("c")
honks = gethonksbycombo(userid, c, wanted) honks = gethonksbycombo(userid, c, wanted)
honks = osmosis(honks, userid, false) honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "honks by combo: " + c hydra.Srvmsg = templates.Sprintf("honks by combo: %s", c)
case "convoy": case "convoy":
c := r.FormValue("c") c := r.FormValue("c")
honks = gethonksbyconvoy(userid, c, wanted) honks = gethonksbyconvoy(userid, c, wanted)
honks = osmosis(honks, userid, false) honks = osmosis(honks, userid, false)
templinfo["ServerMessage"] = "honks in convoy: " + c hydra.Srvmsg = templates.Sprintf("honks in convoy: %s", c)
case "honker": case "honker":
xid := r.FormValue("xid") xid := r.FormValue("xid")
honks = gethonksbyxonker(userid, xid, wanted) honks = gethonksbyxonker(userid, xid, wanted)
@ -2244,24 +2252,31 @@ func webhydra(w http.ResponseWriter, r *http.Request) {
<button tabindex=1 name="add honker" value="add honker">add honker</button> <button tabindex=1 name="add honker" value="add honker">add honker</button>
</form>`, login.GetCSRF("submithonker", r), xid) </form>`, login.GetCSRF("submithonker", r), xid)
msg := templates.Sprintf(`honks by honker: <a href="%s" ref="noreferrer">%s</a>%s`, xid, xid, miniform) msg := templates.Sprintf(`honks by honker: <a href="%s" ref="noreferrer">%s</a>%s`, xid, xid, miniform)
templinfo["ServerMessage"] = msg hydra.Srvmsg = msg
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }
if len(honks) > 0 { if len(honks) > 0 {
templinfo["TopHID"] = honks[0].ID hydra.Tophid = honks[0].ID
} else { } else {
templinfo["TopHID"] = wanted hydra.Tophid = wanted
} }
reverbolate(userid, honks) reverbolate(userid, honks)
var buf strings.Builder
templinfo["Honks"] = honks templinfo["Honks"] = honks
templinfo["MapLink"] = getmaplink(u) templinfo["MapLink"] = getmaplink(u)
templinfo["User"], _ = butwhatabout(u.Username) templinfo["User"], _ = butwhatabout(u.Username)
w.Header().Set("Content-Type", "text/html; charset=utf-8") err := readviews.Execute(&buf, "honkfrags.html", templinfo)
err := readviews.Execute(w, "honkfrags.html", templinfo)
if err != nil { if err != nil {
log.Printf("frag error: %s", err) 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) var honkline = make(chan bool)