···103103 // look for posts where I've "subsribed" so that we can add the parent URI to a list of replies to that parent to look for
104104 if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" {
105105 slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", parentURI)
106106- return h.addDidToSubscribedParent(parentURI, event.Did)
106106+ return h.addDidToSubscribedParent(parentURI, event.Did, event.Commit.RKey)
107107 }
108108109109 // see if the post is a reply to a post we are subscribed to
···133133 slog.Info("delete event received", "post", fmt.Sprintf("%+v", post))
134134 }
135135136136- // we only care about posts that have parents which are replies
137137- if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" {
138138- return nil
139139- }
140140-141141- parentURI := post.Reply.Parent.Uri
142142-143143- // delete from subscriptions for the parentURI and the users DID
144144- err := deleteFeedItemsForParentURIandUserDID(h.db, parentURI, event.Did)
136136+ parentURI, err := getSubscribingPostParentURI(h.db, event.Did, event.Commit.RKey)
145137 if err != nil {
146146- slog.Error("delete feed items for parentURI and user", "error", err, "parentURI", parentURI, "user DID", event.Did)
147147- return fmt.Errorf("delete feed items for parentURI and user: %w", err)
138138+ slog.Error("get subscribing post parent URI", "error", err, "rkey", event.Commit.RKey, "user DID", event.Did)
139139+ return fmt.Errorf("get subscribing post parent URI: %w", err)
148140 }
149141150150- // delete from feeds for the parentURI and the users DID
142142+ // delete from feeds for the parentURI and the users DID first. This is so that if this fails, it can be tried again and the
143143+ // subscription will be still there
151144 err = deleteSubscriptionForUser(h.db, event.Did, parentURI)
152145 if err != nil {
153146 slog.Error("delete subscription for user", "error", err, "parentURI", parentURI, "user DID", event.Did)
154147 return fmt.Errorf("delete subscription and user: %w", err)
155148 }
156149150150+ // delete from subscriptions for the parentURI and the users DID now that we have cleaned up the feeds
151151+ err = deleteFeedItemsForParentURIandUserDID(h.db, parentURI, event.Did)
152152+ if err != nil {
153153+ slog.Error("delete feed items for parentURI and user", "error", err, "parentURI", parentURI, "user DID", event.Did)
154154+ return fmt.Errorf("delete feed items for parentURI and user: %w", err)
155155+ }
156156+157157 return nil
158158}
159159160160-func (h *handler) addDidToSubscribedParent(parentURI, userDid string) error {
161161- err := addSubscriptionForParent(h.db, parentURI, userDid)
160160+func (h *handler) addDidToSubscribedParent(parentURI, userDid, rkey string) error {
161161+ err := addSubscriptionForParent(h.db, parentURI, userDid, rkey)
162162 if err != nil {
163163 return fmt.Errorf("add subscription for parent: %w", err)
164164 }