@me page just for me

This commit is contained in:
Ted Unangst 2019-04-20 11:17:00 -04:00
parent e8c5a307ce
commit 770e57797e
6 changed files with 49 additions and 18 deletions

View File

@ -243,7 +243,7 @@ func needxonk(user *WhatAbout, x *Honk) bool {
return true return true
} }
func savexonk(x *Honk) { func savexonk(user *WhatAbout, x *Honk) {
if x.What == "eradicate" { if x.What == "eradicate" {
log.Printf("eradicating %s by %s", x.RID, x.Honker) log.Printf("eradicating %s by %s", x.RID, x.Honker)
_, err := stmtDeleteHonk.Exec(x.RID, x.Honker) _, err := stmtDeleteHonk.Exec(x.RID, x.Honker)
@ -254,7 +254,12 @@ func savexonk(x *Honk) {
} }
dt := x.Date.UTC().Format(dbtimeformat) dt := x.Date.UTC().Format(dbtimeformat)
aud := strings.Join(x.Audience, " ") aud := strings.Join(x.Audience, " ")
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, x.Noise, x.Convoy) whofore := 0
if strings.Contains(aud, user.URL) {
whofore = 1
}
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
x.Noise, x.Convoy, whofore)
if err != nil { if err != nil {
log.Printf("err saving xonk: %s", err) log.Printf("err saving xonk: %s", err)
return return
@ -347,7 +352,7 @@ func peeppeep() {
xonk := xonkxonk(item) xonk := xonkxonk(item)
if xonk != nil && needxonk(user, xonk) { if xonk != nil && needxonk(user, xonk) {
xonk.UserID = user.ID xonk.UserID = user.ID
savexonk(xonk) savexonk(user, xonk)
} }
} }
} }

37
honk.go
View File

@ -109,7 +109,11 @@ func homepage(w http.ResponseWriter, r *http.Request) {
u := GetUserInfo(r) u := GetUserInfo(r)
var honks []*Honk var honks []*Honk
if u != nil { if u != nil {
if r.URL.Path == "/atme" {
honks = gethonksforme(u.UserID)
} else {
honks = gethonksforuser(u.UserID) honks = gethonksforuser(u.UserID)
}
templinfo["HonkCSRF"] = GetCSRF("honkhonk", r) templinfo["HonkCSRF"] = GetCSRF("honkhonk", r)
} else { } else {
honks = gethonks() honks = gethonks()
@ -354,7 +358,7 @@ func inbox(w http.ResponseWriter, r *http.Request) {
xonk := xonkxonk(j) xonk := xonkxonk(j)
if xonk != nil && needxonk(user, xonk) { if xonk != nil && needxonk(user, xonk) {
xonk.UserID = user.ID xonk.UserID = user.ID
savexonk(xonk) savexonk(user, xonk)
} }
} }
} }
@ -596,6 +600,11 @@ func gethonksforuser(userid int64) []*Honk {
rows, err := stmtHonksForUser.Query(userid, dt.Format(dbtimeformat), userid) rows, err := stmtHonksForUser.Query(userid, dt.Format(dbtimeformat), userid)
return getsomehonks(rows, err) return getsomehonks(rows, err)
} }
func gethonksforme(userid int64) []*Honk {
dt := time.Now().UTC().Add(-2 * 24 * time.Hour)
rows, err := stmtHonksForMe.Query(userid, dt.Format(dbtimeformat), userid)
return getsomehonks(rows, err)
}
func gethonksbyhonker(userid int64, honker string) []*Honk { func gethonksbyhonker(userid int64, honker string) []*Honk {
rows, err := stmtHonksByHonker.Query(userid, honker) rows, err := stmtHonksByHonker.Query(userid, honker)
return getsomehonks(rows, err) return getsomehonks(rows, err)
@ -687,9 +696,15 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
Audience: oneofakind(prepend(thewholeworld, xonk.Audience)), Audience: oneofakind(prepend(thewholeworld, xonk.Audience)),
} }
user, _ := butwhatabout(userinfo.Username)
aud := strings.Join(bonk.Audience, " ") aud := strings.Join(bonk.Audience, " ")
whofore := 0
if strings.Contains(aud, user.URL) {
whofore = 1
}
res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", "", xid, "", res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", "", xid, "",
dt.Format(dbtimeformat), "", aud, bonk.Noise, bonk.Convoy) dt.Format(dbtimeformat), "", aud, bonk.Noise, bonk.Convoy, whofore)
if err != nil { if err != nil {
log.Printf("error saving bonk: %s", err) log.Printf("error saving bonk: %s", err)
return return
@ -703,8 +718,6 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
} }
} }
user, _ := butwhatabout(userinfo.Username)
go honkworldwide(user, &bonk) go honkworldwide(user, &bonk)
} }
@ -827,9 +840,15 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
} }
} }
user, _ := butwhatabout(userinfo.Username)
aud := strings.Join(honk.Audience, " ") aud := strings.Join(honk.Audience, " ")
whofore := 0
if strings.Contains(aud, user.URL) {
whofore = 1
}
res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid, res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid,
dt.Format(dbtimeformat), "", aud, noise, convoy) dt.Format(dbtimeformat), "", aud, noise, convoy, whofore)
if err != nil { if err != nil {
log.Printf("error saving honk: %s", err) log.Printf("error saving honk: %s", err)
return return
@ -843,8 +862,6 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
} }
} }
user, _ := butwhatabout(userinfo.Username)
go honkworldwide(user, &honk) go honkworldwide(user, &honk)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
@ -1015,6 +1032,7 @@ func serve() {
getters := mux.Methods("GET").Subrouter() getters := mux.Methods("GET").Subrouter()
getters.HandleFunc("/", homepage) getters.HandleFunc("/", homepage)
getters.Handle("/atme", LoginRequired(http.HandlerFunc(homepage)))
getters.HandleFunc("/rss", showrss) getters.HandleFunc("/rss", showrss)
getters.HandleFunc("/u/{name:[[:alnum:]]+}", viewuser) getters.HandleFunc("/u/{name:[[:alnum:]]+}", viewuser)
getters.HandleFunc("/u/{name:[[:alnum:]]+}/h/{xid:[[:alnum:]]+}", viewhonk) getters.HandleFunc("/u/{name:[[:alnum:]]+}/h/{xid:[[:alnum:]]+}", viewhonk)
@ -1049,7 +1067,7 @@ func serve() {
} }
var stmtHonkers, stmtDubbers, stmtOneXonk, stmtHonks, stmtUserHonks *sql.Stmt var stmtHonkers, stmtDubbers, stmtOneXonk, stmtHonks, stmtUserHonks *sql.Stmt
var stmtHonksForUser, stmtDeleteHonk, stmtSaveDub *sql.Stmt var stmtHonksForUser, stmtHonksForMe, 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
var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt
@ -1070,8 +1088,9 @@ func prepareStatements(db *sql.DB) {
stmtHonks = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' order by honkid desc limit 50") stmtHonks = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' order by honkid desc limit 50")
stmtUserHonks = preparetodie(db, "select honkid, honks.userid, username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' and username = ? order by honkid desc limit 50") stmtUserHonks = preparetodie(db, "select honkid, honks.userid, username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' and username = ? order by honkid desc limit 50")
stmtHonksForUser = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100) order by honkid desc limit 250") stmtHonksForUser = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100) order by honkid desc limit 250")
stmtHonksForMe = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? and whofore = 1 and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100) order by honkid desc limit 250")
stmtHonksByHonker = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, honks.xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.name = ? order by honkid desc limit 50") stmtHonksByHonker = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, honks.xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.name = ? order by honkid desc limit 50")
stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
stmtFileData = preparetodie(db, "select media, content from files where xid = ?") stmtFileData = preparetodie(db, "select media, content from files where xid = ?")
stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)")

View File

@ -1,5 +1,5 @@
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); 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);
create table donks (honkid integer, fileid integer); create table donks (honkid integer, fileid integer);
create table files(fileid integer primary key, xid text, name text, url text, media text, content blob); create table files(fileid integer primary key, xid text, name text, url text, media text, content blob);
create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, pubkey text); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, pubkey text);

View File

@ -54,6 +54,12 @@ func upgradedb() {
doordie(db, "update config set value = 3 where key = 'dbversion'") doordie(db, "update config set value = 3 where key = 'dbversion'")
fallthrough fallthrough
case 3: case 3:
doordie(db, "alter table honks add column whofore integer")
doordie(db, "update honks set whofore = 0")
doordie(db, "update honks set whofore = 1 where honkid in (select honkid from honks join users on honks.userid = users.userid where instr(audience, username) > 0)")
doordie(db, "update config set value = 4 where key = 'dbversion'")
fallthrough
case 4:
default: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }

View File

@ -70,7 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05"
var alreadyopendb *sql.DB var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 3 var myVersion = 4
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")

View File

@ -12,14 +12,15 @@
<body> <body>
<div class="header"> <div class="header">
<span><a href="/">honk</a></span> <span><a href="/">honk</a></span>
{{ if .ShowRSS }}
<span><a href="/rss">rss</a></span>
{{ end }}
{{ if .UserInfo }} {{ if .UserInfo }}
<span><a href="/atme">@me</a></span>
<span><a href="/u/{{ .UserInfo.Username }}">{{ .UserInfo.Username }}</a></span> <span><a href="/u/{{ .UserInfo.Username }}">{{ .UserInfo.Username }}</a></span>
<span><a href="/honkers">honkers</a></span> <span><a href="/honkers">honkers</a></span>
<span><a href="/logout?CSRF={{ .LogoutCSRF }}">logout</a></span> <span><a href="/logout?CSRF={{ .LogoutCSRF }}">logout</a></span>
{{ else }} {{ else }}
<span><a href="/login">login</a></span> <span><a href="/login">login</a></span>
{{ end }} {{ end }}
{{ if .ShowRSS }}
<span><a href="/rss">rss</a></span>
{{ end }}
</div> </div>