From 435c53a4e66f48b9ec1d2e655955dfe4831e3e1a Mon Sep 17 00:00:00 2001 From: Will Andrews Date: Thu, 21 Nov 2024 20:41:14 +0000 Subject: [PATCH] store the createdAt timestamp for when the feed item was added --- handler.go | 8 ++++++-- store/feed.go | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/handler.go b/handler.go index 4d83aa4..5369999 100644 --- a/handler.go +++ b/handler.go @@ -6,6 +6,7 @@ import ( "fmt" "log/slog" "strings" + "time" apibsky "github.com/bluesky-social/indigo/api/bsky" "github.com/bluesky-social/jetstream/pkg/models" @@ -81,8 +82,10 @@ func (h *handler) handleCreateEvent(_ context.Context, event *models.Event) erro slog.Info("post is a reply to a post that users are subscribed to", "subscribed post URI", subscribedPostURI, "dids", subscribedDids, "RKey", event.Commit.RKey) + createdAt := time.Now().UTC().UnixNano() + replyPostURI := fmt.Sprintf("at://%s/app.bsky.feed.post/%s", event.Did, event.Commit.RKey) - h.createFeedPostForSubscribedUsers(subscribedDids, replyPostURI, subscribedPostURI) + h.createFeedPostForSubscribedUsers(subscribedDids, replyPostURI, subscribedPostURI, createdAt) return nil } @@ -138,12 +141,13 @@ func (h *handler) getSubscribedDidsForPost(postURI string) []string { return dids } -func (h *handler) createFeedPostForSubscribedUsers(usersDids []string, replyPostURI, subscribedPostURI string) { +func (h *handler) createFeedPostForSubscribedUsers(usersDids []string, replyPostURI, subscribedPostURI string, createdAt int64) { for _, did := range usersDids { feedItem := store.FeedPost{ ReplyURI: replyPostURI, UserDID: did, SubscribedPostURI: subscribedPostURI, + CreatedAt: createdAt, } err := h.store.AddFeedPost(feedItem) if err != nil { diff --git a/store/feed.go b/store/feed.go index 9e7d273..74754ff 100644 --- a/store/feed.go +++ b/store/feed.go @@ -12,6 +12,7 @@ func createFeedTable(db *sql.DB) error { "replyURI" TEXT, "userDID" TEXT, "subscribedPostURI" TEXT, + "createdAt" integer NOT NULL, UNIQUE(replyURI, userDID) );` @@ -34,11 +35,12 @@ type FeedPost struct { ReplyURI string UserDID string SubscribedPostURI string + CreatedAt int64 } func (s *Store) AddFeedPost(feedPost FeedPost) error { - sql := `INSERT INTO feed (replyURI, userDID, subscribedPostURI) VALUES (?, ?, ?) ON CONFLICT(replyURI, userDID) DO NOTHING;` - _, err := s.db.Exec(sql, feedPost.ReplyURI, feedPost.UserDID, feedPost.SubscribedPostURI) + sql := `INSERT INTO feed (replyURI, userDID, subscribedPostURI, createdAt) VALUES (?, ?, ?, ?) ON CONFLICT(replyURI, userDID) DO NOTHING;` + _, err := s.db.Exec(sql, feedPost.ReplyURI, feedPost.UserDID, feedPost.SubscribedPostURI, feedPost.CreatedAt) if err != nil { return fmt.Errorf("exec insert feed item: %w", err) } -- 2.51.2