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
}
func savexonk(user *WhatAbout, x *Honk) {
func savexonk(x *Honk) {
if x.What == "eradicate" {
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 {
_, err := stmtZonkDonks.Exec(xonk.ID)
if err != nil {
@ -243,42 +243,16 @@ func savexonk(user *WhatAbout, x *Honk) {
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 {
log.Printf("error eradicating: %s", err)
}
return
}
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.Oonker)
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
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)
}
}
savehonk(x)
}
type Box struct {
@ -377,7 +351,7 @@ func gimmexonks(user *WhatAbout, outbox string) {
}
xonk := xonkxonk(user, obj, origin)
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) {
xonk := xonkxonk(user, j, origin)
if xonk != nil {
savexonk(user, xonk)
savexonk(xonk)
}
}
@ -482,7 +456,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
depth++
xonk := xonkxonkfn(obj, originate(xid))
if xonk != nil {
savexonk(user, xonk)
savexonk(xonk)
}
depth--
}
@ -710,6 +684,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
if oonker == who {
oonker = ""
}
// init xonk
xonk.UserID = user.ID
xonk.What = what
xonk.Honker = who
@ -721,6 +696,12 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
xonk.Precis = precis
xonk.Audience = audience
xonk.Oonker = oonker
xonk.Format = "html"
for _, a := range audience {
if a == user.URL {
xonk.Whofore = 1
}
}
if needxonk(user, &xonk) {
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) {
db := opendatabase()
days, err := strconv.Atoi(arg)

View File

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

View File

@ -2,7 +2,7 @@
<main>
<div class="info">
<p>
<form action="/savehonker" method="POST">
<form action="/submithonker" method="POST">
<span class="title">add new honker</span>
<input type="hidden" name="CSRF" value="{{ .HonkerCSRF }}">
<p><input tabindex=1 type="text" name="url" value="" autocomplete=off> - url
@ -34,7 +34,7 @@ function expandstuff() {
<details>
<p>url: <a href="{{ .XID }}" rel=noreferrer>{{ .XID }}</a>
<p>flavor: {{ .Flavor }}
<form action="/savehonker" method="POST">
<form action="/submithonker" method="POST">
<input type="hidden" name="CSRF" value="{{ $honkercsrf }}">
<input type="hidden" name="honkerid" value="{{ .ID }}">
<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 := ""
if xonk != nil {
convoy = xonk.Convoy
savexonk(user, xonk)
savexonk(xonk)
}
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)
}
func savebonk(w http.ResponseWriter, r *http.Request) {
func submitbonk(w http.ResponseWriter, r *http.Request) {
xid := r.FormValue("xid")
userinfo := login.GetUserInfo(r)
user, _ := butwhatabout(userinfo.Username)
@ -734,39 +734,27 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
Username: userinfo.Username,
What: "bonk",
Honker: user.URL,
Oonker: oonker,
XID: xonk.XID,
RID: xonk.RID,
Noise: xonk.Noise,
Precis: xonk.Precis,
URL: xonk.URL,
Date: dt,
Donks: xonk.Donks,
Whofore: 2,
Convoy: xonk.Convoy,
Audience: []string{thewholeworld, oonker},
Public: true,
}
aud := strings.Join(bonk.Audience, " ")
whofore := 2
onts := xonk.Onts
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, " "))
bonk.Format = "html"
err = savehonk(&bonk)
if err != nil {
log.Printf("error saving bonk: %s", err)
log.Printf("uh oh")
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)
}
@ -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")
noise := r.FormValue("noise")
@ -1016,10 +1004,10 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
}
memetize(&honk)
aud := strings.Join(honk.Audience, " ")
whofore := 2
if !honk.Public {
whofore = 3
if honk.Public {
honk.Whofore = 2
} else {
honk.Whofore = 3
}
if r.FormValue("preview") == "preview" {
honks := []*Honk{&honk}
@ -1038,29 +1026,21 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
return
}
honk.Onts = oneofakind(ontologies(honk.Noise))
res, err := stmtSaveHonk.Exec(userinfo.UserID, what, honk.Honker, xid, rid,
dt.Format(dbtimeformat), "", aud, honk.Noise, convoy, whofore, "html",
honk.Precis, honk.Oonker, 0, strings.Join(honk.Onts, " "))
honk.UserID = userinfo.UserID
honk.What = what
honk.XID = xid
honk.RID = rid
honk.Date = dt
honk.Convoy = convoy
honk.Format = "html"
err := savehonk(&honk)
if err != nil {
log.Printf("error saving honk: %s", err)
http.Error(w, "something bad happened while saving", http.StatusInternalServerError)
log.Printf("uh oh")
return
}
honk.ID, _ = res.LastInsertId()
for _, d := range honk.Donks {
_, 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)
}
}
// reload for consistency
honk.Donks = nil
donksforhonks([]*Honk{&honk})
@ -1073,7 +1053,7 @@ func showhonkers(w http.ResponseWriter, r *http.Request) {
userinfo := login.GetUserInfo(r)
templinfo := getInfo(r)
templinfo["Honkers"] = gethonkers(userinfo.UserID)
templinfo["HonkerCSRF"] = login.GetCSRF("savehonker", r)
templinfo["HonkerCSRF"] = login.GetCSRF("submithonker", r)
err := readviews.Execute(w, "honkers.html", templinfo)
if err != nil {
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)
name := r.FormValue("name")
url := r.FormValue("url")
@ -1455,8 +1435,8 @@ func serve() {
loggedin.HandleFunc("/atme", homepage)
loggedin.HandleFunc("/zonkzone", zonkzone)
loggedin.HandleFunc("/xzone", xzone)
loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(savehonk)))
loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(savebonk)))
loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submithonk)))
loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk)))
loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit)))
loggedin.Handle("/zonkzonk", login.CSRFWrap("zonkzonk", http.HandlerFunc(zonkzonk)))
loggedin.Handle("/saveuser", login.CSRFWrap("saveuser", http.HandlerFunc(saveuser)))
@ -1468,7 +1448,7 @@ func serve() {
loggedin.HandleFunc("/c", showcombos)
loggedin.HandleFunc("/t", showconvoy)
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)
if err != nil {