this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

implement delete

+89 -27
+70 -26
consumer.go
··· 68 68 } 69 69 70 70 func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { 71 - // Unmarshal the record if there is one 72 71 if event.Commit == nil { 73 72 return nil 74 73 } 75 - if event.Commit.Operation == models.CommitOperationCreate || event.Commit.Operation == models.CommitOperationUpdate { 76 - switch event.Commit.Collection { 77 - case "app.bsky.feed.post": 78 - var post apibsky.FeedPost 79 - if err := json.Unmarshal(event.Commit.Record, &post); err != nil { 80 - return fmt.Errorf("failed to unmarshal post: %w", err) 81 - } 82 74 83 - // we only care about posts that have parents which are replies 84 - if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" { 85 - return nil 86 - } 75 + switch event.Commit.Operation { 76 + case models.CommitOperationCreate: 77 + return h.handleCreateEvent(ctx, event) 78 + case models.CommitOperationDelete: 79 + return h.handleDeleteEvent(ctx, event) 80 + default: 81 + return nil 82 + } 83 + } 87 84 88 - parentURI := post.Reply.Parent.Uri 85 + func (h *handler) handleCreateEvent(_ context.Context, event *models.Event) error { 86 + if event.Commit.Collection != "app.bsky.feed.post" { 87 + return nil 88 + } 89 89 90 - // 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 91 - if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" { 92 - slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", parentURI) 93 - return h.addDidToSubscribedParent(parentURI, event.Did) 94 - } 90 + var post apibsky.FeedPost 91 + if err := json.Unmarshal(event.Commit.Record, &post); err != nil { 92 + return fmt.Errorf("failed to unmarshal post: %w", err) 93 + } 95 94 96 - // see if the post is a reply to a post we are subscribed to 97 - subscribedDids := h.getSubscribedDidsForParent(parentURI) 98 - if len(subscribedDids) == 0 { 99 - return nil 100 - } 95 + // we only care about posts that have parents which are replies 96 + if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" { 97 + return nil 98 + } 101 99 102 - slog.Info("post is a reply to a parent that users are subscribed to", "parent URI", parentURI, "dids", subscribedDids, "RKey", event.Commit.RKey) 100 + parentURI := post.Reply.Parent.Uri 103 101 104 - h.feedGenerator.AddToFeedPosts(subscribedDids, parentURI, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey)) 105 - } 102 + // 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 103 + if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" { 104 + slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", parentURI) 105 + return h.addDidToSubscribedParent(parentURI, event.Did) 106 106 } 107 + 108 + // see if the post is a reply to a post we are subscribed to 109 + subscribedDids := h.getSubscribedDidsForParent(parentURI) 110 + if len(subscribedDids) == 0 { 111 + return nil 112 + } 113 + 114 + slog.Info("post is a reply to a parent that users are subscribed to", "parent URI", parentURI, "dids", subscribedDids, "RKey", event.Commit.RKey) 115 + 116 + h.feedGenerator.AddToFeedPosts(subscribedDids, parentURI, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey)) 117 + return nil 118 + } 119 + 120 + func (h *handler) handleDeleteEvent(_ context.Context, event *models.Event) error { 121 + if event.Commit.Collection != "app.bsky.feed.post" { 122 + return nil 123 + } 124 + 125 + var post apibsky.FeedPost 126 + if err := json.Unmarshal(event.Commit.Record, &post); err != nil { 127 + return fmt.Errorf("failed to unmarshal post: %w", err) 128 + } 129 + 130 + // we only care about posts that have parents which are replies 131 + if post.Reply == nil || post.Reply.Parent == nil || post.Reply.Parent.Uri == "" { 132 + return nil 133 + } 134 + 135 + parentURI := post.Reply.Parent.Uri 136 + 137 + // delete from subscriptions for the parentURI and the users DID 138 + err := deleteFeedItemsForParentURIandUserDID(h.db, parentURI, event.Did) 139 + if err != nil { 140 + slog.Error("delete feed items for parentURI and user", "error", err, "parentURI", parentURI, "user DID", event.Did) 141 + return fmt.Errorf("delete feed items for parentURI and user: %w", err) 142 + } 143 + 144 + // delete from feeds for the parentURI and the users DID 145 + err = deleteSubscriptionForUser(h.db, event.Did, parentURI) 146 + if err != nil { 147 + slog.Error("delete subscription for user", "error", err, "parentURI", parentURI, "user DID", event.Did) 148 + return fmt.Errorf("delete subscription and user: %w", err) 149 + } 150 + 107 151 return nil 108 152 } 109 153
+19 -1
database.go
··· 115 115 } 116 116 117 117 func getUsersFeedItems(db *sql.DB, usersDID string) ([]feedItem, error) { 118 - sql := "SELECT id, uri, userDID FROM feed WHERE userDID = ?" 118 + sql := "SELECT id, uri, userDID FROM feed WHERE userDID = ?;" 119 119 rows, err := db.Query(sql, usersDID) 120 120 if err != nil { 121 121 return nil, fmt.Errorf("run query to get users feed item: %w", err) ··· 134 134 return feedItems, nil 135 135 } 136 136 137 + func deleteFeedItemsForParentURIandUserDID(db *sql.DB, parentURI, userDID string) error { 138 + sql := "DELETE FROM feed WHERE (uri = ? AND userDID = ?);" 139 + _, err := db.Exec(sql, parentURI, userDID) 140 + if err != nil { 141 + return fmt.Errorf("exec delete feed items: %w", err) 142 + } 143 + return nil 144 + } 145 + 137 146 type subscription struct { 138 147 ID int 139 148 ParentURI string ··· 168 177 } 169 178 return nil 170 179 } 180 + 181 + func deleteSubscriptionForUser(db *sql.DB, userDID, parentURI string) error { 182 + sql := "DELETE FROM subscription WHERE (parentURI = ? AND userDID = ?);" 183 + _, err := db.Exec(sql, parentURI, userDID) 184 + if err != nil { 185 + return fmt.Errorf("exec delete subscription for user: %w", err) 186 + } 187 + return nil 188 + }