this repo has no description
0
fork

Configure Feed

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

now able to store which users are subscribed to a parent post and create a feed for each of them when a parent post has a reply

+54 -18
-2
auth.go
··· 47 47 }) 48 48 } 49 49 50 - 51 50 var directory = identity.DefaultDirectory() 52 51 53 52 func getRequestUserDID(r *http.Request) (string, error) { ··· 61 60 validMethods := jwt.WithValidMethods([]string{ES256, ES256K}) 62 61 63 62 keyfunc := func(token *jwt.Token) (interface{}, error) { 64 - // return token, nil 65 63 did := syntax.DID(token.Claims.(jwt.MapClaims)["iss"].(string)) 66 64 identity, err := directory.LookupDID(r.Context(), did) 67 65 if err != nil {
+44 -8
consumer.go
··· 28 28 cfg.WantedCollections = []string{ 29 29 "app.bsky.feed.post", 30 30 } 31 - cfg.WantedDids = []string{ 32 - // "did:plc:dadhhalkfcq3gucaq25hjqon", 33 - } 31 + cfg.WantedDids = []string{} 34 32 return &consumer{ 35 33 cfg: cfg, 36 34 } ··· 66 64 highwater int64 67 65 feedGenerator *FeedGenerator 68 66 mu sync.Mutex 69 - parentsToLookFor map[string]struct{} 67 + parentsToLookFor map[string]map[string]struct{} 70 68 } 71 69 72 70 func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { ··· 93 91 // 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 94 92 if strings.Contains(post.Text, "/subscribe") && event.Did == "did:plc:dadhhalkfcq3gucaq25hjqon" { 95 93 slog.Info("a post that's subscribing to a parent. Adding to parents to look for", "parent URI", post.Reply.Parent.Uri) 96 - h.parentsToLookFor[post.Reply.Parent.Uri] = struct{}{} 94 + // h.parentsToLookFor[post.Reply.Parent.Uri] = struct{}{} 95 + h.addDidToSubscribedParent(post.Reply.Parent.Uri, event.Did) 97 96 return nil 98 97 } 99 98 100 99 // see if the post is a reply to a post we are subscribed to 101 - if _, ok := h.parentsToLookFor[post.Reply.Parent.Uri]; ok { 102 - slog.Info("post is a reply to a parent we are subscribed to", "parent URI", post.Reply.Parent.Uri, "did", event.Did, "RKey", event.Commit.RKey) 103 - h.feedGenerator.AddToFeedPosts(event.Did, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey)) 100 + subscribedDids := h.getSubscribedDidsForParent(post.Reply.Parent.Uri) 101 + if len(subscribedDids) == 0 { 102 + return nil 104 103 } 104 + 105 + slog.Info("post is a reply to a parent that users are subscribed to", "parent URI", post.Reply.Parent.Uri, "dids", subscribedDids, "RKey", event.Commit.RKey) 106 + 107 + h.feedGenerator.AddToFeedPosts(subscribedDids, fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey)) 105 108 } 106 109 } 107 110 return nil 108 111 } 112 + 113 + func (h *handler) addDidToSubscribedParent(parentURI, did string) { 114 + h.mu.Lock() 115 + defer h.mu.Unlock() 116 + 117 + subscribedDids, ok := h.parentsToLookFor[parentURI] 118 + if !ok { 119 + h.parentsToLookFor[parentURI] = map[string]struct{}{ 120 + did: struct{}{}, 121 + } 122 + return 123 + } 124 + 125 + subscribedDids[did] = struct{}{} 126 + h.parentsToLookFor[parentURI] = subscribedDids 127 + } 128 + 129 + func (h *handler) getSubscribedDidsForParent(parentURI string) []string { 130 + h.mu.Lock() 131 + defer h.mu.Unlock() 132 + 133 + subscribedDids, ok := h.parentsToLookFor[parentURI] 134 + if !ok { 135 + return nil 136 + } 137 + 138 + dids := make([]string, 0, len(subscribedDids)) 139 + for did := range subscribedDids { 140 + dids = append(dids, did) 141 + } 142 + 143 + return dids 144 + }
+10 -8
feed.go
··· 40 40 return resp, nil 41 41 } 42 42 43 - func (f *FeedGenerator) AddToFeedPosts(usersDid, postURI string) { 43 + func (f *FeedGenerator) AddToFeedPosts(usersDids []string, postURI string) { 44 44 f.mu.Lock() 45 45 defer f.mu.Unlock() 46 - // TODO: store this in DB instead 47 - usersPosts, ok := f.posts[usersDid] 48 - if !ok { 49 - usersPosts = make([]string, 0, 1) 50 - } 46 + for _, did := range usersDids { 47 + // TODO: store this in DB instead 48 + usersPosts, ok := f.posts[did] 49 + if !ok { 50 + usersPosts = make([]string, 0, 1) 51 + } 51 52 52 - usersPosts = append(usersPosts, postURI) 53 - f.posts[usersDid] = usersPosts 53 + usersPosts = append(usersPosts, postURI) 54 + f.posts[did] = usersPosts 55 + } 54 56 }