diff --git a/pkg/persistence/compound_queries_test.go b/pkg/persistence/compound_queries_test.go index 5b145dd..5f94e0e 100644 --- a/pkg/persistence/compound_queries_test.go +++ b/pkg/persistence/compound_queries_test.go @@ -358,6 +358,7 @@ func TestNotificationsFeed(t *testing.T) { assert.True(liked_tweet.IsLikedByCurrentUser) notif1, is_ok := feed.TweetTrove.Notifications["FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw"] + require.True(is_ok) require.Len(notif1.TweetIDs, 1) assert.Equal(notif1.TweetIDs[0], TweetID(1507883724615999488)) require.Len(notif1.RetweetIDs, 1) diff --git a/pkg/persistence/notification.go b/pkg/persistence/notification.go index 142382f..6c27446 100644 --- a/pkg/persistence/notification.go +++ b/pkg/persistence/notification.go @@ -7,6 +7,7 @@ type NotificationType int const ( // ActionUserID is who "liked" it, Action[Re]TweetID is most recent [re]tweet they "liked". // Can have either many Users "liking" one [re]tweet, or many 1 User "liking" many [re]tweets. + // The "liked" items can be a mix of Tweets and Retweets. NOTIFICATION_TYPE_LIKE NotificationType = iota + 1 // ActionUserID is who retweeted it, Action[Re]TweetID is your [re]tweet they retweeted. diff --git a/pkg/persistence/notification_queries.go b/pkg/persistence/notification_queries.go index 6b61a18..6acfeb3 100644 --- a/pkg/persistence/notification_queries.go +++ b/pkg/persistence/notification_queries.go @@ -33,6 +33,7 @@ func (p Profile) SaveNotification(n Notification) { sort_index = max(sort_index, :sort_index), action_user_id = nullif(:action_user_id, 0), action_tweet_id = nullif(:action_tweet_id, 0), + action_retweet_id = nullif(:action_retweet_id, 0), has_detail = has_detail or :has_detail, last_scraped_at = max(last_scraped_at, :last_scraped_at) `, n) @@ -88,6 +89,8 @@ func (p Profile) GetNotification(id NotificationID) Notification { if err != nil { panic(err) } + ret.RetweetIDs = []TweetID{} // If the query returns no rows, these will otherwise be uninitialized (nil)! + ret.TweetIDs = []TweetID{} err = p.DB.Select(&ret.TweetIDs, `select tweet_id from notification_tweets where notification_id = ?`, id) if err != nil { panic(err) diff --git a/pkg/persistence/notification_queries_test.go b/pkg/persistence/notification_queries_test.go index 6537d23..34c9c78 100644 --- a/pkg/persistence/notification_queries_test.go +++ b/pkg/persistence/notification_queries_test.go @@ -35,3 +35,50 @@ func TestGetUnreadNotificationsCount(t *testing.T) { unread_notifs_count := profile.GetUnreadNotificationsCount(UserID(1488963321701171204), 1724372973735) assert.Equal(2, unread_notifs_count) } + +// Ensure that setting / blanking the Action[Re]TweetIDs works correctly +func TestLikesNotificationWithBothTweetsAndRetweets(t *testing.T) { + profile_path := "test_profiles/TestNotificationQuery" + profile := create_or_load_profile(profile_path) + + // Create a "like" on a Tweet + n := create_dummy_notification() + n.Type = NOTIFICATION_TYPE_LIKE + n.ActionTweetID = create_stable_tweet().ID + n.TweetIDs = []TweetID{n.ActionTweetID} // Overwrite the `dummy` slice + n.ActionRetweetID = TweetID(0) + n.RetweetIDs = []TweetID{} + profile.SaveNotification(n) + + // Check it comes back the same + new_n := profile.GetNotification(n.ID) + if diff := deep.Equal(n, new_n); diff != nil { + t.Error(diff) + } + + // Now the user "likes" a Retweet too + n.ActionTweetID = TweetID(0) + n.ActionRetweetID = create_stable_retweet().RetweetID + n.RetweetIDs = append(n.RetweetIDs, n.ActionRetweetID) + profile.SaveNotification(n) + + // Check it comes back the same + new_n = profile.GetNotification(n.ID) + if diff := deep.Equal(n, new_n); diff != nil { + t.Error(diff) + } + + // Now the user "likes" another Tweet + new_tweet := create_dummy_tweet() + profile.SaveTweet(new_tweet) + n.ActionTweetID = new_tweet.ID + n.ActionRetweetID = TweetID(0) + n.TweetIDs = append(n.TweetIDs, new_tweet.ID) + profile.SaveNotification(n) + + // Check it comes back the same + new_n = profile.GetNotification(n.ID) + if diff := deep.Equal(n, new_n); diff != nil { + t.Error(diff) + } +} diff --git a/pkg/persistence/utils_test.go b/pkg/persistence/utils_test.go index 720f9bf..51a45e2 100644 --- a/pkg/persistence/utils_test.go +++ b/pkg/persistence/utils_test.go @@ -204,7 +204,7 @@ func create_dummy_user() User { } } -// Create a new tweet with a random ID and content +// Create a new tweet from the stable User, with a random ID and content func create_dummy_tweet() Tweet { tweet_id := TweetID(rand.Int()) @@ -228,7 +228,7 @@ func create_dummy_tweet() Tweet { return Tweet{ ID: tweet_id, - UserID: -1, + UserID: create_stable_user().ID, Text: "text", PostedAt: Timestamp{time.Now().Truncate(1e9)}, // Round to nearest second NumLikes: 1,