all repos — honk @ 55c201ebb683f091777c421f03294b50ffed8b95

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