From e08c21228c0314d32fbdc73dca00437f9294ee84 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 19 Feb 2023 17:46:59 -0500 Subject: [PATCH] API access for honkers. --- database.go | 10 +++++----- docs/honk.3 | 21 +++++++++++++++++++++ import.go | 2 +- web.go | 53 +++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/database.go b/database.go index 9e5231e..40e2768 100644 --- a/database.go +++ b/database.go @@ -962,7 +962,7 @@ func savexonker(what, value, flav, when string) { 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 if url[0] == '#' { flavor = "peep" @@ -974,7 +974,7 @@ func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error { info, err := investigate(url) if err != nil { ilog.Printf("failed to investigate honker: %s", err) - return err + return 0, err } url = info.XID if name == "" { @@ -993,19 +993,19 @@ func savehonker(user *WhatAbout, url, name, flavor, combos, mj string) error { } else { 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) if err != nil { elog.Print(err) - return err + return 0, err } honkerid, _ := res.LastInsertId() if flavor == "presub" { followyou(user, honkerid) } - return nil + return honkerid, nil } func cleanupdb(arg string) { diff --git a/docs/honk.3 b/docs/honk.3 index 5144ec1..7997ef2 100644 --- a/docs/honk.3 +++ b/docs/honk.3 @@ -158,6 +158,27 @@ Delete this honk. Mute this thread. What should identify a convoy. .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 Send anything. No limits, no error checking. diff --git a/import.go b/import.go index f455837..88db67b 100644 --- a/import.go +++ b/import.go @@ -210,7 +210,7 @@ func importMastotooters(user *WhatAbout, source string) { name := "" flavor := "peep" combos := "" - err := savehonker(user, url, name, flavor, combos, mj) + _, err := savehonker(user, url, name, flavor, combos, mj) if err != nil { elog.Printf("trouble with a honker: %s", err) } diff --git a/web.go b/web.go index 4d0ade8..9c00847 100644 --- a/web.go +++ b/web.go @@ -1563,7 +1563,7 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) { return d, nil } -func submitwebhonk(w http.ResponseWriter, r *http.Request) { +func websubmithonk(w http.ResponseWriter, r *http.Request) { h := submithonk(w, r) if h == nil { 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) user, _ := butwhatabout(u.Username) name := strings.TrimSpace(r.FormValue("name")) @@ -1897,7 +1905,7 @@ func submithonker(w http.ResponseWriter, r *http.Request) { re_namecheck := regexp.MustCompile("[\\pL[:digit:]_.-]+") if name != "" && !re_namecheck.MatchString(name) { http.Error(w, "please use a plainer name", http.StatusInternalServerError) - return + return nil } var meta HonkerMeta @@ -1906,12 +1914,16 @@ func submithonker(w http.ResponseWriter, r *http.Request) { defer honkerinvalidator.Clear(u.UserID) + // mostly dummy, fill in later... + h := &Honker{ + ID: honkerid, + } + if honkerid > 0 { if r.FormValue("delete") == "delete" { unfollowyou(user, honkerid) stmtDeleteHonker.Exec(honkerid) - http.Redirect(w, r, "/honkers", http.StatusSeeOther) - return + return h } if r.FormValue("unsub") == "unsub" { unfollowyou(user, honkerid) @@ -1922,15 +1934,14 @@ func submithonker(w http.ResponseWriter, r *http.Request) { _, err := stmtUpdateHonker.Exec(name, combos, mj, honkerid, u.UserID) if err != nil { elog.Printf("update honker err: %s", err) - return + return nil } - http.Redirect(w, r, "/honkers", http.StatusSeeOther) - return + return h } if url == "" { http.Error(w, "subscribing to nothing?", http.StatusInternalServerError) - return + return nil } flavor := "presub" @@ -1938,13 +1949,13 @@ func submithonker(w http.ResponseWriter, r *http.Request) { flavor = "peep" } - err := savehonker(user, url, name, flavor, combos, mj) + id, err := savehonker(user, url, name, flavor, combos, mj) if err != nil { http.Error(w, "had some trouble with that: "+err.Error(), http.StatusInternalServerError) - return + return nil } - - http.Redirect(w, r, "/honkers", http.StatusSeeOther) + h.ID = id + return h } func hfcspage(w http.ResponseWriter, r *http.Request) { @@ -2311,7 +2322,7 @@ func apihandler(w http.ResponseWriter, r *http.Request) { if h == nil { return } - w.Write([]byte(h.XID)) + fmt.Fprintf(w, "%s", h.XID) case "donk": d, err := submitdonk(w, r) if err != nil { @@ -2371,6 +2382,16 @@ func apihandler(w http.ResponseWriter, r *http.Request) { for rcpt := range rcpts { 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: http.Error(w, "unknown action", http.StatusNotFound) return @@ -2560,7 +2581,7 @@ func serve() { loggedin.HandleFunc("/xzone", xzone) loggedin.HandleFunc("/newhonk", newhonkpage) 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("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit))) loggedin.Handle("/savehfcs", login.CSRFWrap("filter", http.HandlerFunc(savehfcs))) @@ -2575,7 +2596,7 @@ func serve() { loggedin.HandleFunc("/q", showsearch) loggedin.HandleFunc("/hydra", webhydra) 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) if err != nil {