all repos — honk @ 9863222e3d1eca86243a5e9e45970ea0fff4b939

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
 22func goodjunk(rw http.ResponseWriter, j junk.Junk) {
 23	rw.WriteHeader(http.StatusOK)
 24	rw.Header().Set("Content-Type", "application/json; charset=utf-8")
 25	rw.Write(j.ToBytes())
 26}
 27
 28func showoauthlogin(rw http.ResponseWriter, r *http.Request) {
 29	if err := readviews.Execute(rw, "oauthlogin.html", ""); err != nil {
 30		elog.Println(err)
 31	}
 32}
 33
 34// https://docs.joinmastodon.org/methods/apps/#create
 35func apiapps(rw http.ResponseWriter, r *http.Request) {
 36	if err := r.ParseForm(); err != nil {
 37		http.Error(rw, "invalid input", http.StatusUnprocessableEntity)
 38		elog.Println(err)
 39		return
 40	}
 41	clientName := r.Form.Get("client_name")
 42	redirectUri := r.Form.Get("redirect_uris")
 43	scopes := r.Form.Get("scopes")
 44	website := r.Form.Get("website")
 45	clientID := tokengen()
 46	clientSecret := tokengen()
 47	vapidKey := tokengen()
 48
 49	_, err := stmtSaveMastoApp.Exec(clientName, redirectUri, scopes, clientID, clientSecret, vapidKey)
 50	if err != nil {
 51		elog.Printf("error saving masto app: %v", err)
 52		http.Error(rw, "error saving masto app", http.StatusUnprocessableEntity)
 53		return
 54	}
 55
 56	j := junk.New()
 57	j["id"] = fmt.Sprintf("%d", snowflake())
 58	j["website"] = website
 59	j["name"] = clientName
 60	j["redirect_uri"] = redirectUri
 61	j["client_id"] = clientID
 62	j["client_secret"] = clientSecret
 63	j["vapid_key"] = vapidKey
 64
 65	fmt.Println(j.ToString())
 66	goodjunk(rw, j)
 67}
 68
 69func tokengen() string {
 70	b := make([]byte, 32)
 71	rand.Read(b)
 72	return fmt.Sprintf("%x", b)
 73}
 74
 75// https://docs.joinmastodon.org/methods/oauth/#authorize
 76func oauthorize(rw http.ResponseWriter, r *http.Request) {
 77	http.Redirect(rw, r, "/login", 302)
 78}
 79
 80// https://docs.joinmastodon.org/methods/instance/#v2
 81func instance(rw http.ResponseWriter, r *http.Request) {
 82	j := junk.New()
 83
 84	var servername string
 85	if err := getconfig("servername", &servername); err != nil {
 86		http.Error(rw, "getting servername", http.StatusInternalServerError)
 87		return
 88	}
 89
 90	j["uri"] = servername
 91	j["title"] = "honk"
 92	j["description"] = "federated honk conveyance"
 93	j["version"] = "develop"
 94
 95	thumbnail := junk.New()
 96	thumbnail["url"] = fmt.Sprintf("https://%s/icon.png", servername)
 97	j["thumbnail"] = thumbnail
 98	j["languages"] = []string{"en"}
 99
100	config := junk.New()
101
102	a := junk.New()
103	a["max_featured_tags"] = 10
104	config["accounts"] = a
105
106	s := junk.New()
107	s["max_characters"] = 5000
108	s["max_media_attachments"] = 1
109	s["characters_reserved_per_url"] = 23
110	config["statuses"] = s
111
112	m := junk.New()
113	m["supported_mime_types"] = []string{
114		"image/jpeg",
115		"image/png",
116		"image/gif",
117		"image/heic",
118		"image/heif",
119		"image/webp",
120		"image/avif",
121		"video/webm",
122		"video/mp4",
123		"video/quicktime",
124		"video/ogg",
125		"audio/wave",
126		"audio/wav",
127		"audio/x-wav",
128		"audio/x-pn-wave",
129		"audio/vnd.wave",
130		"audio/ogg",
131		"audio/vorbis",
132		"audio/mpeg",
133		"audio/mp3",
134		"audio/webm",
135		"audio/flac",
136		"audio/aac",
137		"audio/m4a",
138		"audio/x-m4a",
139		"audio/mp4",
140		"audio/3gpp",
141		"video/x-ms-asf",
142	}
143
144	m["image_size_limit"] = 10485760
145	m["image_matrix_limit"] = 16777216
146	m["video_size_limit"] = 41943040
147	m["video_frame_rate_limit"] = 60
148	m["video_matrix_limit"] = 2304000
149	j["media_attachments"] = m
150
151	rw.WriteHeader(http.StatusOK)
152	rw.Write(j.ToBytes())
153
154	return
155}