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