From 1af7fa3817e2df8c427febe6fa6eb121837c58ea Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 15 Feb 2025 13:57:00 -0800 Subject: [PATCH] Notification feed should load tweet and retweet IDs in order to display 'liked N of your tweets', etc --- pkg/persistence/compound_queries.go | 25 +++++++++++++++++--- pkg/persistence/compound_queries_test.go | 8 ++++++- pkg/persistence/compound_ssf_queries_test.go | 15 ++++++------ sample_data/seed_data.sql | 10 +++++++- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pkg/persistence/compound_queries.go b/pkg/persistence/compound_queries.go index b3c2a97..f048a19 100644 --- a/pkg/persistence/compound_queries.go +++ b/pkg/persistence/compound_queries.go @@ -107,6 +107,7 @@ func (p Profile) fill_content(trove *TweetTrove, current_user_id UserID) { user_ids = append(user_ids, p) } } + // User detail is needed to render the notification properly (profile image, username) for _, n := range trove.Notifications { // Primary user if n.ActionUserID != UserID(0) { @@ -408,8 +409,7 @@ func (p Profile) GetNotificationsForUser(u_id UserID, cursor int64, count int64) panic(err) } - // Get the user_ids list for each notification. Unlike tweet+retweet_ids, users are needed to render - // the notification properly. + // Get the user_ids, tweet_ids, and retweet_ids list for each notification for i := range notifications { err = p.DB.Select(¬ifications[i].UserIDs, `select user_id from notification_users where notification_id = ?`, @@ -419,8 +419,27 @@ func (p Profile) GetNotificationsForUser(u_id UserID, cursor int64, count int64) panic(err) } } + for i := range notifications { + err = p.DB.Select(¬ifications[i].TweetIDs, + `select tweet_id from notification_tweets where notification_id = ?`, + notifications[i].ID, + ) + if err != nil { + panic(err) + } + } + for i := range notifications { + err = p.DB.Select(¬ifications[i].RetweetIDs, + `select retweet_id from notification_retweets where notification_id = ?`, + notifications[i].ID, + ) + if err != nil { + panic(err) + } + } - // Collect tweet and retweet IDs + // Collect tweet and retweet IDs. + // Users are fetched in `fill_content` retweet_ids := []TweetID{} tweet_ids := []TweetID{} for _, n := range notifications { diff --git a/pkg/persistence/compound_queries_test.go b/pkg/persistence/compound_queries_test.go index 858bc0b..5b145dd 100644 --- a/pkg/persistence/compound_queries_test.go +++ b/pkg/persistence/compound_queries_test.go @@ -352,11 +352,17 @@ 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" + // This tweet should be "liked" liked_tweet, is_ok := feed.TweetTrove.Tweets[1826778617705115869] require.True(is_ok) assert.True(liked_tweet.IsLikedByCurrentUser) + notif1, is_ok := feed.TweetTrove.Notifications["FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw"] + require.Len(notif1.TweetIDs, 1) + assert.Equal(notif1.TweetIDs[0], TweetID(1507883724615999488)) + require.Len(notif1.RetweetIDs, 1) + assert.Equal(notif1.RetweetIDs[0], TweetID(1490135787124232223)) + assert.Equal(feed.CursorBottom.CursorPosition, CURSOR_MIDDLE) assert.Equal(feed.CursorBottom.CursorValue, 1723494244885) diff --git a/pkg/persistence/compound_ssf_queries_test.go b/pkg/persistence/compound_ssf_queries_test.go index c8ee02c..5625a6b 100644 --- a/pkg/persistence/compound_ssf_queries_test.go +++ b/pkg/persistence/compound_ssf_queries_test.go @@ -99,19 +99,20 @@ func TestTimeline(t *testing.T) { require.NoError(err) c := NewTimelineCursor() - c.PageSize = 6 + c.PageSize = 7 feed, err := profile.NextPage(c, UserID(0)) require.NoError(err) - assert.Len(feed.Items, 6) + assert.Len(feed.Items, 7) assert.Len(feed.Retweets, 4) assert.Equal(feed.Items[0].TweetID, TweetID(1826778617705115868)) - assert.Equal(feed.Items[1].RetweetID, TweetID(1490135787144237058)) - assert.Equal(feed.Items[2].RetweetID, TweetID(1490135787124232223)) - assert.Equal(feed.Items[3].RetweetID, TweetID(1490119308692766723)) - assert.Equal(feed.Items[4].RetweetID, TweetID(1490100255987171332)) - assert.Equal(feed.Items[5].TweetID, TweetID(1453461248142495744)) + assert.Equal(feed.Items[1].TweetID, TweetID(1507883724615999488)) + assert.Equal(feed.Items[2].RetweetID, TweetID(1490135787144237058)) + assert.Equal(feed.Items[3].RetweetID, TweetID(1490135787124232223)) + assert.Equal(feed.Items[4].RetweetID, TweetID(1490119308692766723)) + assert.Equal(feed.Items[5].RetweetID, TweetID(1490100255987171332)) + assert.Equal(feed.Items[6].TweetID, TweetID(1453461248142495744)) next_cursor := feed.CursorBottom assert.Equal(next_cursor.CursorPosition, CURSOR_MIDDLE) diff --git a/sample_data/seed_data.sql b/sample_data/seed_data.sql index f824f62..f2c9eaa 100644 --- a/sample_data/seed_data.sql +++ b/sample_data/seed_data.sql @@ -196,7 +196,8 @@ INSERT INTO tweets VALUES (1411566,1698848086880133147,1458284524761075714,'I have basically no experience with one and literally no experience with the other, and additionally I''ve never even used Haskell. So unfortunately I''m not really in a position to say.',1693871921000,1,0,1,0,1698802806096846909,0,'sol_plunder,ilyakooo0','sol_plunder,ilyakooo0','',NULL,NULL, 0,1,0,0,0), (1169437,1665509126737129472,1458284524761075714,replace('Btw, to the extent this has happened, it''s partly thanks to the Golden One (@TheGloriousLion) who invented #fizeekfriday and the "post physique" rejoinder. Everyone should follow him if they don''t already.\n\nSince I forgot last week, and since it''s topical, here''s a leg poast','\n',char(10)),1685923294000,7,0,0,0,1665505986184900611,0,'TheGloriousLion','','fizeekfriday',NULL,NULL,0,1,0,0,0), (2857438,1826778617705115868,1488963321701171204,'Conversations are trees, not sequences. They branch. They don''t flow in a perfectly linear way.',1724372937000,4,1,0,0,0,0,'','','',NULL,NULL,0,1,0,0,0), - (2857439,1826778617705115869,1178839081222115328,'Real tweet that is definitely real',1724372938000,4,1,0,0,1826778617705115868,0,'Offline_Twatter','Offline_Twatter','',NULL,NULL,0,1,0,0,0); + (2857439,1826778617705115869,1178839081222115328,'Real tweet that is definitely real',1724372938000,4,1,0,0,1826778617705115868,0,'Offline_Twatter','Offline_Twatter','',NULL,NULL,0,1,0,0,0), + (98105,1507883724615999488,1488963321701171204,'Apparently I am not allowed to call myself "Twitter"',1648342469000,5,1,0,1,0,0,'','','',NULL,NULL,0,1,1,1739654377321,0); CREATE TABLE retweets(rowid integer primary key, retweet_id integer not null unique, @@ -576,12 +577,19 @@ create table notification_tweets (rowid integer primary key, tweet_id not null references tweets(id), unique(notification_id, tweet_id) ); +insert into notification_tweets values + (1, 'FKncQJGVgAQAAAABSQ3bEaTgXL8f40e77r4', 1826778617705115868), + (2, 'FKncQJGVgAQAAAABSQ3bEaTgXL8VBxefepo', 1826778617705115868), + (3, 'FDzeDIfVUAIAAvsBiJONcqYgiLgXOolO9t0', 1826778617705115869), + (4, 'FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw', 1507883724615999488); create table notification_retweets (rowid integer primary key, notification_id not null references notifications(id), retweet_id not null references retweets(retweet_id), unique(notification_id, retweet_id) ); +insert into notification_retweets values + (1, 'FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw', 1490135787124232223); create table notification_users (rowid integer primary key, notification_id not null references notifications(id),