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