all repos — honk @ 3762e5454acd3062b5c5d67e658143ac5f8be51e

my fork of honk

masto.go (view raw)

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