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