add a raw sendactivity API action

This commit is contained in:
Ted Unangst 2019-11-26 14:57:23 -05:00
parent f76a5d6113
commit 3014826f6f
7 changed files with 108 additions and 10 deletions

View File

@ -1186,24 +1186,34 @@ func gimmejonk(xid string) ([]byte, bool) {
return j, ok
}
func honkworldwide(user *WhatAbout, honk *Honk) {
jonk, _ := jonkjonk(user, honk)
jonk["@context"] = itiswhatitis
msg := jonk.ToBytes()
func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
rcpts := make(map[string]bool)
for _, a := range honk.Audience {
if a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
for _, a := range addresses {
if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
continue
}
if a[0] == '%' {
rcpts[a] = true
continue
}
var box *Box
ok := boxofboxes.Get(a, &box)
if ok && honk.Public && box.Shared != "" {
if ok && useshared && box.Shared != "" {
rcpts["%"+box.Shared] = true
} else {
rcpts[a] = true
}
}
return rcpts
}
func honkworldwide(user *WhatAbout, honk *Honk) {
jonk, _ := jonkjonk(user, honk)
jonk["@context"] = itiswhatitis
msg := jonk.ToBytes()
rcpts := boxuprcpts(user, honk.Audience, honk.Public)
if honk.Public {
for _, h := range getdubs(user.ID) {
if h.XID == user.URL {

View File

@ -2,6 +2,8 @@ changelog
-- next
+ A raw sendactivity API action for the bold.
+ More flexible meme names.
-- 0.8.5 Turnkey Blaster

View File

@ -109,6 +109,20 @@ If there are no results, wait this many seconds for something to appear.
.El
.Pp
The result will be returned as json.
.Ss sendactivity
Send anything.
No limits, no error checking.
.Bl -tag -width public
.It Fa rcpt
An actor to deliver the message to to.
May be specified more than once.
An inbox may be specified directly by prefixing with %.
.It Fa msg
The message.
It should be a valid json activity, but yolo.
.It Fa public
Set to 1 to use shared inboxes for delivery.
.El
.Sh EXAMPLES
Refer to the sample code in the
.Pa toys

View File

@ -1,5 +1,5 @@
all: gettoken saytheday youvegothonks
all: gettoken saytheday sprayandpray youvegothonks
gettoken: gettoken.go
go build gettoken.go
@ -7,5 +7,8 @@ gettoken: gettoken.go
saytheday: saytheday.go
go build saytheday.go
sprayandpray: sprayandpray.go
go build sprayandpray.go
youvegothonks: youvegothonks.go
go build youvegothonks.go

View File

@ -4,6 +4,8 @@ A little of this, a little of that.
gettoken.go - obtains an authorization token
saytheday.go - posts a new honk
saytheday.go - posts a new honk that's a date based look and say sequence
sprayandpray.go - send an activity with no error checking and hope it works
youvegothonks.go - polls for new mesages

59
toys/sprayandpray.go Normal file
View File

@ -0,0 +1,59 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
func sendmsg(server, token, msg, rcpt string) {
form := make(url.Values)
form.Add("token", token)
form.Add("action", "sendactivity")
form.Add("msg", msg)
form.Add("rcpt", rcpt)
apiurl := fmt.Sprintf("https://%s/api", server)
req, err := http.NewRequest("POST", apiurl, strings.NewReader(form.Encode()))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
answer, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatalf("status: %d: %s", resp.StatusCode, answer)
}
}
func main() {
var server, token, msgfile, rcpt string
flag.StringVar(&server, "server", server, "server to connnect")
flag.StringVar(&token, "token", token, "auth token to use")
flag.StringVar(&msgfile, "msgfile", token, "file with message to send")
flag.StringVar(&rcpt, "rcpt", rcpt, "rcpt to send it to")
flag.Parse()
if server == "" || token == "" || msgfile == "" || rcpt == "" {
flag.Usage()
os.Exit(1)
}
msg, err := ioutil.ReadFile(msgfile)
if err != nil {
log.Fatal(err)
}
sendmsg(server, token, string(msg), rcpt)
}

8
web.go
View File

@ -2138,6 +2138,7 @@ func honkhonkline() {
func apihandler(w http.ResponseWriter, r *http.Request) {
u := login.GetUserInfo(r)
userid := u.UserID
user, _ := butwhatabout(u.Username)
action := r.FormValue("action")
wait, _ := strconv.ParseInt(r.FormValue("wait"), 10, 0)
log.Printf("api request '%s' on behalf of %s", action, u.Username)
@ -2175,6 +2176,13 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
j := junk.New()
j["honks"] = honks
j.Write(w)
case "sendactivity":
public := r.FormValue("public") == "1"
rcpts := boxuprcpts(user, r.Form["rcpt"], public)
msg := []byte(r.FormValue("msg"))
for rcpt := range rcpts {
go deliverate(0, userid, rcpt, msg)
}
default:
http.Error(w, "unknown action", http.StatusNotFound)
return