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