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"
"humungus.tedunangst.com/r/webs/cache"
"humungus.tedunangst.com/r/webs/htfilter"
"humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/login"
"humungus.tedunangst.com/r/webs/mz"
@ -342,8 +343,7 @@ func gethonksbysearch(userid int64, q string, wanted int64) []*Honk {
continue
}
t = "%" + t + "%"
queries = append(queries, negate+"(noise like ? or precis like ?)")
params = append(params, t)
queries = append(queries, negate+"(plain like ?)")
params = append(params, t)
}
@ -789,6 +789,27 @@ func loadchatter(userid int64) []*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 {
dt := h.Date.UTC().Format(dbtimeformat)
aud := strings.Join(h.Audience, " ")
@ -799,10 +820,11 @@ func savehonk(h *Honk) error {
elog.Printf("can't begin tx: %s", 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,
aud, h.Noise, h.Convoy, h.Whofore, h.Format, h.Precis,
h.Oonker, h.Flags)
h.Oonker, h.Flags, plain)
if err == nil {
h.ID, _ = res.LastInsertId()
err = saveextras(tx, h)
@ -833,10 +855,11 @@ func updatehonk(h *Honk) error {
elog.Printf("can't begin tx: %s", err)
return err
}
plain := h.Plain()
err = deleteextras(tx, h.ID, false)
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 {
err = saveextras(tx, h)
@ -1171,9 +1194,9 @@ func prepareStatements(db *sql.DB) {
stmtDeleteAllMeta = preparetodie(db, "delete from honkmeta where honkid = ?")
stmtDeleteSomeMeta = preparetodie(db, "delete from honkmeta where honkid = ? and genus not in ('oldrev')")
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 = ?")
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 (?, ?)")
stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, chonkid, fileid) values (?, ?, ?)")

View File

@ -152,10 +152,10 @@ func OntIsPresent(ont, noise string) bool {
idx += 6
}
idx += 1
if idx + len(ont) >= len(noise) {
if idx+len(ont) >= len(noise) {
return false
}
test := noise[idx:idx+len(ont)]
test := noise[idx : idx+len(ont)]
test = strings.ToLower(test)
if test == ont {
return true

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);
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 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 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"
)
var myVersion = 43
var myVersion = 44
type dbexecer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
@ -42,6 +42,16 @@ func upgradedb() {
if dbversion < 40 {
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 {
case 40:
doordie(db, "PRAGMA journal_mode=WAL")
@ -87,6 +97,11 @@ func upgradedb() {
doordie(db, "update config set value = 43 where key = 'dbversion'")
fallthrough
case 43:
try("alter table honks add column plain text")
try("update honks set plain = ''")
setV(44)
fallthrough
case 44:
default:
elog.Fatalf("can't upgrade unknown version %d", dbversion)