all repos — honk @ dea3cb9d2c80622a1ff6273f60ac0fa87e0d3847

my fork of honk

support inline images in received posts
Ted Unangst tedu@tedunangst.com
Wed, 02 Oct 2019 23:02:01 -0400
commit

dea3cb9d2c80622a1ff6273f60ac0fa87e0d3847

parent

e0286830e8c33a62501d9547afacb856d2050615

5 files changed, 55 insertions(+), 15 deletions(-)

jump to
M activity.goactivity.go

@@ -30,6 +30,7 @@ "strings"

"sync" "time" + "humungus.tedunangst.com/r/webs/htfilter" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/junk"

@@ -127,16 +128,12 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {

if url == "" { return nil } - var donk Donk - row := stmtFindFile.QueryRow(url) - err := row.Scan(&donk.FileID) - if err == nil { - return &donk + donk := finddonk(url) + if donk != nil { + return donk } + donk = new(Donk) log.Printf("saving donk: %s", url) - if err != nil && err != sql.ErrNoRows { - log.Printf("error querying: %s", err) - } xid := xfiltrate() data := []byte{} if localize {

@@ -184,7 +181,8 @@ log.Printf("error saving file %s: %s", url, err)

return nil } donk.FileID = fileid - return &donk + donk.XID = xid + return donk } func iszonked(userid int64, xid string) bool {

@@ -764,6 +762,11 @@

if currenttid == "" { currenttid = convoy } + + // grab any inline imgs + imgfilt := htfilter.New() + imgfilt.Imager = inlineimgs + imgfilt.String(content) // init xonk xonk.What = what
M database.godatabase.go

@@ -309,6 +309,19 @@ }

return fileid, nil } +func finddonk(url string) *Donk { + donk := new(Donk) + row := stmtFindFile.QueryRow(url) + err := row.Scan(&donk.FileID, &donk.XID) + if err == nil { + return donk + } + if err != sql.ErrNoRows { + log.Printf("error finding file: %s", err) + } + return nil +} + func savehonk(h *Honk) error { dt := h.Date.UTC().Format(dbtimeformat) aud := strings.Join(h.Audience, " ")

@@ -550,7 +563,7 @@ blobdb := openblobdb()

stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, content) values (?, ?, ?)") stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") - stmtFindFile = preparetodie(db, "select fileid from filemeta where url = ? and local = 1") + stmtFindFile = preparetodie(db, "select fileid, xid from filemeta where url = ? and local = 1") stmtWhatAbout = preparetodie(db, "select userid, username, displayname, about, pubkey, options from users where username = ?") stmtSaveDub = preparetodie(db, "insert into honkers (userid, name, xid, flavor) values (?, ?, ?, ?)") stmtAddDoover = preparetodie(db, "insert into doovers (dt, tries, username, rcpt, msg) values (?, ?, ?, ?, ?)")
M docs/changelog.txtdocs/changelog.txt

@@ -2,6 +2,8 @@ changelog

-- next ++ Inline images in received posts. + + Times for events. + Split media database into separate blob.db.
M fun.gofun.go

@@ -120,7 +120,31 @@ return alt

} alt = html.EscapeString(alt) src = html.EscapeString(src) + d := finddonk(src) + if d != nil { + src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID) + return fmt.Sprintf(`<img alt="%s" title="%s" src="%s">`, alt, alt, src) + } return fmt.Sprintf(`&lt;img alt="%s" src="<a href="%s">%s<a>"&gt;`, alt, src, src) +} + +func inlineimgs(node *html.Node) string { + src := htfilter.GetAttr(node, "src") + alt := htfilter.GetAttr(node, "alt") + //title := GetAttr(node, "title") + if htfilter.HasClass(node, "Emoji") && alt != "" { + return alt + } + alt = html.EscapeString(alt) + src = html.EscapeString(src) + if !strings.HasPrefix(src, "https://"+serverName+"/") { + d := savedonk(src, "image", alt, "image", true) + if d != nil { + src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID) + } + } + log.Printf("inline img with src: %s", src) + return fmt.Sprintf(`<img alt="%s" title="%s" src="%s>`, alt, alt, src) } func translate(honk *Honk) {
M web.goweb.go

@@ -1022,11 +1022,9 @@ }

} else { xid := donkxid url := fmt.Sprintf("https://%s/d/%s", serverName, xid) - var donk Donk - row := stmtFindFile.QueryRow(url) - err := row.Scan(&donk.FileID) - if err == nil { - honk.Donks = append(honk.Donks, &donk) + donk := finddonk(url) + if donk != nil { + honk.Donks = append(honk.Donks, donk) } else { log.Printf("can't find file: %s", xid) }