masto.go (view raw)
1package main
2
3import (
4 "crypto/rand"
5 "fmt"
6 "net/http"
7 "time"
8
9 "humungus.tedunangst.com/r/webs/junk"
10)
11
12func snowflake() uint64 {
13 ts := time.Now()
14 return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff))
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
22func goodjunk(rw http.ResponseWriter, j junk.Junk) {
23 rw.WriteHeader(http.StatusOK)
24 rw.Header().Set("Content-Type", "application/json; charset=utf-8")
25 rw.Write(j.ToBytes())
26}
27
28func showoauthlogin(rw http.ResponseWriter, r *http.Request) {
29 templinfo := make(map[string]interface{})
30 templinfo = getInfo(r)
31 if err := readviews.Execute(rw, "oauthlogin.html", templinfo); err != nil {
32 elog.Println(err)
33 }
34}
35
36// https://docs.joinmastodon.org/methods/apps/#create
37func apiapps(rw http.ResponseWriter, r *http.Request) {
38 if err := r.ParseForm(); err != nil {
39 http.Error(rw, "invalid input", http.StatusUnprocessableEntity)
40 elog.Println(err)
41 return
42 }
43 clientName := r.Form.Get("client_name")
44 redirectUri := r.Form.Get("redirect_uris")
45 scopes := r.Form.Get("scopes")
46 website := r.Form.Get("website")
47 clientID := tokengen()
48 clientSecret := tokengen()
49 vapidKey := tokengen()
50
51 _, err := stmtSaveMastoApp.Exec(clientName, redirectUri, scopes, clientID, clientSecret, vapidKey)
52 if err != nil {
53 elog.Printf("error saving masto app: %v", err)
54 http.Error(rw, "error saving masto app", http.StatusUnprocessableEntity)
55 return
56 }
57
58 j := junk.New()
59 j["id"] = fmt.Sprintf("%d", snowflake())
60 j["website"] = website
61 j["name"] = clientName
62 j["redirect_uri"] = redirectUri
63 j["client_id"] = clientID
64 j["client_secret"] = clientSecret
65 j["vapid_key"] = vapidKey
66
67 fmt.Println(j.ToString())
68 goodjunk(rw, j)
69}
70
71func tokengen() string {
72 b := make([]byte, 32)
73 rand.Read(b)
74 return fmt.Sprintf("%x", b)
75}
76
77// https://docs.joinmastodon.org/methods/oauth/#authorize
78func oauthorize(rw http.ResponseWriter, r *http.Request) {
79 http.Redirect(rw, r, "/login", 302)
80}
81
82// https://docs.joinmastodon.org/methods/instance/#v2
83func instance(rw http.ResponseWriter, r *http.Request) {
84 j := junk.New()
85
86 var servername string
87 if err := getconfig("servername", &servername); err != nil {
88 http.Error(rw, "getting servername", http.StatusInternalServerError)
89 return
90 }
91
92 j["uri"] = servername
93 j["title"] = "honk"
94 j["description"] = "federated honk conveyance"
95 j["version"] = "develop"
96
97 thumbnail := junk.New()
98 thumbnail["url"] = fmt.Sprintf("https://%s/icon.png", servername)
99 j["thumbnail"] = thumbnail
100 j["languages"] = []string{"en"}
101
102 config := junk.New()
103
104 a := junk.New()
105 a["max_featured_tags"] = 10
106 config["accounts"] = a
107
108 s := junk.New()
109 s["max_characters"] = 5000
110 s["max_media_attachments"] = 1
111 s["characters_reserved_per_url"] = 23
112 config["statuses"] = s
113
114 m := junk.New()
115 m["supported_mime_types"] = []string{
116 "image/jpeg",
117 "image/png",
118 "image/gif",
119 "image/heic",
120 "image/heif",
121 "image/webp",
122 "image/avif",
123 "video/webm",
124 "video/mp4",
125 "video/quicktime",
126 "video/ogg",
127 "audio/wave",
128 "audio/wav",
129 "audio/x-wav",
130 "audio/x-pn-wave",
131 "audio/vnd.wave",
132 "audio/ogg",
133 "audio/vorbis",
134 "audio/mpeg",
135 "audio/mp3",
136 "audio/webm",
137 "audio/flac",
138 "audio/aac",
139 "audio/m4a",
140 "audio/x-m4a",
141 "audio/mp4",
142 "audio/3gpp",
143 "video/x-ms-asf",
144 }
145
146 m["image_size_limit"] = 10485760
147 m["image_matrix_limit"] = 16777216
148 m["video_size_limit"] = 41943040
149 m["video_frame_rate_limit"] = 60
150 m["video_matrix_limit"] = 2304000
151 j["media_attachments"] = m
152
153 rw.WriteHeader(http.StatusOK)
154 rw.Write(j.ToBytes())
155
156 return
157}