tiny 88x31 lexicon for atproto

favi + beep

+38 -35
+18 -8
db/lexicon.go
··· 206 206 return err 207 207 } 208 208 209 - func (s *Store) GetLike(uri string, ctx context.Context) (string, error) { 210 - row := s.pool.QueryRow(ctx, ` 211 - SELECT subject_uri FROM likes WHERE uri = $1 212 - `, uri) 209 + func (s *Store) GetMyLikesFor(buttonuri string, buttoncid string, mydid string, ctx context.Context) ([]string, error) { 210 + rows, err := s.pool.Query(ctx, ` 211 + SELECT subject_uri FROM likes WHERE subject_uri = $1 AND subject_cid = $2 AND did = $3 212 + `, buttonuri, buttoncid, mydid) 213 + if err != nil { 214 + return nil, err 215 + } 216 + var resuris []string = make([]string, 0) 213 217 var resuri string 214 - err := row.Scan(&resuri) 218 + for rows.Next() { 219 + err = rows.Scan(&resuri) 220 + if err != nil { 221 + return nil, err 222 + } 223 + resuris = append(resuris, resuri) 224 + } 215 225 if err != nil { 216 - err = errors.New(fmt.Sprintf("error scanning like subject: %s; uri %s", err.Error(), uri)) 217 - return "", err 226 + err = errors.New(fmt.Sprintf("error scanning like subject: %s; uri %s", err.Error(), buttonuri)) 227 + return nil, err 218 228 } 219 - return resuri, nil 229 + return resuris, nil 220 230 } 221 231 222 232 func (s *Store) DeleteLike(uri string, ctx context.Context) error {
+3
handler/handler.go
··· 44 44 mux.HandleFunc(oauthCallbackPath(), h.oauthCallback) 45 45 mux.HandleFunc(clientMetadataPath(), h.WithCORS(h.serveClientMetadata)) 46 46 mux.HandleFunc(oauthJWKSPath(), h.WithCORS(h.serveJWKS)) 47 + mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { 48 + http.ServeFile(w, r, "./static/favicon.ico") 49 + }) 47 50 return h 48 51 } 49 52
+17 -27
handler/upload.go
··· 63 63 } 64 64 65 65 func (h *Handler) unlike(cs *oauth.ClientSession, w http.ResponseWriter, r *http.Request) { 66 - log.Println("unliking") 67 66 if cs == nil { 68 67 http.Error(w, "unlike requires auth", http.StatusUnauthorized) 69 68 return 70 69 } 71 - log.Println("has auth") 72 70 err := r.ParseForm() 73 71 if err != nil { 74 72 http.Error(w, "form failed to parse", http.StatusBadRequest) 75 73 return 76 74 } 77 - log.Println("form parsed") 78 75 uri := r.FormValue("uri") 79 76 if uri == "" { 80 77 http.Error(w, "must provide uri, don't tamper with form", http.StatusBadRequest) 81 78 return 82 79 } 83 - log.Println("has uri") 84 80 cid := r.FormValue("cid") 85 81 if cid == "" { 86 82 http.Error(w, "must provide a cid, don't tamper with form", http.StatusBadRequest) 87 83 return 88 84 } 89 - log.Println("has cid") 90 85 91 - aturi, err := syntax.ParseATURI(uri) 92 - if err != nil { 93 - http.Error(w, "uri doesn't parse, don't tamper with form", http.StatusBadRequest) 94 - return 95 - } 96 - log.Println("uri parsed") 97 - var cached = true 98 - l, err := h.db.GetLike(uri, r.Context()) 86 + ll, err := h.db.GetMyLikesFor(uri, cid, cs.Data.AccountDID.String(), r.Context()) 99 87 if err != nil { 100 88 log.Println(err) 101 - cached = false 102 - } 103 - err = myoauth.DeleteLike(cs, aturi.RecordKey().String(), cid, r.Context()) 104 - if err != nil { 105 - log.Println(err) 106 - http.Error(w, "error deleting like", http.StatusInternalServerError) 89 + http.Redirect(w, r, fmt.Sprintf("/button?uri=%s", uri), http.StatusCreated) 107 90 return 108 91 } 109 - log.Println("deleted like") 110 - if cached { 111 - err = h.db.DeleteLike(uri, r.Context()) 92 + for _, likeuri := range ll { 93 + aturi, err := syntax.ParseATURI(likeuri) 94 + if err != nil { 95 + http.Error(w, "uri doesn't parse, don't tamper with form", http.StatusBadRequest) 96 + return 97 + } 98 + err = myoauth.DeleteLike(cs, aturi.RecordKey().String(), cid, r.Context()) 112 99 if err != nil { 113 100 log.Println(err) 114 - http.Error(w, "error storing like", http.StatusInternalServerError) 101 + http.Error(w, "error deleting like", http.StatusInternalServerError) 115 102 return 116 103 } 117 - log.Println("cached deleted like") 118 - http.Redirect(w, r, fmt.Sprintf("/button?uri=%s", l), http.StatusCreated) 119 - } else { 120 - http.Redirect(w, r, "/", http.StatusCreated) 104 + err = h.db.DeleteLike(likeuri, r.Context()) 105 + if err != nil { 106 + log.Println(err) 107 + http.Error(w, "error deleting cached like", http.StatusInternalServerError) 108 + return 109 + } 121 110 } 111 + http.Redirect(w, r, fmt.Sprintf("/button?uri=%s", uri), http.StatusCreated) 122 112 } 123 113 124 114 func (h *Handler) upload(cs *oauth.ClientSession, w http.ResponseWriter, r *http.Request) {
static/favicon.ico

This is a binary file and will not be displayed.