clean up honk queries some

This commit is contained in:
Ted Unangst 2019-04-13 22:57:16 -04:00
parent a21eccc71c
commit b867a2bba3
1 changed files with 30 additions and 49 deletions

77
honk.go
View File

@ -103,12 +103,13 @@ func getInfo(r *http.Request) map[string]interface{} {
func homepage(w http.ResponseWriter, r *http.Request) { func homepage(w http.ResponseWriter, r *http.Request) {
templinfo := getInfo(r) templinfo := getInfo(r)
honks := gethonks("")
u := GetUserInfo(r) u := GetUserInfo(r)
var honks []*Honk
if u != nil { if u != nil {
morehonks := gethonksforuser(u.UserID) honks = gethonksforuser(u.UserID)
honks = append(honks, morehonks...)
templinfo["HonkCSRF"] = GetCSRF("honkhonk", r) templinfo["HonkCSRF"] = GetCSRF("honkhonk", r)
} else {
honks = gethonks()
} }
sort.Slice(honks, func(i, j int) bool { sort.Slice(honks, func(i, j int) bool {
return honks[i].Date.After(honks[j].Date) return honks[i].Date.After(honks[j].Date)
@ -146,7 +147,12 @@ func homepage(w http.ResponseWriter, r *http.Request) {
func showrss(w http.ResponseWriter, r *http.Request) { func showrss(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"] name := mux.Vars(r)["name"]
honks := gethonks(name) var honks []*Honk
if name != "" {
honks = gethonksbyuser(name)
} else {
honks = gethonks()
}
sort.Slice(honks, func(i, j int) bool { sort.Slice(honks, func(i, j int) bool {
return honks[i].Date.After(honks[j].Date) return honks[i].Date.After(honks[j].Date)
}) })
@ -343,7 +349,7 @@ func outbox(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
honks := gethonks(name) honks := gethonksbyuser(name)
var jonks []map[string]interface{} var jonks []map[string]interface{}
for _, h := range honks { for _, h := range honks {
@ -375,7 +381,7 @@ func viewuser(w http.ResponseWriter, r *http.Request) {
WriteJunk(w, j) WriteJunk(w, j)
return return
} }
honks := gethonks(name) honks := gethonksbyuser(name)
u := GetUserInfo(r) u := GetUserInfo(r)
honkpage(w, r, u, user, honks) honkpage(w, r, u, user, honks)
} }
@ -531,21 +537,6 @@ func getdubs(userid int64) []*Honker {
return honkers return honkers
} }
func gethonk(honkid int64) *Honk {
var h Honk
var dt, aud string
row := stmtOneHonk.QueryRow(honkid)
err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID,
&dt, &h.URL, &aud, &h.Noise)
if err != nil {
log.Printf("error scanning honk: %s", err)
return nil
}
h.Date, _ = time.Parse(dbtimeformat, dt)
h.Audience = strings.Split(aud, " ")
return &h
}
func getxonk(name, xid string) *Honk { func getxonk(name, xid string) *Honk {
var h Honk var h Honk
var dt, aud string var dt, aud string
@ -566,29 +557,24 @@ func getxonk(name, xid string) *Honk {
return &h return &h
} }
func gethonks(username string) []*Honk { func gethonks() []*Honk {
return getsomehonks(username, 0, "") rows, err := stmtHonks.Query()
return getsomehonks(rows, err)
}
func gethonksbyuser(name string) []*Honk {
rows, err := stmtUserHonks.Query(name)
return getsomehonks(rows, err)
} }
func gethonksforuser(userid int64) []*Honk { func gethonksforuser(userid int64) []*Honk {
return getsomehonks("", userid, "") rows, err := stmtHonksForUser.Query(userid)
return getsomehonks(rows, err)
} }
func gethonksbyhonker(userid int64, honker string) []*Honk { func gethonksbyhonker(userid int64, honker string) []*Honk {
return getsomehonks("", userid, honker) rows, err := stmtHonksByHonker.Query(userid, honker)
return getsomehonks(rows, err)
} }
func getsomehonks(username string, userid int64, honkername string) []*Honk { func getsomehonks(rows *sql.Rows, err error) []*Honk {
var rows *sql.Rows
var err error
if username != "" {
rows, err = stmtUserHonks.Query(username)
} else if honkername != "" {
rows, err = stmtHonksByHonker.Query(userid, honkername)
} else if userid > 0 {
rows, err = stmtHonksForUser.Query(userid)
} else {
rows, err = stmtHonks.Query()
}
if err != nil { if err != nil {
log.Printf("error querying honks: %s", err) log.Printf("error querying honks: %s", err)
return nil return nil
@ -616,8 +602,10 @@ func getsomehonks(username string, userid int64, honkername string) []*Honk {
func donksforhonks(honks []*Honk) { func donksforhonks(honks []*Honk) {
db := opendatabase() db := opendatabase()
var ids []string var ids []string
hmap := make(map[int64]*Honk)
for _, h := range honks { for _, h := range honks {
ids = append(ids, fmt.Sprintf("%d", h.ID)) ids = append(ids, fmt.Sprintf("%d", h.ID))
hmap[h.ID] = h
} }
q := fmt.Sprintf("select honkid, donks.fileid, xid, name, url, media from donks join files on donks.fileid = files.fileid where honkid in (%s)", strings.Join(ids, ",")) q := fmt.Sprintf("select honkid, donks.fileid, xid, name, url, media from donks join files on donks.fileid = files.fileid where honkid in (%s)", strings.Join(ids, ","))
rows, err := db.Query(q) rows, err := db.Query(q)
@ -634,13 +622,10 @@ func donksforhonks(honks []*Honk) {
log.Printf("error scanning donk: %s", err) log.Printf("error scanning donk: %s", err)
continue continue
} }
for _, h := range honks { h := hmap[hid]
if h.ID == hid {
h.Donks = append(h.Donks, &d) h.Donks = append(h.Donks, &d)
} }
} }
}
}
func savebonk(w http.ResponseWriter, r *http.Request) { func savebonk(w http.ResponseWriter, r *http.Request) {
xid := r.FormValue("xid") xid := r.FormValue("xid")
@ -985,7 +970,7 @@ func serve() {
} }
} }
var stmtHonkers, stmtDubbers, stmtOneHonk, stmtOneXonk, stmtHonks, stmtUserHonks *sql.Stmt var stmtHonkers, stmtDubbers, stmtOneXonk, stmtHonks, stmtUserHonks *sql.Stmt
var stmtHonksForUser, stmtDeleteHonk, stmtSaveDub *sql.Stmt var stmtHonksForUser, stmtDeleteHonk, stmtSaveDub *sql.Stmt
var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt
var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt
@ -1000,10 +985,6 @@ func prepareStatements(db *sql.DB) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
stmtOneHonk, err = db.Prepare("select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honkid = ? limit 50")
if err != nil {
log.Fatal(err)
}
stmtOneXonk, err = db.Prepare("select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where xid = ?") stmtOneXonk, err = db.Prepare("select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where xid = ?")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -1016,7 +997,7 @@ func prepareStatements(db *sql.DB) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
stmtHonksForUser, err = db.Prepare("select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honks.userid = ? and honker <> '' and what <> 'zonk' order by honkid desc limit 150") stmtHonksForUser, err = db.Prepare("select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honks.userid = ? order by honkid desc limit 50")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }