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