this repo has no description
0
fork

Configure Feed

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

start the tests

+120 -4
+4
go.mod
··· 9 9 github.com/bugsnag/bugsnag-go/v2 v2.5.1 10 10 github.com/glebarez/go-sqlite v1.22.0 11 11 github.com/golang-jwt/jwt/v5 v5.2.1 12 + github.com/stretchr/testify v1.9.0 12 13 ) 13 14 14 15 require ( ··· 16 17 github.com/bugsnag/panicwrap v1.3.4 // indirect 17 18 github.com/carlmjohnson/versioninfo v0.22.5 // indirect 18 19 github.com/cespare/xxhash/v2 v2.3.0 // indirect 20 + github.com/davecgh/go-spew v1.1.1 // indirect 19 21 github.com/dustin/go-humanize v1.0.1 // indirect 20 22 github.com/felixge/httpsnoop v1.0.4 // indirect 21 23 github.com/go-logr/logr v1.4.1 // indirect ··· 54 56 github.com/multiformats/go-varint v0.0.7 // indirect 55 57 github.com/opentracing/opentracing-go v1.2.0 // indirect 56 58 github.com/pkg/errors v0.9.1 // indirect 59 + github.com/pmezard/go-difflib v1.0.0 // indirect 57 60 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f // indirect 58 61 github.com/prometheus/client_golang v1.19.1 // indirect 59 62 github.com/prometheus/client_model v0.6.1 // indirect ··· 77 80 golang.org/x/time v0.5.0 // indirect 78 81 golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect 79 82 google.golang.org/protobuf v1.34.2 // indirect 83 + gopkg.in/yaml.v3 v3.0.1 // indirect 80 84 lukechampine.com/blake3 v1.2.1 // indirect 81 85 modernc.org/libc v1.37.6 // indirect 82 86 modernc.org/mathutil v1.6.0 // indirect
+106
hander_test.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "encoding/json" 6 + "testing" 7 + 8 + "github.com/bluesky-social/indigo/api/atproto" 9 + apibsky "github.com/bluesky-social/indigo/api/bsky" 10 + "github.com/bluesky-social/jetstream/pkg/models" 11 + "github.com/stretchr/testify/assert" 12 + "github.com/stretchr/testify/require" 13 + "github.com/willdot/bskyfeedgen/store" 14 + ) 15 + 16 + func TestHandlerReceivesSubscribeMessage(t *testing.T) { 17 + db, err := store.New(":memory:") 18 + require.NoError(t, err) 19 + 20 + handler := handler{ 21 + store: db, 22 + } 23 + 24 + record := apibsky.FeedPost{ 25 + Text: "/subscribe", 26 + Reply: &apibsky.FeedPost_ReplyRef{ 27 + Parent: &atproto.RepoStrongRef{ 28 + Uri: "parent-uri", 29 + }, 30 + }, 31 + } 32 + 33 + recordB, err := json.Marshal(record) 34 + require.NoError(t, err) 35 + 36 + event := &models.Event{ 37 + Did: myDid, 38 + Commit: &models.Commit{ 39 + Operation: models.CommitOperationCreate, 40 + Collection: "app.bsky.feed.post", 41 + RKey: "subscribe-post-rkey", 42 + Record: recordB, 43 + }, 44 + } 45 + 46 + // send the event twice to simulate subscribing to the same post twice, to check only 47 + // 1 subscription is created 48 + err = handler.HandleEvent(context.Background(), event) 49 + require.NoError(t, err) 50 + err = handler.HandleEvent(context.Background(), event) 51 + require.NoError(t, err) 52 + 53 + subs, err := db.GetSubscriptionsForPost("parent-uri") 54 + require.NoError(t, err) 55 + 56 + assert.Len(t, subs, 1) 57 + assert.Equal(t, myDid, subs[0]) 58 + } 59 + 60 + func TestHandlerReceivesReplyToASubscribedPost(t *testing.T) { 61 + db, err := store.New(":memory:") 62 + require.NoError(t, err) 63 + 64 + handler := handler{ 65 + store: db, 66 + } 67 + 68 + // add the subscription 69 + err = db.AddSubscriptionForPost("parent-uri", myDid, "subscribe-post-rkey") 70 + require.NoError(t, err) 71 + 72 + record := apibsky.FeedPost{ 73 + Text: "this is a reply to a post that was subscribed to", 74 + Reply: &apibsky.FeedPost_ReplyRef{ 75 + Parent: &atproto.RepoStrongRef{ 76 + Uri: "parent-uri", 77 + }, 78 + }, 79 + } 80 + 81 + recordB, err := json.Marshal(record) 82 + require.NoError(t, err) 83 + 84 + event := &models.Event{ 85 + Did: myDid, 86 + Commit: &models.Commit{ 87 + Operation: models.CommitOperationCreate, 88 + Collection: "app.bsky.feed.post", 89 + RKey: "reply-post-rkey", 90 + Record: recordB, 91 + }, 92 + } 93 + 94 + // send the event twice to simulate subscribing to the same post twice, to check only 95 + // 1 subscription is created 96 + err = handler.HandleEvent(context.Background(), event) 97 + require.NoError(t, err) 98 + err = handler.HandleEvent(context.Background(), event) 99 + require.NoError(t, err) 100 + 101 + subs, err := db.GetSubscriptionsForPost("some-uri") 102 + require.NoError(t, err) 103 + 104 + assert.Len(t, subs, 1) 105 + assert.Equal(t, myDid, subs[0]) 106 + }
+5 -1
handler.go
··· 13 13 "github.com/willdot/bskyfeedgen/store" 14 14 ) 15 15 16 + const ( 17 + myDid = "did:plc:dadhhalkfcq3gucaq25hjqon" 18 + ) 19 + 16 20 type HandlerStore interface { 17 21 AddFeedPost(feedItem store.FeedPost) error 18 22 GetSubscriptionsForPost(postURI string) ([]string, error) ··· 62 66 // look for posts that are "subscribe" so that we can add the post URI to a list of posts we want to find replies for 63 67 if strings.Contains(post.Text, "/subscribe") { 64 68 // For now just look for me 65 - if event.Did != "did:plc:dadhhalkfcq3gucaq25hjqon" { 69 + if event.Did != myDid { 66 70 return nil 67 71 } 68 72 slog.Info("a post that's subscribing to another post. Adding to posts to look for", "subscribed post URI", subscribedPostURI)
+5 -3
store/database.go
··· 15 15 } 16 16 17 17 func New(dbPath string) (*Store, error) { 18 - err := createDbFile(dbPath) 19 - if err != nil { 20 - return nil, fmt.Errorf("create db file: %w", err) 18 + if dbPath != ":memory:" { 19 + err := createDbFile(dbPath) 20 + if err != nil { 21 + return nil, fmt.Errorf("create db file: %w", err) 22 + } 21 23 } 22 24 23 25 db, err := sql.Open("sqlite", dbPath)