From b8df8a9b939520b28f002b51d99a2c9ddb82b549 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 16 Sep 2019 18:00:13 -0400 Subject: [PATCH] beta/prototype of Update --- activity.go | 26 ++++++++++++- database.go | 35 ++++++++++++++++-- docs/changelog.txt | 2 + schema.sql | 1 + upgradedb.go | 4 ++ util.go | 2 +- views/honk.html | 1 + views/honkform.html | 1 + web.go | 90 +++++++++++++++++++++++++++++++++------------ 9 files changed, 133 insertions(+), 29 deletions(-) diff --git a/activity.go b/activity.go index df459be..19cbb17 100644 --- a/activity.go +++ b/activity.go @@ -464,6 +464,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk { var xid, rid, url, content, precis, convoy, oonker string var obj junk.Junk var ok bool + isUpdate := false switch what { case "Announce": obj, ok = item.GetMap("object") @@ -482,6 +483,10 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk { } origin = originate(xid) what = "bonk" + case "Update": + log.Printf("should be updating") + isUpdate = true + fallthrough case "Create": obj, ok = item.GetMap("object") if !ok { @@ -695,6 +700,19 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk { } } + if isUpdate { + prev := getxonk(user.ID, xonk.XID) + if prev == nil { + log.Printf("didn't find old version for update: %s", xonk.XID) + return nil + } + prev.Noise = xonk.Noise + prev.Precis = xonk.Precis + prev.Date = xonk.Date + updatehonk(prev) + return nil + } + if needxonk(user, &xonk) { if rid != "" { if needxonkid(user, rid) { @@ -789,10 +807,16 @@ func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) { } switch h.What { + case "update": + fallthrough case "tonk": fallthrough case "honk": - j["type"] = "Create" + if h.What == "update" { + j["type"] = "Update" + } else { + j["type"] = "Create" + } jo = junk.New() jo["id"] = h.XID diff --git a/database.go b/database.go index 0a1290a..5952d13 100644 --- a/database.go +++ b/database.go @@ -239,15 +239,20 @@ func savehonk(h *Honk) error { return err } h.ID, _ = res.LastInsertId() + err = saveextras(h) + return err +} + +func saveextras(h *Honk) error { for _, d := range h.Donks { - _, err = stmtSaveDonk.Exec(h.ID, d.FileID) + _, 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 = stmtSaveOnt.Exec(strings.ToLower(o), h.ID) + _, err := stmtSaveOnt.Exec(strings.ToLower(o), h.ID) if err != nil { log.Printf("error saving ont: %s", err) return err @@ -256,7 +261,7 @@ func savehonk(h *Honk) error { return nil } -func deletehonk(honkid int64) { +func deleteextras(honkid int64) { _, err := stmtDeleteDonks.Exec(honkid) if err != nil { log.Printf("error deleting: %s", err) @@ -265,12 +270,31 @@ func deletehonk(honkid int64) { if err != nil { log.Printf("error deleting: %s", err) } - _, err = stmtDeleteHonk.Exec(honkid) +} + +func deletehonk(honkid int64) { + deleteextras(honkid) + _, err := stmtDeleteHonk.Exec(honkid) if err != nil { log.Printf("error deleting: %s", err) } } +func updatehonk(h *Honk) { + old := getxonk(h.UserID, h.XID) + _, err := stmtSaveOld.Exec(old.ID, old.Precis, old.Noise) + if err != nil { + log.Printf("error saving old: %s", err) + return + } + deleteextras(h.ID) + + dt := h.Date.UTC().Format(dbtimeformat) + stmtUpdateHonk.Exec(h.Precis, h.Noise, dt, h.ID) + + saveextras(h) +} + func cleanupdb(arg string) { db := opendatabase() days, err := strconv.Atoi(arg) @@ -299,6 +323,7 @@ var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover, stmtOneHonker var stmtThumbBiters, stmtDeleteHonk, stmtDeleteDonks, stmtDeleteOnts, stmtSaveZonker *sql.Stmt var stmtGetZonkers, stmtRecentHonkers, stmtGetXonker, stmtSaveXonker, stmtDeleteXonker *sql.Stmt var stmtSelectOnts, stmtSaveOnt, stmtUpdateFlags, stmtClearFlags *sql.Stmt +var stmtSaveOld, stmtUpdateHonk *sql.Stmt func preparetodie(db *sql.DB, s string) *sql.Stmt { stmt, err := db.Prepare(s) @@ -332,8 +357,10 @@ func prepareStatements(db *sql.DB) { stmtHonksByConvoy = preparetodie(db, selecthonks+"where (honks.userid = ? or (? = -1 and whofore = 2)) and convoy = ?"+limit) stmtHonksByOntology = preparetodie(db, selecthonks+"join onts on honks.honkid = onts.honkid where onts.ontology = ? and (honks.userid = ? or (? = -1 and honks.whofore = 2))"+limit) + stmtSaveOld = preparetodie(db, "insert into forsaken (honkid, precis, noise) values (?, ?, ?)") stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags, onts) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtDeleteHonk = preparetodie(db, "delete from honks where honkid = ?") + stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, dt = ? where honkid = ?") stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)") stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") diff --git a/docs/changelog.txt b/docs/changelog.txt index 111b571..992c000 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog -- next ++++ Editing honks (Update activity). + + Quick mention @alias. + Image descriptions. diff --git a/schema.sql b/schema.sql index c444c9b..f59ec6f 100644 --- a/schema.sql +++ b/schema.sql @@ -7,6 +7,7 @@ create table xonkers (xonkerid integer primary key, name text, info text, flavor create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text); create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob); create table onts (ontology text, honkid integer); +create table forsaken (honkid integer, precis text, noise text); create index idx_honksxid on honks(xid); create index idx_honksconvoy on honks(convoy); diff --git a/upgradedb.go b/upgradedb.go index 0f4e9d2..a6b7a10 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -189,6 +189,10 @@ func upgradedb() { doordie(db, "update config set value = 17 where key = 'dbversion'") fallthrough case 17: + doordie(db, "create table forsaken (xid text, noise text)") + doordie(db, "update config set value = 18 where key = 'dbversion'") + fallthrough + case 18: default: log.Fatalf("can't upgrade unknown version %d", dbversion) } diff --git a/util.go b/util.go index e4c0fec..e185b37 100644 --- a/util.go +++ b/util.go @@ -72,7 +72,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt -var myVersion = 17 +var myVersion = 18 func initdb() { schema, err := ioutil.ReadFile("schema.sql") diff --git a/views/honk.html b/views/honk.html index 96d4201..9763c39 100644 --- a/views/honk.html +++ b/views/honk.html @@ -87,6 +87,7 @@ in reply to: {{ .RID }} {{ else }} {{ end }} +

diff --git a/views/honkform.html b/views/honkform.html index cbe6172..41df3b3 100644 --- a/views/honkform.html +++ b/views/honkform.html @@ -4,6 +4,7 @@

in reply to: +

diff --git a/web.go b/web.go index ad8d9ea..35bfb8c 100644 --- a/web.go +++ b/web.go @@ -348,6 +348,9 @@ func inbox(w http.ResponseWriter, r *http.Request) { return case "Question": return + case "Note": + go consumeactivity(user, j, origin) + return } } log.Printf("unknown Update activity") @@ -822,6 +825,30 @@ func zonkit(w http.ResponseWriter, r *http.Request) { } } +func edithonkpage(w http.ResponseWriter, r *http.Request) { + u := login.GetUserInfo(r) + user, _ := butwhatabout(u.Username) + xid := r.FormValue("xid") + honk := getxonk(u.UserID, xid) + if honk == nil || honk.Honker != user.URL || honk.What != "honk" { + log.Printf("no edit") + return + } + + honks := []*Honk{honk} + reverbolate(u.UserID, honks) + templinfo := getInfo(r) + templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r) + templinfo["Honks"] = honks + templinfo["Noise"] = honk.Noise + templinfo["ServerMessage"] = "honk edit" + templinfo["UpdateXID"] = honk.XID + err := readviews.Execute(w, "honkpage.html", templinfo) + if err != nil { + log.Print(err) + } +} + func submithonk(w http.ResponseWriter, r *http.Request) { rid := r.FormValue("rid") noise := r.FormValue("noise") @@ -830,18 +857,30 @@ func submithonk(w http.ResponseWriter, r *http.Request) { user, _ := butwhatabout(userinfo.Username) dt := time.Now().UTC() - xid := fmt.Sprintf("%s/%s/%s", user.URL, honkSep, xfiltrate()) - what := "honk" - if rid != "" { - what = "tonk" - } - honk := Honk{ - UserID: userinfo.UserID, - Username: userinfo.Username, - What: "honk", - Honker: user.URL, - XID: xid, - Date: dt, + updatexid := r.FormValue("updatexid") + var honk *Honk + if updatexid != "" { + honk = getxonk(userinfo.UserID, updatexid) + if honk == nil || honk.Honker != user.URL || honk.What != "honk" { + log.Printf("not saving edit") + return + } + honk.Date = dt + honk.What = "update" + } else { + xid := fmt.Sprintf("%s/%s/%s", user.URL, honkSep, xfiltrate()) + what := "honk" + if rid != "" { + what = "tonk" + } + honk = &Honk{ + UserID: userinfo.UserID, + Username: userinfo.Username, + What: what, + Honker: user.URL, + XID: xid, + Date: dt, + } } if strings.HasPrefix(noise, "DZ:") { idx := strings.Index(noise, "\n") @@ -977,7 +1016,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) { honk.Donks = append(honk.Donks, donk) } } - memetize(&honk) + memetize(honk) if honk.Public { honk.Whofore = 2 @@ -985,7 +1024,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) { honk.Whofore = 3 } if r.FormValue("preview") == "preview" { - honks := []*Honk{&honk} + honks := []*Honk{honk} reverbolate(userinfo.UserID, honks) templinfo := getInfo(r) templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r) @@ -1002,26 +1041,30 @@ func submithonk(w http.ResponseWriter, r *http.Request) { } honk.Onts = oneofakind(ontologies(honk.Noise)) 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("uh oh") - return + if updatexid != "" { + updatehonk(honk) + + } else { + + err := savehonk(honk) + if err != nil { + log.Printf("uh oh") + return + } } // reload for consistency honk.Donks = nil - donksforhonks([]*Honk{&honk}) + donksforhonks([]*Honk{honk}) - go honkworldwide(user, &honk) + go honkworldwide(user, honk) - http.Redirect(w, r, xid, http.StatusSeeOther) + http.Redirect(w, r, honk.XID, http.StatusSeeOther) } func showhonkers(w http.ResponseWriter, r *http.Request) { @@ -1451,6 +1494,7 @@ func serve() { loggedin.HandleFunc("/atme", homepage) loggedin.HandleFunc("/zonkzone", zonkzone) loggedin.HandleFunc("/xzone", xzone) + loggedin.HandleFunc("/edit", edithonkpage) 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)))