From fb410079059f4fa945e2c914ce567486b33aed60 Mon Sep 17 00:00:00 2001 From: Will Andrews Date: Fri, 17 Jan 2025 18:11:01 +0000 Subject: [PATCH] get sub uri so we can delete posts --- feedgenerator.go | 5 +++++ frontend_handlers.go | 16 ++++++++-------- server.go | 1 + store/subscription.go | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/feedgenerator.go b/feedgenerator.go index 2b4fc18..1ee466e 100644 --- a/feedgenerator.go +++ b/feedgenerator.go @@ -14,6 +14,7 @@ type feedStore interface { GetSubscriptionsForUser(ctx context.Context, userDID string) ([]store.Subscription, error) DeleteSubscriptionBySubRKeyAndUser(userDID, rkey string) error DeleteFeedPostsForSubscribedPostURIandUserDID(subscribedPostURI, userDID string) error + GetSubscriptionURIByRKeyAndUserDID(userDID, rkey string) (string, error) } type FeedGenerator struct { @@ -74,3 +75,7 @@ func (f *FeedGenerator) DeleteSubscriptionBySubRKeyAndUser(userDID, rkey string) func (f *FeedGenerator) DeleteFeedPostsForSubscribedPostURIandUserDID(subscribedPostURI, userDID string) error { return f.store.DeleteFeedPostsForSubscribedPostURIandUserDID(subscribedPostURI, userDID) } + +func (f *FeedGenerator) GetSubscriptionURIByRKeyAndUserDID(userDID, rkey string) (string, error) { + return f.GetSubscriptionURIByRKeyAndUserDID(userDID, rkey) +} diff --git a/frontend_handlers.go b/frontend_handlers.go index 38027a2..241179d 100644 --- a/frontend_handlers.go +++ b/frontend_handlers.go @@ -96,14 +96,14 @@ func (s *Server) HandleDeleteSubscription(w http.ResponseWriter, r *http.Request usersDid := didCookie.Value - // id, err := strconv.Atoi(sub) - // if err != nil { - // slog.Error("failed to convert sub ID to int", "error", err) - // http.Error(w, "invalid ID", http.StatusBadRequest) - // return - // } - - err = s.feeder.DeleteFeedPostsForSubscribedPostURIandUserDID(subRKey, usersDid) + subURI, err := s.feeder.GetSubscriptionURIByRKeyAndUserDID(usersDid, subRKey) + if err != nil { + slog.Error("get sub URI by rkey and user did", "error", err, "subscription URI", subRKey) + http.Error(w, "failed to delete feed posts for subscription and user", http.StatusInternalServerError) + return + } + + err = s.feeder.DeleteFeedPostsForSubscribedPostURIandUserDID(subURI, usersDid) if err != nil { slog.Error("delete feed posts for subscription and user", "error", err, "subscription URI", subRKey) http.Error(w, "failed to delete feed posts for subscription and user", http.StatusInternalServerError) diff --git a/server.go b/server.go index d6ff0ec..91cf4fd 100644 --- a/server.go +++ b/server.go @@ -15,6 +15,7 @@ type Feeder interface { GetSubscriptionsForUser(ctx context.Context, userDID string) ([]store.Subscription, error) DeleteSubscriptionBySubRKeyAndUser(userDID, rkey string) error DeleteFeedPostsForSubscribedPostURIandUserDID(subscribedPostURI, userDID string) error + GetSubscriptionURIByRKeyAndUserDID(userDID, rkey string) (string, error) } type Server struct { diff --git a/store/subscription.go b/store/subscription.go index 66b20df..749cb8c 100644 --- a/store/subscription.go +++ b/store/subscription.go @@ -124,3 +124,22 @@ func (s *Store) DeleteSubscriptionBySubRKeyAndUser(userDID, rkey string) error { } return nil } + +func (s *Store) GetSubscriptionURIByRKeyAndUserDID(userDID, rkey string) (string, error) { + sql := "SELECT subscribedPostURI FROM subscriptions WHERE subscriptionPostRkey = ? AND userDID = ?;" + rows, err := s.db.Query(sql, userDID) + if err != nil { + return "", fmt.Errorf("run query to get subscribed by rkey and userDID: %w", err) + } + defer rows.Close() + + var result string + for rows.Next() { + if err := rows.Scan(&result); err != nil { + return "", fmt.Errorf("scan row: %w", err) + } + + return result, nil + } + return "", nil +} -- 2.51.2