this repo has no description
0
fork

Configure Feed

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

opps need the tests in there

+179 -1
-1
handler.go
··· 86 86 87 87 slog.Info("post is a reply to a post that users are subscribed to", "subscribed post URI", subscribedPostURI, "dids", subscribedDids, "RKey", event.Commit.RKey) 88 88 89 - // TODO: parse from the event 90 89 createdAt, err := time.Parse(time.RFC1123, post.CreatedAt) 91 90 if err != nil { 92 91 slog.Error("parsing createdAt time from post", "error", err, "timestamp", post.CreatedAt)
+179
handler_test.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "encoding/json" 6 + "fmt" 7 + "testing" 8 + "time" 9 + 10 + "github.com/bluesky-social/indigo/api/atproto" 11 + apibsky "github.com/bluesky-social/indigo/api/bsky" 12 + "github.com/bluesky-social/jetstream/pkg/models" 13 + "github.com/stretchr/testify/assert" 14 + "github.com/stretchr/testify/require" 15 + "github.com/willdot/bskyfeedgen/store" 16 + ) 17 + 18 + func TestIt(t *testing.T) { 19 + ti := time.Now().Add(-time.Minute * 60) 20 + 21 + fmt.Println(ti.UnixMicro()) 22 + t.FailNow() 23 + } 24 + 25 + func TestHandlerReceivesSubscribeMessage(t *testing.T) { 26 + db, err := store.New(":memory:") 27 + require.NoError(t, err) 28 + 29 + handler := handler{ 30 + store: db, 31 + } 32 + 33 + record := apibsky.FeedPost{ 34 + Text: "/subscribe", 35 + Reply: &apibsky.FeedPost_ReplyRef{ 36 + Parent: &atproto.RepoStrongRef{ 37 + Uri: "parent-uri", 38 + }, 39 + }, 40 + } 41 + 42 + recordB, err := json.Marshal(record) 43 + require.NoError(t, err) 44 + 45 + event := &models.Event{ 46 + Did: myDid, 47 + Commit: &models.Commit{ 48 + Operation: models.CommitOperationCreate, 49 + Collection: "app.bsky.feed.post", 50 + RKey: "subscribe-post-rkey", 51 + Record: recordB, 52 + }, 53 + } 54 + 55 + // send the event twice to simulate subscribing to the same post twice, to check only 56 + // 1 subscription is created 57 + err = handler.HandleEvent(context.Background(), event) 58 + require.NoError(t, err) 59 + err = handler.HandleEvent(context.Background(), event) 60 + require.NoError(t, err) 61 + 62 + subs, err := db.GetSubscriptionsForPost("parent-uri") 63 + require.NoError(t, err) 64 + 65 + assert.Len(t, subs, 1) 66 + assert.Equal(t, myDid, subs[0]) 67 + } 68 + 69 + func TestHandlerReceivesReplyToASubscribedPost(t *testing.T) { 70 + db, err := store.New(":memory:") 71 + require.NoError(t, err) 72 + 73 + handler := handler{ 74 + store: db, 75 + } 76 + 77 + // add the subscription 78 + err = db.AddSubscriptionForPost("parent-uri", myDid, "subscribe-post-rkey") 79 + require.NoError(t, err) 80 + 81 + record := apibsky.FeedPost{ 82 + Text: "this is a reply to a post that was subscribed to", 83 + Reply: &apibsky.FeedPost_ReplyRef{ 84 + Parent: &atproto.RepoStrongRef{ 85 + Uri: "parent-uri", 86 + }, 87 + }, 88 + } 89 + 90 + recordB, err := json.Marshal(record) 91 + require.NoError(t, err) 92 + 93 + event := &models.Event{ 94 + Did: "some-random-did", 95 + Commit: &models.Commit{ 96 + Operation: models.CommitOperationCreate, 97 + Collection: "app.bsky.feed.post", 98 + RKey: "reply-post-rkey", 99 + Record: recordB, 100 + }, 101 + } 102 + 103 + err = handler.HandleEvent(context.Background(), event) 104 + require.NoError(t, err) 105 + 106 + feed, err := db.GetUsersFeed(myDid, 9999999999999, 5) 107 + require.NoError(t, err) 108 + 109 + assert.Len(t, feed, 1) 110 + expectedFeedPost := store.FeedPost{ 111 + ID: 1, 112 + ReplyURI: "at://some-random-did/app.bsky.feed.post/reply-post-rkey", 113 + UserDID: myDid, 114 + SubscribedPostURI: "parent-uri", 115 + } 116 + 117 + res := feed[0] 118 + // timestamps are hard to assert so check it's within a few seconds and then remove from 119 + // the result so the rest of the assertion can complete 120 + assert.WithinDuration(t, time.Now(), time.UnixMilli(res.CreatedAt), time.Second) 121 + res.CreatedAt = 0 122 + 123 + assert.Equal(t, expectedFeedPost, res) 124 + } 125 + 126 + func TestHandlerReceivesDeleteEvent(t *testing.T) { 127 + db, err := store.New(":memory:") 128 + require.NoError(t, err) 129 + 130 + handler := handler{ 131 + store: db, 132 + } 133 + 134 + // add the subscription 135 + err = db.AddSubscriptionForPost("parent-uri", myDid, "subscribe-post-rkey") 136 + require.NoError(t, err) 137 + // add in some feed posts 138 + feedPost1 := store.FeedPost{ 139 + ReplyURI: "at://some-random-did-1/app.bsky.feed.post/reply-post-rkey", 140 + UserDID: myDid, 141 + SubscribedPostURI: "parent-uri", 142 + } 143 + feedPost2 := store.FeedPost{ 144 + ReplyURI: "at://some-random-did-2/app.bsky.feed.post/reply-post-rkey", 145 + UserDID: myDid, 146 + SubscribedPostURI: "parent-uri", 147 + } 148 + err = db.AddFeedPost(feedPost1) 149 + require.NoError(t, err) 150 + err = db.AddFeedPost(feedPost2) 151 + require.NoError(t, err) 152 + // add a feed post for a different subscribed post 153 + feedPost3 := store.FeedPost{ 154 + ReplyURI: "at://some-random-did-3/app.bsky.feed.post/reply-post-rkey", 155 + UserDID: myDid, 156 + SubscribedPostURI: "different-parent-uri", 157 + } 158 + err = db.AddFeedPost(feedPost3) 159 + require.NoError(t, err) 160 + 161 + event := &models.Event{ 162 + Did: myDid, 163 + Commit: &models.Commit{ 164 + Operation: models.CommitOperationDelete, 165 + Collection: "app.bsky.feed.post", 166 + RKey: "subscribe-post-rkey", 167 + }, 168 + } 169 + 170 + err = handler.HandleEvent(context.Background(), event) 171 + require.NoError(t, err) 172 + 173 + feed, err := db.GetUsersFeed(myDid, 9999999999999, 5) 174 + require.NoError(t, err) 175 + 176 + assert.Len(t, feed, 1) 177 + feedPost3.ID = 3 178 + assert.Equal(t, feedPost3, feed[0]) 179 + }