API access for honkers.

This commit is contained in:
Ted Unangst 2023-02-19 17:46:59 -05:00
parent ad5a149f64
commit e08c21228c
4 changed files with 64 additions and 22 deletions

View File

@ -962,7 +962,7 @@ func savexonker(what, value, flav, when string) {
stmtSaveXonker.Exec(what, value, flav, when) stmtSaveXonker.Exec(what, value, flav, when)
} }
func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error { func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) (int64, error) {
var owner string var owner string
if url[0] == '#' { if url[0] == '#' {
flavor = "peep" flavor = "peep"
@ -974,7 +974,7 @@ func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error {
info, err := investigate(url) info, err := investigate(url)
if err != nil { if err != nil {
ilog.Printf("failed to investigate honker: %s", err) ilog.Printf("failed to investigate honker: %s", err)
return err return 0, err
} }
url = info.XID url = info.XID
if name == "" { if name == "" {
@ -993,19 +993,19 @@ func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error {
} else { } else {
err = fmt.Errorf("it seems you are already subscribed to them") err = fmt.Errorf("it seems you are already subscribed to them")
} }
return err return 0, err
} }
res, err := stmtSaveHonker.Exec(user.ID, name, url, flavor, combos, owner, mj) res, err := stmtSaveHonker.Exec(user.ID, name, url, flavor, combos, owner, mj)
if err != nil { if err != nil {
elog.Print(err) elog.Print(err)
return err return 0, err
} }
honkerid, _ := res.LastInsertId() honkerid, _ := res.LastInsertId()
if flavor == "presub" { if flavor == "presub" {
followyou(user, honkerid) followyou(user, honkerid)
} }
return nil return honkerid, nil
} }
func cleanupdb(arg string) { func cleanupdb(arg string) {

View File

@ -158,6 +158,27 @@ Delete this honk.
Mute this thread. Mute this thread.
What should identify a convoy. What should identify a convoy.
.El .El
.Ss gethonkers
Returns a list of current honkers in json format.
.Ss savehonker
Save a new honker, or update an existing one.
The following fields are used.
.Bl -tag -width honkerid
.It Fa honkerid
The numeric ID of an existing honker to update.
.It Fa name
The preferred short name.
.It Fa url
The ActivityPub actor url.
.It Fa combos
Space separated list of combos.
.It Fa notes
Some notes.
.El
.Pp
The honker numeric ID will be returned for success.
To delete, unsub, or sub, include a form value with name and value equal.
As in, a form value named delete with the value delete, or unsub=unsub, etc.
.Ss sendactivity .Ss sendactivity
Send anything. Send anything.
No limits, no error checking. No limits, no error checking.

View File

@ -210,7 +210,7 @@ func importMastotooters(user *WhatAbout, source string) {
name := "" name := ""
flavor := "peep" flavor := "peep"
combos := "" combos := ""
err := savehonker(user, url, name, flavor, combos, mj) _, err := savehonker(user, url, name, flavor, combos, mj)
if err != nil { if err != nil {
elog.Printf("trouble with a honker: %s", err) elog.Printf("trouble with a honker: %s", err)
} }

53
web.go
View File

@ -1563,7 +1563,7 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
return d, nil return d, nil
} }
func submitwebhonk(w http.ResponseWriter, r *http.Request) { func websubmithonk(w http.ResponseWriter, r *http.Request) {
h := submithonk(w, r) h := submithonk(w, r)
if h == nil { if h == nil {
return return
@ -1884,7 +1884,15 @@ func showcombos(w http.ResponseWriter, r *http.Request) {
} }
} }
func submithonker(w http.ResponseWriter, r *http.Request) { func websubmithonker(w http.ResponseWriter, r *http.Request) {
h := submithonker(w, r)
if h == nil {
return
}
http.Redirect(w, r, "/honkers", http.StatusSeeOther)
}
func submithonker(w http.ResponseWriter, r *http.Request) *Honker {
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
user, _ := butwhatabout(u.Username) user, _ := butwhatabout(u.Username)
name := strings.TrimSpace(r.FormValue("name")) name := strings.TrimSpace(r.FormValue("name"))
@ -1897,7 +1905,7 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
re_namecheck := regexp.MustCompile("[\\pL[:digit:]_.-]+") re_namecheck := regexp.MustCompile("[\\pL[:digit:]_.-]+")
if name != "" && !re_namecheck.MatchString(name) { if name != "" && !re_namecheck.MatchString(name) {
http.Error(w, "please use a plainer name", http.StatusInternalServerError) http.Error(w, "please use a plainer name", http.StatusInternalServerError)
return return nil
} }
var meta HonkerMeta var meta HonkerMeta
@ -1906,12 +1914,16 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
defer honkerinvalidator.Clear(u.UserID) defer honkerinvalidator.Clear(u.UserID)
// mostly dummy, fill in later...
h := &Honker{
ID: honkerid,
}
if honkerid > 0 { if honkerid > 0 {
if r.FormValue("delete") == "delete" { if r.FormValue("delete") == "delete" {
unfollowyou(user, honkerid) unfollowyou(user, honkerid)
stmtDeleteHonker.Exec(honkerid) stmtDeleteHonker.Exec(honkerid)
http.Redirect(w, r, "/honkers", http.StatusSeeOther) return h
return
} }
if r.FormValue("unsub") == "unsub" { if r.FormValue("unsub") == "unsub" {
unfollowyou(user, honkerid) unfollowyou(user, honkerid)
@ -1922,15 +1934,14 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
_, err := stmtUpdateHonker.Exec(name, combos, mj, honkerid, u.UserID) _, err := stmtUpdateHonker.Exec(name, combos, mj, honkerid, u.UserID)
if err != nil { if err != nil {
elog.Printf("update honker err: %s", err) elog.Printf("update honker err: %s", err)
return return nil
} }
http.Redirect(w, r, "/honkers", http.StatusSeeOther) return h
return
} }
if url == "" { if url == "" {
http.Error(w, "subscribing to nothing?", http.StatusInternalServerError) http.Error(w, "subscribing to nothing?", http.StatusInternalServerError)
return return nil
} }
flavor := "presub" flavor := "presub"
@ -1938,13 +1949,13 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
flavor = "peep" flavor = "peep"
} }
err := savehonker(user, url, name, flavor, combos, mj) id, err := savehonker(user, url, name, flavor, combos, mj)
if err != nil { if err != nil {
http.Error(w, "had some trouble with that: "+err.Error(), http.StatusInternalServerError) http.Error(w, "had some trouble with that: "+err.Error(), http.StatusInternalServerError)
return return nil
} }
h.ID = id
http.Redirect(w, r, "/honkers", http.StatusSeeOther) return h
} }
func hfcspage(w http.ResponseWriter, r *http.Request) { func hfcspage(w http.ResponseWriter, r *http.Request) {
@ -2311,7 +2322,7 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
if h == nil { if h == nil {
return return
} }
w.Write([]byte(h.XID)) fmt.Fprintf(w, "%s", h.XID)
case "donk": case "donk":
d, err := submitdonk(w, r) d, err := submitdonk(w, r)
if err != nil { if err != nil {
@ -2371,6 +2382,16 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
for rcpt := range rcpts { for rcpt := range rcpts {
go deliverate(0, userid, rcpt, msg, true) go deliverate(0, userid, rcpt, msg, true)
} }
case "gethonkers":
j := junk.New()
j["honkers"] = gethonkers(u.UserID)
j.Write(w)
case "savehonker":
h := submithonker(w, r)
if h == nil {
return
}
fmt.Fprintf(w, "%d", h.ID)
default: default:
http.Error(w, "unknown action", http.StatusNotFound) http.Error(w, "unknown action", http.StatusNotFound)
return return
@ -2560,7 +2581,7 @@ func serve() {
loggedin.HandleFunc("/xzone", xzone) loggedin.HandleFunc("/xzone", xzone)
loggedin.HandleFunc("/newhonk", newhonkpage) loggedin.HandleFunc("/newhonk", newhonkpage)
loggedin.HandleFunc("/edit", edithonkpage) loggedin.HandleFunc("/edit", edithonkpage)
loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitwebhonk))) loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(websubmithonk)))
loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk))) loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk)))
loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit))) loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit)))
loggedin.Handle("/savehfcs", login.CSRFWrap("filter", http.HandlerFunc(savehfcs))) loggedin.Handle("/savehfcs", login.CSRFWrap("filter", http.HandlerFunc(savehfcs)))
@ -2575,7 +2596,7 @@ func serve() {
loggedin.HandleFunc("/q", showsearch) loggedin.HandleFunc("/q", showsearch)
loggedin.HandleFunc("/hydra", webhydra) loggedin.HandleFunc("/hydra", webhydra)
loggedin.HandleFunc("/emus", showemus) loggedin.HandleFunc("/emus", showemus)
loggedin.Handle("/submithonker", login.CSRFWrap("submithonker", http.HandlerFunc(submithonker))) loggedin.Handle("/submithonker", login.CSRFWrap("submithonker", http.HandlerFunc(websubmithonker)))
err = http.Serve(listener, mux) err = http.Serve(listener, mux)
if err != nil { if err != nil {