beta/prototype of Update

This commit is contained in:
Ted Unangst 2019-09-16 18:00:13 -04:00
parent 4d95d2290a
commit b8df8a9b93
9 changed files with 133 additions and 29 deletions

View File

@ -464,6 +464,7 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
var xid, rid, url, content, precis, convoy, oonker string var xid, rid, url, content, precis, convoy, oonker string
var obj junk.Junk var obj junk.Junk
var ok bool var ok bool
isUpdate := false
switch what { switch what {
case "Announce": case "Announce":
obj, ok = item.GetMap("object") obj, ok = item.GetMap("object")
@ -482,6 +483,10 @@ func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
} }
origin = originate(xid) origin = originate(xid)
what = "bonk" what = "bonk"
case "Update":
log.Printf("should be updating")
isUpdate = true
fallthrough
case "Create": case "Create":
obj, ok = item.GetMap("object") obj, ok = item.GetMap("object")
if !ok { 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 needxonk(user, &xonk) {
if rid != "" { if rid != "" {
if needxonkid(user, rid) { if needxonkid(user, rid) {
@ -789,10 +807,16 @@ func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
} }
switch h.What { switch h.What {
case "update":
fallthrough
case "tonk": case "tonk":
fallthrough fallthrough
case "honk": case "honk":
j["type"] = "Create" if h.What == "update" {
j["type"] = "Update"
} else {
j["type"] = "Create"
}
jo = junk.New() jo = junk.New()
jo["id"] = h.XID jo["id"] = h.XID

View File

@ -239,15 +239,20 @@ func savehonk(h *Honk) error {
return err return err
} }
h.ID, _ = res.LastInsertId() h.ID, _ = res.LastInsertId()
err = saveextras(h)
return err
}
func saveextras(h *Honk) error {
for _, d := range h.Donks { for _, d := range h.Donks {
_, err = stmtSaveDonk.Exec(h.ID, d.FileID) _, err := stmtSaveDonk.Exec(h.ID, d.FileID)
if err != nil { if err != nil {
log.Printf("err saving donk: %s", err) log.Printf("err saving donk: %s", err)
return err return err
} }
} }
for _, o := range h.Onts { for _, o := range h.Onts {
_, err = stmtSaveOnt.Exec(strings.ToLower(o), h.ID) _, err := stmtSaveOnt.Exec(strings.ToLower(o), h.ID)
if err != nil { if err != nil {
log.Printf("error saving ont: %s", err) log.Printf("error saving ont: %s", err)
return err return err
@ -256,7 +261,7 @@ func savehonk(h *Honk) error {
return nil return nil
} }
func deletehonk(honkid int64) { func deleteextras(honkid int64) {
_, err := stmtDeleteDonks.Exec(honkid) _, err := stmtDeleteDonks.Exec(honkid)
if err != nil { if err != nil {
log.Printf("error deleting: %s", err) log.Printf("error deleting: %s", err)
@ -265,12 +270,31 @@ func deletehonk(honkid int64) {
if err != nil { if err != nil {
log.Printf("error deleting: %s", err) log.Printf("error deleting: %s", err)
} }
_, err = stmtDeleteHonk.Exec(honkid) }
func deletehonk(honkid int64) {
deleteextras(honkid)
_, err := stmtDeleteHonk.Exec(honkid)
if err != nil { if err != nil {
log.Printf("error deleting: %s", err) 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) { func cleanupdb(arg string) {
db := opendatabase() db := opendatabase()
days, err := strconv.Atoi(arg) days, err := strconv.Atoi(arg)
@ -299,6 +323,7 @@ var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover, stmtOneHonker
var stmtThumbBiters, stmtDeleteHonk, stmtDeleteDonks, stmtDeleteOnts, stmtSaveZonker *sql.Stmt var stmtThumbBiters, stmtDeleteHonk, stmtDeleteDonks, stmtDeleteOnts, stmtSaveZonker *sql.Stmt
var stmtGetZonkers, stmtRecentHonkers, stmtGetXonker, stmtSaveXonker, stmtDeleteXonker *sql.Stmt var stmtGetZonkers, stmtRecentHonkers, stmtGetXonker, stmtSaveXonker, stmtDeleteXonker *sql.Stmt
var stmtSelectOnts, stmtSaveOnt, stmtUpdateFlags, stmtClearFlags *sql.Stmt var stmtSelectOnts, stmtSaveOnt, stmtUpdateFlags, stmtClearFlags *sql.Stmt
var stmtSaveOld, stmtUpdateHonk *sql.Stmt
func preparetodie(db *sql.DB, s string) *sql.Stmt { func preparetodie(db *sql.DB, s string) *sql.Stmt {
stmt, err := db.Prepare(s) 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) 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) 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") 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 = ?") 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 (?, ?)") stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)")
stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?") stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)")

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+++ Editing honks (Update activity).
+ Quick mention @alias. + Quick mention @alias.
+ Image descriptions. + Image descriptions.

View File

@ -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 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 doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob);
create table onts (ontology text, honkid integer); 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_honksxid on honks(xid);
create index idx_honksconvoy on honks(convoy); create index idx_honksconvoy on honks(convoy);

View File

@ -189,6 +189,10 @@ func upgradedb() {
doordie(db, "update config set value = 17 where key = 'dbversion'") doordie(db, "update config set value = 17 where key = 'dbversion'")
fallthrough fallthrough
case 17: 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: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }

View File

@ -72,7 +72,7 @@ var dbtimeformat = "2006-01-02 15:04:05"
var alreadyopendb *sql.DB var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 17 var myVersion = 18
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")

View File

@ -87,6 +87,7 @@ in reply to: <a href="{{ .RID }}" rel=noreferrer>{{ .RID }}</a>
{{ else }} {{ else }}
<button onclick="ackit(this, '{{ .Honk.XID }}');">ack</button> <button onclick="ackit(this, '{{ .Honk.XID }}');">ack</button>
{{ end }} {{ end }}
<button><a href="/edit?xid={{ .Honk.XID }}">edit</a></button>
</div> </div>
</details> </details>
<p> <p>

View File

@ -4,6 +4,7 @@
<p> <p>
in reply to: <input type="text" name="rid" id="ridinput" value="{{ .InReplyTo }}" autocomplete=off> in reply to: <input type="text" name="rid" id="ridinput" value="{{ .InReplyTo }}" autocomplete=off>
<input type="hidden" name="CSRF" value="{{ .HonkCSRF }}"> <input type="hidden" name="CSRF" value="{{ .HonkCSRF }}">
<input type="hidden" name="updatexid" value = "{{ .UpdateXID }}">
<p> <p>
<label id="donker">attach: {{ if .SavedFile }} {{ .SavedFile }} {{ else }} <input onchange="updatedonker();" type="file" name="donk"><span></span> {{ end }}</label> <label id="donker">attach: {{ if .SavedFile }} {{ .SavedFile }} {{ else }} <input onchange="updatedonker();" type="file" name="donk"><span></span> {{ end }}</label>
<input type="hidden" name="donkxid" value="{{ .SavedFile }}"> <input type="hidden" name="donkxid" value="{{ .SavedFile }}">

90
web.go
View File

@ -348,6 +348,9 @@ func inbox(w http.ResponseWriter, r *http.Request) {
return return
case "Question": case "Question":
return return
case "Note":
go consumeactivity(user, j, origin)
return
} }
} }
log.Printf("unknown Update activity") 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) { func submithonk(w http.ResponseWriter, r *http.Request) {
rid := r.FormValue("rid") rid := r.FormValue("rid")
noise := r.FormValue("noise") noise := r.FormValue("noise")
@ -830,18 +857,30 @@ func submithonk(w http.ResponseWriter, r *http.Request) {
user, _ := butwhatabout(userinfo.Username) user, _ := butwhatabout(userinfo.Username)
dt := time.Now().UTC() dt := time.Now().UTC()
xid := fmt.Sprintf("%s/%s/%s", user.URL, honkSep, xfiltrate()) updatexid := r.FormValue("updatexid")
what := "honk" var honk *Honk
if rid != "" { if updatexid != "" {
what = "tonk" honk = getxonk(userinfo.UserID, updatexid)
} if honk == nil || honk.Honker != user.URL || honk.What != "honk" {
honk := Honk{ log.Printf("not saving edit")
UserID: userinfo.UserID, return
Username: userinfo.Username, }
What: "honk", honk.Date = dt
Honker: user.URL, honk.What = "update"
XID: xid, } else {
Date: dt, 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:") { if strings.HasPrefix(noise, "DZ:") {
idx := strings.Index(noise, "\n") idx := strings.Index(noise, "\n")
@ -977,7 +1016,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) {
honk.Donks = append(honk.Donks, donk) honk.Donks = append(honk.Donks, donk)
} }
} }
memetize(&honk) memetize(honk)
if honk.Public { if honk.Public {
honk.Whofore = 2 honk.Whofore = 2
@ -985,7 +1024,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) {
honk.Whofore = 3 honk.Whofore = 3
} }
if r.FormValue("preview") == "preview" { if r.FormValue("preview") == "preview" {
honks := []*Honk{&honk} honks := []*Honk{honk}
reverbolate(userinfo.UserID, honks) reverbolate(userinfo.UserID, honks)
templinfo := getInfo(r) templinfo := getInfo(r)
templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", 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.Onts = oneofakind(ontologies(honk.Noise))
honk.UserID = userinfo.UserID honk.UserID = userinfo.UserID
honk.What = what
honk.XID = xid
honk.RID = rid honk.RID = rid
honk.Date = dt honk.Date = dt
honk.Convoy = convoy honk.Convoy = convoy
honk.Format = "html" honk.Format = "html"
err := savehonk(&honk) if updatexid != "" {
if err != nil { updatehonk(honk)
log.Printf("uh oh")
return } else {
err := savehonk(honk)
if err != nil {
log.Printf("uh oh")
return
}
} }
// reload for consistency // reload for consistency
honk.Donks = nil 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) { func showhonkers(w http.ResponseWriter, r *http.Request) {
@ -1451,6 +1494,7 @@ 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.HandleFunc("/edit", edithonkpage)
loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submithonk))) loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submithonk)))
loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk))) 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)))