all repos — honk @ 41c75993540b3f2bb739c6c986abb986e294ecaf

my fork of honk

masto.go (view raw)

  1package main
  2
  3import (
  4	"crypto/rand"
  5	"fmt"
  6	"net/http"
  7	"time"
  8
  9	"humungus.tedunangst.com/r/webs/junk"
 10)
 11
 12func snowflake() uint64 {
 13	ts := time.Now()
 14	return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff))
 15}
 16
 17func badjunk(rw http.ResponseWriter, j junk.Junk, path string) {
 18	elog.Printf("%s: bad junk: %s", path, j.ToString())
 19	http.Error(rw, fmt.Sprintf("%s: bad junk", path), http.StatusBadRequest)
 20}
 21
 22// https://docs.joinmastodon.org/methods/apps/#create
 23func apiapps(rw http.ResponseWriter, r *http.Request) {
 24	dlog.Println(r.URL.String())
 25	if err := r.ParseForm(); err != nil {
 26		http.Error(rw, "invalid input", http.StatusUnprocessableEntity)
 27		elog.Println(err)
 28		return
 29	}
 30	clientName := r.Form.Get("client_name")
 31	redirectUri := r.Form.Get("redirect_uris")
 32	scopes := r.Form.Get("scopes")
 33	clientID := tokengen()
 34	clientSecret := tokengen()
 35	vapidKey := tokengen()
 36
 37	_, err := stmtSaveMastoApp.Exec(clientName, redirectUri, scopes, clientID, clientSecret, vapidKey)
 38	if err != nil {
 39		elog.Printf("error saving masto app: %v", err)
 40		http.Error(rw, "error saving masto app", http.StatusUnprocessableEntity)
 41		return
 42	}
 43
 44	j := junk.New()
 45	j["id"] = snowflake()
 46	j["name"] = clientName
 47	j["redirect_uri"] = redirectUri
 48	j["client_id"] = clientID
 49	j["client_secret"] = clientSecret
 50	j["vapid_key"] = vapidKey
 51
 52	fmt.Println(j.ToString())
 53
 54	rw.WriteHeader(http.StatusOK)
 55	rw.Write(j.ToBytes())
 56}
 57
 58func tokengen() string {
 59	b := make([]byte, 32)
 60	rand.Read(b)
 61	return fmt.Sprintf("%x", b)
 62}
 63
 64// https://docs.joinmastodon.org/methods/oauth/#authorize
 65func oauthorize(rw http.ResponseWriter, r *http.Request) {
 66	dlog.Println("oauthorizing!")
 67	dlog.Println(r.URL.String)
 68}
 69
 70// https://docs.joinmastodon.org/methods/instance/#v2
 71func instance(rw http.ResponseWriter, r *http.Request) {
 72	dlog.Println(r.URL.String)
 73	j := junk.New()
 74
 75	var servername string
 76	if err := getconfig("servername", &servername); err != nil {
 77		http.Error(rw, "getting servername", http.StatusInternalServerError)
 78		return
 79	}
 80
 81	j["uri"] = servername
 82	j["title"] = "honk"
 83	j["description"] = "federated honk conveyance"
 84	j["version"] = "develop"
 85
 86	thumbnail := junk.New()
 87	thumbnail["url"] = fmt.Sprintf("https://%s/icon.png", servername)
 88	j["thumbnail"] = thumbnail
 89	j["languages"] = []string{"en"}
 90
 91	config := junk.New()
 92
 93	a := junk.New()
 94	a["max_featured_tags"] = 10
 95	config["accounts"] = a
 96
 97	s := junk.New()
 98	s["max_characters"] = 5000
 99	s["max_media_attachments"] = 1
100	s["characters_reserved_per_url"] = 23
101	config["statuses"] = s
102
103	m := junk.New()
104	m["supported_mime_types"] = []string{
105		"image/jpeg",
106		"image/png",
107		"image/gif",
108		"image/heic",
109		"image/heif",
110		"image/webp",
111		"image/avif",
112		"video/webm",
113		"video/mp4",
114		"video/quicktime",
115		"video/ogg",
116		"audio/wave",
117		"audio/wav",
118		"audio/x-wav",
119		"audio/x-pn-wave",
120		"audio/vnd.wave",
121		"audio/ogg",
122		"audio/vorbis",
123		"audio/mpeg",
124		"audio/mp3",
125		"audio/webm",
126		"audio/flac",
127		"audio/aac",
128		"audio/m4a",
129		"audio/x-m4a",
130		"audio/mp4",
131		"audio/3gpp",
132		"video/x-ms-asf",
133	}
134
135	m["image_size_limit"] = 10485760
136	m["image_matrix_limit"] = 16777216
137	m["video_size_limit"] = 41943040
138	m["video_frame_rate_limit"] = 60
139	m["video_matrix_limit"] = 2304000
140	j["media_attachments"] = m
141
142	err := j.Write(rw)
143	if err != nil {
144		http.Error(rw, "writing json", http.StatusInternalServerError)
145	}
146	return
147}