cleanup the honk save code

This commit is contained in:
Ted Unangst 2019-09-11 16:20:22 -04:00
parent d57a633f0f
commit 56141dbb6f
5 changed files with 79 additions and 88 deletions

View File

@ -229,10 +229,10 @@ func needxonkid(user *WhatAbout, xid string) bool {
return true return true
} }
func savexonk(user *WhatAbout, x *Honk) { func savexonk(x *Honk) {
if x.What == "eradicate" { if x.What == "eradicate" {
log.Printf("eradicating %s by %s", x.XID, x.Honker) log.Printf("eradicating %s by %s", x.XID, x.Honker)
xonk := getxonk(user.ID, x.XID) xonk := getxonk(x.UserID, x.XID)
if xonk != nil { if xonk != nil {
_, err := stmtZonkDonks.Exec(xonk.ID) _, err := stmtZonkDonks.Exec(xonk.ID)
if err != nil { if err != nil {
@ -243,42 +243,16 @@ func savexonk(user *WhatAbout, x *Honk) {
log.Printf("error eradicating: %s", err) log.Printf("error eradicating: %s", err)
} }
} }
_, err := stmtSaveZonker.Exec(user.ID, x.XID, "zonk") _, err := stmtSaveZonker.Exec(x.UserID, x.XID, "zonk")
if err != nil { if err != nil {
log.Printf("error eradicating: %s", err) log.Printf("error eradicating: %s", err)
} }
return return
} }
log.Printf("saving xonk: %s", x.XID) log.Printf("saving xonk: %s", x.XID)
dt := x.Date.UTC().Format(dbtimeformat)
aud := strings.Join(x.Audience, " ")
whofore := 0
if strings.Contains(aud, user.URL) {
whofore = 1
}
go prehandle(x.Honker) go prehandle(x.Honker)
go prehandle(x.Oonker) go prehandle(x.Oonker)
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, savehonk(x)
x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker, 0, strings.Join(x.Onts, " "))
if err != nil {
log.Printf("err saving xonk: %s", err)
return
}
x.ID, _ = res.LastInsertId()
for _, d := range x.Donks {
_, err = stmtSaveDonk.Exec(x.ID, d.FileID)
if err != nil {
log.Printf("err saving donk: %s", err)
return
}
}
for _, o := range x.Onts {
_, err = stmtSaveOnts.Exec(strings.ToLower(o), x.ID)
if err != nil {
log.Printf("error saving ont: %s", err)
}
}
} }
type Box struct { type Box struct {
@ -377,7 +351,7 @@ func gimmexonks(user *WhatAbout, outbox string) {
} }
xonk := xonkxonk(user, obj, origin) xonk := xonkxonk(user, obj, origin)
if xonk != nil { if xonk != nil {
savexonk(user, xonk) savexonk(xonk)
} }
} }
} }
@ -458,7 +432,7 @@ func extractattrto(obj junk.Junk) string {
func consumeactivity(user *WhatAbout, j junk.Junk, origin string) { func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
xonk := xonkxonk(user, j, origin) xonk := xonkxonk(user, j, origin)
if xonk != nil { if xonk != nil {
savexonk(user, xonk) savexonk(xonk)
} }
} }
@ -482,7 +456,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
depth++ depth++
xonk := xonkxonkfn(obj, originate(xid)) xonk := xonkxonkfn(obj, originate(xid))
if xonk != nil { if xonk != nil {
savexonk(user, xonk) savexonk(xonk)
} }
depth-- depth--
} }
@ -710,6 +684,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
if oonker == who { if oonker == who {
oonker = "" oonker = ""
} }
// init xonk
xonk.UserID = user.ID xonk.UserID = user.ID
xonk.What = what xonk.What = what
xonk.Honker = who xonk.Honker = who
@ -721,6 +696,12 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
xonk.Precis = precis xonk.Precis = precis
xonk.Audience = audience xonk.Audience = audience
xonk.Oonker = oonker xonk.Oonker = oonker
xonk.Format = "html"
for _, a := range audience {
if a == user.URL {
xonk.Whofore = 1
}
}
if needxonk(user, &xonk) { if needxonk(user, &xonk) {
if rid != "" { if rid != "" {

View File

@ -246,6 +246,35 @@ func donksforhonks(honks []*Honk) {
} }
} }
func savehonk(h *Honk) error {
dt := h.Date.UTC().Format(dbtimeformat)
aud := strings.Join(h.Audience, " ")
res, err := stmtSaveHonk.Exec(h.UserID, h.What, h.Honker, h.XID, h.RID, dt, h.URL,
aud, h.Noise, h.Convoy, h.Whofore, h.Format, h.Precis,
h.Oonker, h.Flags, strings.Join(h.Onts, " "))
if err != nil {
log.Printf("err saving honk: %s", err)
return err
}
h.ID, _ = res.LastInsertId()
for _, d := range h.Donks {
_, err = stmtSaveDonk.Exec(h.ID, d.FileID)
if err != nil {
log.Printf("err saving donk: %s", err)
return err
}
}
for _, o := range h.Onts {
_, err = stmtSaveOnts.Exec(strings.ToLower(o), h.ID)
if err != nil {
log.Printf("error saving ont: %s", err)
return err
}
}
return nil
}
func cleanupdb(arg string) { func cleanupdb(arg string) {
db := opendatabase() db := opendatabase()
days, err := strconv.Atoi(arg) days, err := strconv.Atoi(arg)

View File

@ -48,6 +48,7 @@ type Honk struct {
URL string URL string
Noise string Noise string
Precis string Precis string
Format string
Convoy string Convoy string
Audience []string Audience []string
Public bool Public bool

View File

@ -2,7 +2,7 @@
<main> <main>
<div class="info"> <div class="info">
<p> <p>
<form action="/savehonker" method="POST"> <form action="/submithonker" method="POST">
<span class="title">add new honker</span> <span class="title">add new honker</span>
<input type="hidden" name="CSRF" value="{{ .HonkerCSRF }}"> <input type="hidden" name="CSRF" value="{{ .HonkerCSRF }}">
<p><input tabindex=1 type="text" name="url" value="" autocomplete=off> - url <p><input tabindex=1 type="text" name="url" value="" autocomplete=off> - url
@ -34,7 +34,7 @@ function expandstuff() {
<details> <details>
<p>url: <a href="{{ .XID }}" rel=noreferrer>{{ .XID }}</a> <p>url: <a href="{{ .XID }}" rel=noreferrer>{{ .XID }}</a>
<p>flavor: {{ .Flavor }} <p>flavor: {{ .Flavor }}
<form action="/savehonker" method="POST"> <form action="/submithonker" method="POST">
<input type="hidden" name="CSRF" value="{{ $honkercsrf }}"> <input type="hidden" name="CSRF" value="{{ $honkercsrf }}">
<input type="hidden" name="honkerid" value="{{ .ID }}"> <input type="hidden" name="honkerid" value="{{ .ID }}">
<p>combos: <input type="text" name="combos" value="{{ range .Combos }}{{ . }} {{end}}"> <p>combos: <input type="text" name="combos" value="{{ range .Combos }}{{ . }} {{end}}">

86
web.go
View File

@ -421,7 +421,7 @@ func ximport(w http.ResponseWriter, r *http.Request) {
convoy := "" convoy := ""
if xonk != nil { if xonk != nil {
convoy = xonk.Convoy convoy = xonk.Convoy
savexonk(user, xonk) savexonk(xonk)
} }
http.Redirect(w, r, "/t?c="+url.QueryEscape(convoy), http.StatusSeeOther) http.Redirect(w, r, "/t?c="+url.QueryEscape(convoy), http.StatusSeeOther)
} }
@ -703,7 +703,7 @@ func saveuser(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/account", http.StatusSeeOther) http.Redirect(w, r, "/account", http.StatusSeeOther)
} }
func savebonk(w http.ResponseWriter, r *http.Request) { func submitbonk(w http.ResponseWriter, r *http.Request) {
xid := r.FormValue("xid") xid := r.FormValue("xid")
userinfo := login.GetUserInfo(r) userinfo := login.GetUserInfo(r)
user, _ := butwhatabout(userinfo.Username) user, _ := butwhatabout(userinfo.Username)
@ -734,39 +734,27 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
Username: userinfo.Username, Username: userinfo.Username,
What: "bonk", What: "bonk",
Honker: user.URL, Honker: user.URL,
Oonker: oonker,
XID: xonk.XID, XID: xonk.XID,
RID: xonk.RID, RID: xonk.RID,
Noise: xonk.Noise,
Precis: xonk.Precis,
URL: xonk.URL,
Date: dt, Date: dt,
Donks: xonk.Donks, Donks: xonk.Donks,
Whofore: 2,
Convoy: xonk.Convoy, Convoy: xonk.Convoy,
Audience: []string{thewholeworld, oonker}, Audience: []string{thewholeworld, oonker},
Public: true, Public: true,
} }
aud := strings.Join(bonk.Audience, " ") bonk.Format = "html"
whofore := 2
onts := xonk.Onts err = savehonk(&bonk)
res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", bonk.Honker, xid, bonk.RID,
dt.Format(dbtimeformat), "", aud, xonk.Noise, xonk.Convoy, whofore, "html",
xonk.Precis, oonker, 0, strings.Join(onts, " "))
if err != nil { if err != nil {
log.Printf("error saving bonk: %s", err) log.Printf("uh oh")
return return
} }
bonk.ID, _ = res.LastInsertId()
for _, d := range bonk.Donks {
_, err = stmtSaveDonk.Exec(bonk.ID, d.FileID)
if err != nil {
log.Printf("err saving donk: %s", err)
return
}
}
for _, o := range onts {
_, err = stmtSaveOnts.Exec(strings.ToLower(o), bonk.ID)
if err != nil {
log.Printf("error saving ont: %s", err)
}
}
go honkworldwide(user, &bonk) go honkworldwide(user, &bonk)
} }
@ -859,7 +847,7 @@ func zonkit(w http.ResponseWriter, r *http.Request) {
} }
} }
func savehonk(w http.ResponseWriter, r *http.Request) { func submithonk(w http.ResponseWriter, r *http.Request) {
rid := r.FormValue("rid") rid := r.FormValue("rid")
noise := r.FormValue("noise") noise := r.FormValue("noise")
@ -1016,10 +1004,10 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
} }
memetize(&honk) memetize(&honk)
aud := strings.Join(honk.Audience, " ") if honk.Public {
whofore := 2 honk.Whofore = 2
if !honk.Public { } else {
whofore = 3 honk.Whofore = 3
} }
if r.FormValue("preview") == "preview" { if r.FormValue("preview") == "preview" {
honks := []*Honk{&honk} honks := []*Honk{&honk}
@ -1038,29 +1026,21 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
return return
} }
honk.Onts = oneofakind(ontologies(honk.Noise)) honk.Onts = oneofakind(ontologies(honk.Noise))
res, err := stmtSaveHonk.Exec(userinfo.UserID, what, honk.Honker, xid, rid, honk.UserID = userinfo.UserID
dt.Format(dbtimeformat), "", aud, honk.Noise, convoy, whofore, "html", honk.What = what
honk.Precis, honk.Oonker, 0, strings.Join(honk.Onts, " ")) honk.XID = xid
honk.RID = rid
honk.Date = dt
honk.Convoy = convoy
honk.Format = "html"
err := savehonk(&honk)
if err != nil { if err != nil {
log.Printf("error saving honk: %s", err) log.Printf("uh oh")
http.Error(w, "something bad happened while saving", http.StatusInternalServerError)
return return
} }
honk.ID, _ = res.LastInsertId()
for _, d := range honk.Donks { // reload for consistency
_, err = stmtSaveDonk.Exec(honk.ID, d.FileID)
if err != nil {
log.Printf("err saving donk: %s", err)
http.Error(w, "something bad happened while saving", http.StatusInternalServerError)
return
}
}
for _, o := range honk.Onts {
_, err = stmtSaveOnts.Exec(strings.ToLower(o), honk.ID)
if err != nil {
log.Printf("error saving ont: %s", err)
}
}
honk.Donks = nil honk.Donks = nil
donksforhonks([]*Honk{&honk}) donksforhonks([]*Honk{&honk})
@ -1073,7 +1053,7 @@ func showhonkers(w http.ResponseWriter, r *http.Request) {
userinfo := login.GetUserInfo(r) userinfo := login.GetUserInfo(r)
templinfo := getInfo(r) templinfo := getInfo(r)
templinfo["Honkers"] = gethonkers(userinfo.UserID) templinfo["Honkers"] = gethonkers(userinfo.UserID)
templinfo["HonkerCSRF"] = login.GetCSRF("savehonker", r) templinfo["HonkerCSRF"] = login.GetCSRF("submithonker", r)
err := readviews.Execute(w, "honkers.html", templinfo) err := readviews.Execute(w, "honkers.html", templinfo)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -1102,7 +1082,7 @@ func showcombos(w http.ResponseWriter, r *http.Request) {
} }
} }
func savehonker(w http.ResponseWriter, r *http.Request) { func submithonker(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
name := r.FormValue("name") name := r.FormValue("name")
url := r.FormValue("url") url := r.FormValue("url")
@ -1455,8 +1435,8 @@ func serve() {
loggedin.HandleFunc("/atme", homepage) loggedin.HandleFunc("/atme", homepage)
loggedin.HandleFunc("/zonkzone", zonkzone) loggedin.HandleFunc("/zonkzone", zonkzone)
loggedin.HandleFunc("/xzone", xzone) loggedin.HandleFunc("/xzone", xzone)
loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(savehonk))) loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submithonk)))
loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(savebonk))) loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk)))
loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit))) loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit)))
loggedin.Handle("/zonkzonk", login.CSRFWrap("zonkzonk", http.HandlerFunc(zonkzonk))) loggedin.Handle("/zonkzonk", login.CSRFWrap("zonkzonk", http.HandlerFunc(zonkzonk)))
loggedin.Handle("/saveuser", login.CSRFWrap("saveuser", http.HandlerFunc(saveuser))) loggedin.Handle("/saveuser", login.CSRFWrap("saveuser", http.HandlerFunc(saveuser)))
@ -1468,7 +1448,7 @@ func serve() {
loggedin.HandleFunc("/c", showcombos) loggedin.HandleFunc("/c", showcombos)
loggedin.HandleFunc("/t", showconvoy) loggedin.HandleFunc("/t", showconvoy)
loggedin.HandleFunc("/q", showsearch) loggedin.HandleFunc("/q", showsearch)
loggedin.Handle("/savehonker", login.CSRFWrap("savehonker", http.HandlerFunc(savehonker))) loggedin.Handle("/submithonker", login.CSRFWrap("submithonker", http.HandlerFunc(submithonker)))
err = http.Serve(listener, mux) err = http.Serve(listener, mux)
if err != nil { if err != nil {