From 5be812e96eb4992729bd1d062953e3c1002f6929 Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 15 Feb 2025 12:51:07 -0800 Subject: [PATCH] Include current user "likes" information when building notifications feed --- pkg/persistence/compound_queries.go | 13 +++++++------ pkg/persistence/compound_queries_test.go | 5 +++++ sample_data/seed_data.sql | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/persistence/compound_queries.go b/pkg/persistence/compound_queries.go index 64e0b89..b3c2a97 100644 --- a/pkg/persistence/compound_queries.go +++ b/pkg/persistence/compound_queries.go @@ -25,6 +25,8 @@ const TWEETS_ALL_SQL_FIELDS = ` // - spaces // - users // - images, videos, urls, polls +// +// `current_user_id` is used to fetch likes on quoted tweets func (p Profile) fill_content(trove *TweetTrove, current_user_id UserID) { if len(trove.Tweets) == 0 { // Empty trove, nothing to fetch @@ -453,15 +455,15 @@ func (p Profile) GetNotificationsForUser(u_id UserID, cursor int64, count int64) tweet_ids = append(tweet_ids, r.TweetID) } } - // Get tweets, if there are any var tweets []Tweet if len(tweet_ids) != 0 { - sql_str, vals, err := sqlx.In(`select `+TWEETS_ALL_SQL_FIELDS+` + sql_str, vals, err := sqlx.In(` + select `+TWEETS_ALL_SQL_FIELDS+` from tweets left join tombstone_types on tweets.tombstone_type = tombstone_types.rowid - left join likes on tweets.id = likes.tweet_id and likes.user_id = -1 - where id in (?)`, tweet_ids) + left join likes on tweets.id = likes.tweet_id and likes.user_id = ? + where id in (?)`, u_id, tweet_ids) if err != nil { panic(err) } @@ -496,8 +498,7 @@ func (p Profile) GetNotificationsForUser(u_id UserID, cursor int64, count int64) ret.Items = append(ret.Items, feed_item) } - // TODO: proper user id - p.fill_content(&ret.TweetTrove, UserID(0)) + p.fill_content(&ret.TweetTrove, u_id) // Set the bottom cursor value ret.CursorBottom = Cursor{} diff --git a/pkg/persistence/compound_queries_test.go b/pkg/persistence/compound_queries_test.go index a00588b..858bc0b 100644 --- a/pkg/persistence/compound_queries_test.go +++ b/pkg/persistence/compound_queries_test.go @@ -352,6 +352,11 @@ func TestNotificationsFeed(t *testing.T) { assert.Equal(feed.Items[5].NotificationID, NotificationID("FKncQJGVgAQAAAABSQ3bEaTgXL8f40e77r4")) assert.Equal(feed.Items[5].TweetID, TweetID(1826778617705115868)) + // Tweet should be "liked" + liked_tweet, is_ok := feed.TweetTrove.Tweets[1826778617705115869] + require.True(is_ok) + assert.True(liked_tweet.IsLikedByCurrentUser) + assert.Equal(feed.CursorBottom.CursorPosition, CURSOR_MIDDLE) assert.Equal(feed.CursorBottom.CursorValue, 1723494244885) diff --git a/sample_data/seed_data.sql b/sample_data/seed_data.sql index d0599ff..f824f62 100644 --- a/sample_data/seed_data.sql +++ b/sample_data/seed_data.sql @@ -359,7 +359,8 @@ insert into likes values (2, 2, 1178839081222115328, 1513313535480287235), (3, 3, 1178839081222115328, 1343633011364016128), (4, 4, 1178839081222115328, 1426669666928414720), - (5, 5, 1178839081222115328, 1698765208393576891); + (5, 5, 1178839081222115328, 1698765208393576891), + (6, 6, 1488963321701171204, 1826778617705115869); create table bookmarks(rowid integer primary key,