From 90c7001e15a0cc3f76d2bb67103519ae110bf358 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 15 May 2019 16:27:50 -0400 Subject: [PATCH] rework honk schema to store more information. add original honker for bonks. add separate precis field for summary. add format to prepare for changing how html is saved. --- activity.go | 33 ++++++++++++--------------------- fun.go | 6 +++++- honk.go | 38 ++++++++++++++++++++++++++------------ schema.sql | 2 +- upgradedb.go | 9 +++++++++ util.go | 2 +- views/honk.html | 7 +++++++ 7 files changed, 61 insertions(+), 36 deletions(-) diff --git a/activity.go b/activity.go index cb61263..18fd256 100644 --- a/activity.go +++ b/activity.go @@ -292,7 +292,7 @@ func savexonk(user *WhatAbout, x *Honk) { whofore = 1 } res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, - x.Noise, x.Convoy, whofore) + x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker) if err != nil { log.Printf("err saving xonk: %s", err) return @@ -463,7 +463,7 @@ func xonkxonk(user *WhatAbout, item interface{}) *Honk { var audience []string var err error - var xid, rid, url, content, convoy string + var xid, rid, url, content, precis, convoy, oonker string var obj map[string]interface{} var ok bool switch what { @@ -511,19 +511,17 @@ func xonkxonk(user *WhatAbout, item interface{}) *Honk { if who == "" { who, _ = jsongetstring(obj, "attributedTo") } + oonker, _ = jsongetstring(obj, "attributedTo") ot, _ := jsongetstring(obj, "type") url, _ = jsongetstring(obj, "url") if ot == "Note" || ot == "Article" || ot == "Page" { audience = newphone(audience, obj) xid, _ = jsongetstring(obj, "id") + precis, _ = jsongetstring(obj, "summary") content, _ = jsongetstring(obj, "content") - summary, _ := jsongetstring(obj, "summary") if !strings.HasPrefix(content, "

") { content = "

" + content } - if summary != "" { - content = "

summary: " + summary + content - } rid, _ = jsongetstring(obj, "inReplyTo") convoy, _ = jsongetstring(obj, "context") if convoy == "" { @@ -582,6 +580,9 @@ func xonkxonk(user *WhatAbout, item interface{}) *Honk { audience = oneofakind(audience) + if oonker == who { + oonker = "" + } xonk.UserID = user.ID xonk.What = what xonk.Honker = who @@ -590,8 +591,10 @@ func xonkxonk(user *WhatAbout, item interface{}) *Honk { xonk.Date, _ = time.Parse(time.RFC3339, dt) xonk.URL = url xonk.Noise = content + xonk.Precis = precis xonk.Audience = audience xonk.Convoy = convoy + xonk.Oonker = oonker if needxonk(user, &xonk) { if what == "tonk" { @@ -721,22 +724,10 @@ func jonkjonk(user *WhatAbout, h *Honk) (map[string]interface{}, map[string]inte if len(h.Audience) > 1 { jo["cc"] = h.Audience[1:] } - if strings.HasPrefix(h.Noise, "DZ:") { - // alas, it's already been turned into html - idx := strings.Index(h.Noise, "
") - if idx == -1 { - jo["summary"] = h.Noise - jo["content"] = "" - } else { - jo["summary"] = h.Noise[:idx] - if strings.HasPrefix(h.Noise[idx+4:], "
") { - idx += 4 - } - jo["content"] = mentionize(h.Noise[idx+4:]) - } + jo["summary"] = h.Precis + jo["content"] = mentionize(h.Noise) + if strings.HasPrefix(h.Precis, "DZ:") { jo["sensitive"] = true - } else { - jo["content"] = mentionize(h.Noise) } var tags []interface{} g := bunchofgrapes(h.Noise) diff --git a/fun.go b/fun.go index ce32d5f..04d420f 100644 --- a/fun.go +++ b/fun.go @@ -52,7 +52,11 @@ func reverbolate(honks []*Honk) { } zap := make(map[*Donk]bool) h.Noise = unpucker(h.Noise) - h.HTML = cleanstring(h.Noise) + precis := h.Precis + if precis != "" { + precis = "

summary: " + precis + "

" + } + h.HTML = cleanstring(precis + h.Noise) emuxifier := func(e string) string { for _, d := range h.Donks { if d.Name == e { diff --git a/honk.go b/honk.go index 5b88e53..8a5557c 100644 --- a/honk.go +++ b/honk.go @@ -55,11 +55,13 @@ type Honk struct { Username string What string Honker string + Oonker string XID string RID string Date time.Time URL string Noise string + Precis string Convoy string Audience []string Privacy string @@ -566,8 +568,8 @@ func getxonk(userid int64, xid string) *Honk { h := new(Honk) var dt, aud string row := stmtOneXonk.QueryRow(userid, xid) - err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, - &dt, &h.URL, &aud, &h.Noise, &h.Convoy) + err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, &h.XID, &h.RID, + &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Convoy) if err != nil { if err != sql.ErrNoRows { log.Printf("error scanning xonk: %s", err) @@ -627,8 +629,8 @@ func getsomehonks(rows *sql.Rows, err error) []*Honk { for rows.Next() { var h Honk var dt, aud string - err = rows.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, - &dt, &h.URL, &aud, &h.Noise, &h.Convoy) + err = rows.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, + &h.XID, &h.RID, &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Convoy) if err != nil { log.Printf("error scanning honks: %s", err) return nil @@ -696,12 +698,9 @@ func savebonk(w http.ResponseWriter, r *http.Request) { bonk := Honk{ UserID: userinfo.UserID, Username: userinfo.Username, - Honker: xonk.Honker, What: "bonk", XID: xonk.XID, Date: dt, - Noise: xonk.Noise, - Convoy: xonk.Convoy, Donks: xonk.Donks, Audience: []string{thewholeworld}, } @@ -714,7 +713,7 @@ func savebonk(w http.ResponseWriter, r *http.Request) { whofore = 1 } res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", "", xid, "", - dt.Format(dbtimeformat), "", aud, bonk.Noise, bonk.Convoy, whofore) + dt.Format(dbtimeformat), "", aud, xonk.Noise, xonk.Convoy, whofore, "html", xonk.Precis, xonk.Honker) if err != nil { log.Printf("error saving bonk: %s", err) return @@ -797,6 +796,19 @@ func savehonk(w http.ResponseWriter, r *http.Request) { XID: xid, Date: dt, } + if strings.HasPrefix(noise, "DZ:") { + idx := strings.Index(noise, "\n") + if idx == -1 { + honk.Precis = noise + noise = "" + } else { + honk.Precis = noise[:idx] + noise = noise[idx+1:] + } + } + noise = strings.TrimSpace(noise) + honk.Precis = strings.TrimSpace(honk.Precis) + if noise != "" && noise[0] == '@' { honk.Audience = append(grapevine(noise), thewholeworld) } else { @@ -807,7 +819,8 @@ func savehonk(w http.ResponseWriter, r *http.Request) { xonk := getxonk(userinfo.UserID, rid) if xonk != nil { if xonk.Honker == "" { - rid = "https://" + serverName + "/u/" + xonk.Username + "/h/" + rid + xonk.Honker = "https://" + serverName + "/u/" + xonk.Username + rid = xonk.Honker + "/h/" + rid } honk.Audience = append(honk.Audience, xonk.Audience...) convoy = xonk.Convoy @@ -816,6 +829,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) { honk.Audience = append(honk.Audience, xonkaud...) convoy = c } + honk.Oonker = xonk.Honker honk.RID = rid } if convoy == "" { @@ -897,7 +911,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) { whofore = 1 } res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid, - dt.Format(dbtimeformat), "", aud, noise, convoy, whofore) + dt.Format(dbtimeformat), "", aud, noise, convoy, whofore, "html", honk.Precis, honk.Oonker) if err != nil { log.Printf("error saving honk: %s", err) return @@ -1300,7 +1314,7 @@ func prepareStatements(db *sql.DB) { stmtHasHonker = preparetodie(db, "select honkerid from honkers where xid = ? and userid = ?") stmtDubbers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'dub'") - selecthonks := "select honkid, honks.userid, username, what, honker, honks.xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid " + selecthonks := "select honkid, honks.userid, username, what, honker, oonker, honks.xid, rid, dt, url, audience, noise, precis, convoy from honks join users on honks.userid = users.userid " limit := " order by honkid desc limit 250" butnotthose := " and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100)" stmtOneXonk = preparetodie(db, selecthonks+"where honks.userid = ? and xid = ?") @@ -1312,7 +1326,7 @@ func prepareStatements(db *sql.DB) { stmtHonksByCombo = preparetodie(db, selecthonks+"join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.combos like ?"+butnotthose+limit) stmtHonksByConvoy = preparetodie(db, selecthonks+"where (honks.userid = ? or honker = '') and convoy = ?"+limit) - stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") + stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtFileData = preparetodie(db, "select media, content from files where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") diff --git a/schema.sql b/schema.sql index df87e3d..07aaae1 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,5 @@ -create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer); +create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text); create table donks (honkid integer, fileid integer); create table files(fileid integer primary key, xid text, name text, url text, media text, content blob); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, pubkey text); diff --git a/upgradedb.go b/upgradedb.go index eb26026..2be37c7 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -70,6 +70,15 @@ func upgradedb() { doordie(db, "update config set value = 6 where key = 'dbversion'") fallthrough case 6: + doordie(db, "alter table honks add column format") + doordie(db, "update honks set format = 'html'") + doordie(db, "alter table honks add column precis") + doordie(db, "update honks set precis = ''") + doordie(db, "alter table honks add column oonker") + doordie(db, "update honks set oonker = ''") + doordie(db, "update config set value = 7 where key = 'dbversion'") + fallthrough + case 7: default: log.Fatalf("can't upgrade unknown version %d", dbversion) } diff --git a/util.go b/util.go index a6b0224..4ebc4f7 100644 --- a/util.go +++ b/util.go @@ -70,7 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt -var myVersion = 6 +var myVersion = 7 func initdb() { schema, err := ioutil.ReadFile("schema.sql") diff --git a/views/honk.html b/views/honk.html index 7dc5605..d5352f6 100644 --- a/views/honk.html +++ b/views/honk.html @@ -3,12 +3,19 @@

avatar

{{ .Username }} {{ .What }} {{ .Date.Format "02 Jan 2006 15:04" }} {{ .URL }} +{{ if .Oonker }} +
+ +original: {{ .Oonker }} + +{{ else }} {{ if .RID }}
in reply to: {{ .RID }} {{ end }} +{{ end }}
convoy: {{ .Convoy }}