tangled
alpha
login
or
join now
moth11.net
/
88x31
0
fork
atom
tiny 88x31 lexicon for atproto
0
fork
atom
overview
issues
pulls
pipelines
lets see if this is any good
moth11.net
1 month ago
2088e1c6
6bcfba6e
+95
-1
5 changed files
expand all
collapse all
unified
split
db
lexicon.go
handler
handler.go
upload.go
oauth
client.go
tmpl
button.html
+7
db/lexicon.go
···
236
236
`, uri)
237
237
return err
238
238
}
239
239
+
240
240
+
func (s *Store) DeleteButton(uri string, ctx context.Context) error {
241
241
+
_, err := s.pool.Exec(ctx, `
242
242
+
DELETE FROM buttons WHERE uri = $1
243
243
+
`, uri)
244
244
+
return err
245
245
+
}
+1
handler/handler.go
···
45
45
mux.HandleFunc("POST /logout", h.oauthMiddleware(h.logout))
46
46
mux.HandleFunc("GET /upload", h.oauthMiddleware(getupload))
47
47
mux.HandleFunc("POST /upload", h.oauthMiddleware(h.upload))
48
48
+
mux.HandleFunc("POST /delete", h.oauthMiddleware(h.delete))
48
49
mux.HandleFunc("POST /like", h.oauthMiddleware(h.like))
49
50
mux.HandleFunc("POST /unlike", h.oauthMiddleware(h.unlike))
50
51
mux.HandleFunc("GET /button", h.oauthMiddleware(h.getbutton))
+69
handler/upload.go
···
118
118
http.Redirect(w, r, fmt.Sprintf("/button?uri=%s", uri), http.StatusSeeOther)
119
119
}
120
120
121
121
+
func (h *Handler) delete(cs *oauth.ClientSession, w http.ResponseWriter, r *http.Request) {
122
122
+
if cs == nil {
123
123
+
http.Error(w, "deletion requires auth", http.StatusUnauthorized)
124
124
+
return
125
125
+
}
126
126
+
err := r.ParseForm()
127
127
+
if err != nil {
128
128
+
http.Error(w, "form failed to parse", http.StatusBadRequest)
129
129
+
return
130
130
+
}
131
131
+
uri := r.FormValue("uri")
132
132
+
if uri == "" {
133
133
+
http.Error(w, "must provide uri, don't tamper with form", http.StatusBadRequest)
134
134
+
return
135
135
+
}
136
136
+
aturi, err := syntax.ParseATURI(uri)
137
137
+
if err != nil {
138
138
+
http.Error(w, "uri should parse", http.StatusBadRequest)
139
139
+
return
140
140
+
}
141
141
+
cid := r.FormValue("cid")
142
142
+
if cid == "" {
143
143
+
http.Error(w, "must provide a cid, don't tamper with form", http.StatusBadRequest)
144
144
+
return
145
145
+
}
146
146
+
ll, err := h.db.GetMyLikesFor(uri, cid, cs.Data.AccountDID.String(), r.Context())
147
147
+
if err == nil {
148
148
+
for _, likeuricid := range ll {
149
149
+
arr := strings.Split(likeuricid, " ")
150
150
+
if len(arr) != 2 {
151
151
+
http.Error(w, "invalid likeuricid", http.StatusInternalServerError)
152
152
+
return
153
153
+
}
154
154
+
likeuri := arr[0]
155
155
+
likecid := arr[1]
156
156
+
aturi, err := syntax.ParseATURI(likeuri)
157
157
+
if err != nil {
158
158
+
http.Error(w, "uri doesn't parse, don't tamper with form", http.StatusBadRequest)
159
159
+
return
160
160
+
}
161
161
+
err = myoauth.DeleteLike(cs, aturi.RecordKey().String(), likecid, r.Context())
162
162
+
if err != nil {
163
163
+
log.Println(err)
164
164
+
http.Error(w, "error deleting like", http.StatusInternalServerError)
165
165
+
return
166
166
+
}
167
167
+
err = h.db.DeleteLike(likeuri, r.Context())
168
168
+
if err != nil {
169
169
+
log.Println(err)
170
170
+
http.Error(w, "error deleting cached like", http.StatusInternalServerError)
171
171
+
return
172
172
+
}
173
173
+
}
174
174
+
}
175
175
+
err = myoauth.DeleteButton(cs, aturi.RecordKey().String(), cid, r.Context())
176
176
+
if err != nil {
177
177
+
log.Println(err)
178
178
+
http.Error(w, "failed to delete button", http.StatusInternalServerError)
179
179
+
return
180
180
+
}
181
181
+
err = h.db.DeleteButton(uri, r.Context())
182
182
+
if err != nil {
183
183
+
log.Println(err)
184
184
+
http.Error(w, "error deleting cached button", http.StatusInternalServerError)
185
185
+
return
186
186
+
}
187
187
+
http.Redirect(w, r, "/", http.StatusSeeOther)
188
188
+
}
189
189
+
121
190
func (h *Handler) upload(cs *oauth.ClientSession, w http.ResponseWriter, r *http.Request) {
122
191
if cs == nil {
123
192
http.Error(w, "upload requires auth", http.StatusUnauthorized)
+17
oauth/client.go
···
67
67
return
68
68
}
69
69
70
70
+
func DeleteButton(cs *oauth.ClientSession, rkey string, cid string, ctx context.Context) error {
71
71
+
c := cs.APIClient()
72
72
+
body := map[string]any{
73
73
+
"collection": "store.88x31.button",
74
74
+
"repo": *c.AccountDID,
75
75
+
"rkey": rkey,
76
76
+
"swapRecord": cid,
77
77
+
}
78
78
+
var out atproto.RepoDeleteRecord_Output
79
79
+
err := c.Post(ctx, "com.atproto.repo.deleteRecord", body, &out)
80
80
+
if err != nil {
81
81
+
err = errors.New("oops! failed to delete a button: " + err.Error())
82
82
+
return err
83
83
+
}
84
84
+
return nil
85
85
+
}
86
86
+
70
87
func CreateLike(cs *oauth.ClientSession, like *lex.LikeRecord, ctx context.Context) (uri string, cid string, err error) {
71
88
c := cs.APIClient()
72
89
body := map[string]any{
+1
-1
tmpl/button.html
···
15
15
</form>
16
16
{{end}}
17
17
{{if eq .Button.DID (deref .DID)}}
18
18
-
<form action="/unlike" method="POST">
18
18
+
<form action="/delete" method="POST">
19
19
<input type="text" name="uri" value="{{.Button.URI}}" hidden />
20
20
<input type="text" name="cid" value="{{.Button.CID}}" hidden />
21
21
<input type="submit" value='delete (permanent, one click)'/>