restructure chatter so target is always available to view

This commit is contained in:
Ted Unangst 2020-05-23 19:17:30 -04:00
parent 81425225e4
commit 68752c28e5
4 changed files with 38 additions and 22 deletions

View File

@ -538,7 +538,7 @@ func savechonk(ch *Chonk) error {
return err 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) duedt := time.Now().Add(-3 * 24 * time.Hour).UTC().Format(dbtimeformat)
rows, err := stmtLoadChonks.Query(userid, duedt) rows, err := stmtLoadChonks.Query(userid, duedt)
if err != nil { if err != nil {
@ -561,7 +561,32 @@ func loadchatter(userid int64) map[string][]*Chonk {
allchonks = append(allchonks, ch) allchonks = append(allchonks, ch)
} }
donksforchonks(allchonks) 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 { func savehonk(h *Honk) error {
@ -821,7 +846,7 @@ var stmtHonksForUserFirstClass *sql.Stmt
var stmtSaveMeta, stmtDeleteAllMeta, stmtDeleteSomeMeta, stmtUpdateHonk *sql.Stmt var stmtSaveMeta, stmtDeleteAllMeta, stmtDeleteSomeMeta, stmtUpdateHonk *sql.Stmt
var stmtHonksISaved, stmtGetFilters, stmtSaveFilter, stmtDeleteFilter *sql.Stmt var stmtHonksISaved, stmtGetFilters, stmtSaveFilter, stmtDeleteFilter *sql.Stmt
var stmtGetTracks *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 { func preparetodie(db *sql.DB, s string) *sql.Stmt {
stmt, err := db.Prepare(s) stmt, err := db.Prepare(s)
@ -901,4 +926,5 @@ func prepareStatements(db *sql.DB) {
stmtGetTracks = preparetodie(db, "select fetches from tracks where xid = ?") stmtGetTracks = preparetodie(db, "select fetches from tracks where xid = ?")
stmtSaveChonk = preparetodie(db, "insert into chonks (userid, xid, who, target, dt, noise, format) values (?, ?, ?, ?, ?, ?, ?)") 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") 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 = ?")
} }

17
honk.go
View File

@ -108,6 +108,11 @@ type Chonk struct {
HTML template.HTML HTML template.HTML
} }
type Chatter struct {
Target string
Chonks []*Chonk
}
type Mention struct { type Mention struct {
Who string Who string
Where string Where string
@ -337,18 +342,6 @@ func main() {
case "backend": case "backend":
backendServer() backendServer()
case "test": 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() ElaborateUnitTests()
default: default:
log.Fatal("unknown command") log.Fatal("unknown command")

View File

@ -21,13 +21,10 @@ function updatedonker(el) {
{{ $chonkcsrf := .ChonkCSRF }} {{ $chonkcsrf := .ChonkCSRF }}
{{ range .Chatter }} {{ range .Chatter }}
<section class="honk"> <section class="honk">
{{ $target := "" }}
{{ range . }}
{{ if eq $target "" }}
{{ $target = .Target }}
<p class="chattarget"> <p class="chattarget">
chatter: {{ .Target }} chatter: {{ .Target }}
{{ end }} {{ $target := .Target }}
{{ range .Chonks }}
<div class="chat"> <div class="chat">
<p> <p>
<span class="chatstamp">{{ .Date.Local.Format "15:04" }} {{ .Handle }}:</span> <span class="chatstamp">{{ .Date.Local.Format "15:04" }} {{ .Handle }}:</span>

4
web.go
View File

@ -1773,8 +1773,8 @@ func showhonkers(w http.ResponseWriter, r *http.Request) {
func showchatter(w http.ResponseWriter, r *http.Request) { func showchatter(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
chatter := loadchatter(u.UserID) chatter := loadchatter(u.UserID)
for _, chonks := range chatter { for _, chat := range chatter {
for _, ch := range chonks { for _, ch := range chat.Chonks {
filterchonk(ch) filterchonk(ch)
} }
} }