From 68752c28e52c72591ca637b152c584c4f025e51c Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sat, 23 May 2020 19:17:30 -0400 Subject: [PATCH] restructure chatter so target is always available to view --- database.go | 32 +++++++++++++++++++++++++++++--- honk.go | 17 +++++------------ views/chatter.html | 7 ++----- web.go | 4 ++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/database.go b/database.go index d7bae40..4676e34 100644 --- a/database.go +++ b/database.go @@ -538,7 +538,7 @@ func savechonk(ch *Chonk) error { return err } -func loadchatter(userid int64) map[string][]*Chonk { +func loadchatter(userid int64) []*Chatter { duedt := time.Now().Add(-3 * 24 * time.Hour).UTC().Format(dbtimeformat) rows, err := stmtLoadChonks.Query(userid, duedt) if err != nil { @@ -561,7 +561,32 @@ func loadchatter(userid int64) map[string][]*Chonk { allchonks = append(allchonks, ch) } donksforchonks(allchonks) - return chonks + rows.Close() + rows, err = stmtGetChatters.Query(userid) + if err != nil { + log.Printf("error getting chatters: %s", err) + return nil + } + for rows.Next() { + var target string + err = rows.Scan(&target) + if err != nil { + log.Printf("error scanning chatter: %s", target) + continue + } + if _, ok := chonks[target]; !ok { + chonks[target] = []*Chonk{} + + } + } + var chatter []*Chatter + for target, chonks := range chonks { + chatter = append(chatter, &Chatter{ + Target: target, + Chonks: chonks, + }) + } + return chatter } func savehonk(h *Honk) error { @@ -821,7 +846,7 @@ var stmtHonksForUserFirstClass *sql.Stmt var stmtSaveMeta, stmtDeleteAllMeta, stmtDeleteSomeMeta, stmtUpdateHonk *sql.Stmt var stmtHonksISaved, stmtGetFilters, stmtSaveFilter, stmtDeleteFilter *sql.Stmt var stmtGetTracks *sql.Stmt -var stmtSaveChonk, stmtLoadChonks *sql.Stmt +var stmtSaveChonk, stmtLoadChonks, stmtGetChatters *sql.Stmt func preparetodie(db *sql.DB, s string) *sql.Stmt { stmt, err := db.Prepare(s) @@ -901,4 +926,5 @@ func prepareStatements(db *sql.DB) { stmtGetTracks = preparetodie(db, "select fetches from tracks where xid = ?") stmtSaveChonk = preparetodie(db, "insert into chonks (userid, xid, who, target, dt, noise, format) values (?, ?, ?, ?, ?, ?, ?)") stmtLoadChonks = preparetodie(db, "select chonkid, userid, xid, who, target, dt, noise, format from chonks where userid = ? and dt > ? order by chonkid asc") + stmtGetChatters = preparetodie(db, "select distinct(target) from chonks where userid = ?") } diff --git a/honk.go b/honk.go index a8b05d4..c87a83d 100644 --- a/honk.go +++ b/honk.go @@ -108,6 +108,11 @@ type Chonk struct { HTML template.HTML } +type Chatter struct { + Target string + Chonks []*Chonk +} + type Mention struct { Who string Where string @@ -337,18 +342,6 @@ func main() { case "backend": backendServer() case "test": - xid := args[1] - chatter := loadchatter(1) - for _, chonks := range chatter { - for _, ch := range chonks { - if ch.XID == xid { - var user *WhatAbout - somenumberedusers.Get(ch.UserID, &user) - msg := chonkifymsg(user, ch) - fmt.Println(string(msg)) - } - } - } ElaborateUnitTests() default: log.Fatal("unknown command") diff --git a/views/chatter.html b/views/chatter.html index c7119fb..e9b72fb 100644 --- a/views/chatter.html +++ b/views/chatter.html @@ -21,13 +21,10 @@ function updatedonker(el) { {{ $chonkcsrf := .ChonkCSRF }} {{ range .Chatter }}
-{{ $target := "" }} -{{ range . }} -{{ if eq $target "" }} -{{ $target = .Target }}

chatter: {{ .Target }} -{{ end }} +{{ $target := .Target }} +{{ range .Chonks }}

{{ .Date.Local.Format "15:04" }} {{ .Handle }}: diff --git a/web.go b/web.go index e25153c..bf75030 100644 --- a/web.go +++ b/web.go @@ -1773,8 +1773,8 @@ func showhonkers(w http.ResponseWriter, r *http.Request) { func showchatter(w http.ResponseWriter, r *http.Request) { u := login.GetUserInfo(r) chatter := loadchatter(u.UserID) - for _, chonks := range chatter { - for _, ch := range chonks { + for _, chat := range chatter { + for _, ch := range chat.Chonks { filterchonk(ch) } }