···6868}
69697070func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error {
7171- // Unmarshal the record if there is one
7271 if event.Commit == nil {
7372 return nil
7473 }
7575- if event.Commit.Operation == models.CommitOperationCreate || event.Commit.Operation == models.CommitOperationUpdate {
7676- switch event.Commit.Collection {
7777- case "app.bsky.feed.post":
7878- var post apibsky.FeedPost
7979- if err := json.Unmarshal(event.Commit.Record, &post); err != nil {
8080- return fmt.Errorf("failed to unmarshal post: %w", err)
8181- }
82748383- // we only care about posts that have parents which are replies
8484- if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" {
8585- return nil
8686- }
7575+ switch event.Commit.Operation {
7676+ case models.CommitOperationCreate:
7777+ return h.handleCreateEvent(ctx, event)
7878+ case models.CommitOperationDelete:
7979+ return h.handleDeleteEvent(ctx, event)
8080+ default:
8181+ return nil
8282+ }
8383+}
87848888- parentURI := post.Reply.Parent.Uri
8585+func (h *handler) handleCreateEvent(_ context.Context, event *models.Event) error {
8686+ if event.Commit.Collection != "app.bsky.feed.post" {
8787+ return nil
8888+ }
89899090- // 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
9191- if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" {
9292- slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", parentURI)
9393- return h.addDidToSubscribedParent(parentURI, event.Did)
9494- }
9090+ var post apibsky.FeedPost
9191+ if err := json.Unmarshal(event.Commit.Record, &post); err != nil {
9292+ return fmt.Errorf("failed to unmarshal post: %w", err)
9393+ }
95949696- // see if the post is a reply to a post we are subscribed to
9797- subscribedDids := h.getSubscribedDidsForParent(parentURI)
9898- if len(subscribedDids) == 0 {
9999- return nil
100100- }
9595+ // we only care about posts that have parents which are replies
9696+ if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" {
9797+ return nil
9898+ }
10199102102- slog.Info("post is a reply to a parent that users are subscribed to", "parent URI", parentURI, "dids", subscribedDids, "RKey", event.Commit.RKey)
100100+ parentURI := post.Reply.Parent.Uri
103101104104- h.feedGenerator.AddToFeedPosts(subscribedDids, parentURI, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey))
105105- }
102102+ // 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
103103+ if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" {
104104+ slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", parentURI)
105105+ return h.addDidToSubscribedParent(parentURI, event.Did)
106106 }
107107+108108+ // see if the post is a reply to a post we are subscribed to
109109+ subscribedDids := h.getSubscribedDidsForParent(parentURI)
110110+ if len(subscribedDids) == 0 {
111111+ return nil
112112+ }
113113+114114+ slog.Info("post is a reply to a parent that users are subscribed to", "parent URI", parentURI, "dids", subscribedDids, "RKey", event.Commit.RKey)
115115+116116+ h.feedGenerator.AddToFeedPosts(subscribedDids, parentURI, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey))
117117+ return nil
118118+}
119119+120120+func (h *handler) handleDeleteEvent(_ context.Context, event *models.Event) error {
121121+ if event.Commit.Collection != "app.bsky.feed.post" {
122122+ return nil
123123+ }
124124+125125+ var post apibsky.FeedPost
126126+ if err := json.Unmarshal(event.Commit.Record, &post); err != nil {
127127+ return fmt.Errorf("failed to unmarshal post: %w", err)
128128+ }
129129+130130+ // we only care about posts that have parents which are replies
131131+ if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" {
132132+ return nil
133133+ }
134134+135135+ parentURI := post.Reply.Parent.Uri
136136+137137+ // delete from subscriptions for the parentURI and the users DID
138138+ err := deleteFeedItemsForParentURIandUserDID(h.db, parentURI, event.Did)
139139+ if err != nil {
140140+ slog.Error("delete feed items for parentURI and user", "error", err, "parentURI", parentURI, "user DID", event.Did)
141141+ return fmt.Errorf("delete feed items for parentURI and user: %w", err)
142142+ }
143143+144144+ // delete from feeds for the parentURI and the users DID
145145+ err = deleteSubscriptionForUser(h.db, event.Did, parentURI)
146146+ if err != nil {
147147+ slog.Error("delete subscription for user", "error", err, "parentURI", parentURI, "user DID", event.Did)
148148+ return fmt.Errorf("delete subscription and user: %w", err)
149149+ }
150150+107151 return nil
108152}
109153
+19-1
database.go
···115115}
116116117117func getUsersFeedItems(db *sql.DB, usersDID string) ([]feedItem, error) {
118118- sql := "SELECT id, uri, userDID FROM feed WHERE userDID = ?"
118118+ sql := "SELECT id, uri, userDID FROM feed WHERE userDID = ?;"
119119 rows, err := db.Query(sql, usersDID)
120120 if err != nil {
121121 return nil, fmt.Errorf("run query to get users feed item: %w", err)
···134134 return feedItems, nil
135135}
136136137137+func deleteFeedItemsForParentURIandUserDID(db *sql.DB, parentURI, userDID string) error {
138138+ sql := "DELETE FROM feed WHERE (uri = ? AND userDID = ?);"
139139+ _, err := db.Exec(sql, parentURI, userDID)
140140+ if err != nil {
141141+ return fmt.Errorf("exec delete feed items: %w", err)
142142+ }
143143+ return nil
144144+}
145145+137146type subscription struct {
138147 ID int
139148 ParentURI string
···168177 }
169178 return nil
170179}
180180+181181+func deleteSubscriptionForUser(db *sql.DB, userDID, parentURI string) error {
182182+ sql := "DELETE FROM subscription WHERE (parentURI = ? AND userDID = ?);"
183183+ _, err := db.Exec(sql, parentURI, userDID)
184184+ if err != nil {
185185+ return fmt.Errorf("exec delete subscription for user: %w", err)
186186+ }
187187+ return nil
188188+}