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