create a plain column and use that for search

This commit is contained in:
Ted Unangst 2023-07-31 19:46:55 -04:00
parent 22d448fa5f
commit a08e0d0cb8
4 changed files with 48 additions and 11 deletions

View File

@ -30,6 +30,7 @@ import (
"time" "time"
"humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/cache"
"humungus.tedunangst.com/r/webs/htfilter"
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/login" "humungus.tedunangst.com/r/webs/login"
"humungus.tedunangst.com/r/webs/mz" "humungus.tedunangst.com/r/webs/mz"
@ -342,8 +343,7 @@ func gethonksbysearch(userid int64, q string, wanted int64) []*Honk {
continue continue
} }
t = "%" + t + "%" t = "%" + t + "%"
queries = append(queries, negate+"(noise like ? or precis like ?)") queries = append(queries, negate+"(plain like ?)")
params = append(params, t)
params = append(params, t) params = append(params, t)
} }
@ -789,6 +789,27 @@ func loadchatter(userid int64) []*Chatter {
return chatter return chatter
} }
func (honk *Honk) Plain() string {
var plain []string
var filt htfilter.Filter
filt.WithLinks = true
if honk.Precis != "" {
t, _ := filt.TextOnly(honk.Precis)
plain = append(plain, t)
}
if honk.Format == "html" {
t, _ := filt.TextOnly(honk.Noise)
plain = append(plain, t)
} else {
plain = append(plain, honk.Noise)
}
for _, d := range honk.Donks {
plain = append(plain, d.Name)
plain = append(plain, d.Desc)
}
return strings.Join(plain, " ")
}
func savehonk(h *Honk) error { func savehonk(h *Honk) error {
dt := h.Date.UTC().Format(dbtimeformat) dt := h.Date.UTC().Format(dbtimeformat)
aud := strings.Join(h.Audience, " ") aud := strings.Join(h.Audience, " ")
@ -799,10 +820,11 @@ func savehonk(h *Honk) error {
elog.Printf("can't begin tx: %s", err) elog.Printf("can't begin tx: %s", err)
return err return err
} }
plain := h.Plain()
res, err := tx.Stmt(stmtSaveHonk).Exec(h.UserID, h.What, h.Honker, h.XID, h.RID, dt, h.URL, res, err := tx.Stmt(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, aud, h.Noise, h.Convoy, h.Whofore, h.Format, h.Precis,
h.Oonker, h.Flags) h.Oonker, h.Flags, plain)
if err == nil { if err == nil {
h.ID, _ = res.LastInsertId() h.ID, _ = res.LastInsertId()
err = saveextras(tx, h) err = saveextras(tx, h)
@ -833,10 +855,11 @@ func updatehonk(h *Honk) error {
elog.Printf("can't begin tx: %s", err) elog.Printf("can't begin tx: %s", err)
return err return err
} }
plain := h.Plain()
err = deleteextras(tx, h.ID, false) err = deleteextras(tx, h.ID, false)
if err == nil { if err == nil {
_, err = tx.Stmt(stmtUpdateHonk).Exec(h.Precis, h.Noise, h.Format, h.Whofore, dt, h.ID) _, err = tx.Stmt(stmtUpdateHonk).Exec(h.Precis, h.Noise, h.Format, h.Whofore, dt, plain, h.ID)
} }
if err == nil { if err == nil {
err = saveextras(tx, h) err = saveextras(tx, h)
@ -1171,9 +1194,9 @@ func prepareStatements(db *sql.DB) {
stmtDeleteAllMeta = preparetodie(db, "delete from honkmeta where honkid = ?") stmtDeleteAllMeta = preparetodie(db, "delete from honkmeta where honkid = ?")
stmtDeleteSomeMeta = preparetodie(db, "delete from honkmeta where honkid = ? and genus not in ('oldrev')") stmtDeleteSomeMeta = preparetodie(db, "delete from honkmeta where honkid = ? and genus not in ('oldrev')")
stmtDeleteOneMeta = preparetodie(db, "delete from honkmeta where honkid = ? and genus = ?") stmtDeleteOneMeta = preparetodie(db, "delete from honkmeta where honkid = ? and genus = ?")
stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags, plain) 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 = ?, format = ?, whofore = ?, dt = ? where honkid = ?") stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, format = ?, whofore = ?, dt = ?, plain = ? 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, chonkid, fileid) values (?, ?, ?)") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, chonkid, fileid) values (?, ?, ?)")

View File

@ -1,5 +1,4 @@
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, flags integer, plain text);
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, flags integer);
create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text); create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text);
create table donks (honkid integer, chonkid integer, fileid integer); create table donks (honkid integer, chonkid integer, fileid integer);
create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer); create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer);

View File

@ -20,7 +20,7 @@ import (
"os" "os"
) )
var myVersion = 43 var myVersion = 44
type dbexecer interface { type dbexecer interface {
Exec(query string, args ...interface{}) (sql.Result, error) Exec(query string, args ...interface{}) (sql.Result, error)
@ -42,6 +42,16 @@ func upgradedb() {
if dbversion < 40 { if dbversion < 40 {
elog.Fatal("database is too old to upgrade") elog.Fatal("database is too old to upgrade")
} }
try := func(s string, args ...interface{}) {
_, err := db.Exec(s, args...)
if err != nil {
elog.Fatalf("can't run %s: %s", s, err)
}
}
setV := func(ver int64) {
try("update config set value = ? where key = 'dbversion'", ver)
}
switch dbversion { switch dbversion {
case 40: case 40:
doordie(db, "PRAGMA journal_mode=WAL") doordie(db, "PRAGMA journal_mode=WAL")
@ -87,6 +97,11 @@ func upgradedb() {
doordie(db, "update config set value = 43 where key = 'dbversion'") doordie(db, "update config set value = 43 where key = 'dbversion'")
fallthrough fallthrough
case 43: case 43:
try("alter table honks add column plain text")
try("update honks set plain = ''")
setV(44)
fallthrough
case 44:
default: default:
elog.Fatalf("can't upgrade unknown version %d", dbversion) elog.Fatalf("can't upgrade unknown version %d", dbversion)