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