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