BUGFIX: ensure the ActionTweetID and ActionRetweetIDs are updated correctly when adding new Tweets/Retweets to a notification's lists

This commit is contained in:
Alessio 2025-02-15 14:30:47 -08:00
parent 084bca9c57
commit c0e4c85028
5 changed files with 54 additions and 2 deletions

View File

@ -358,6 +358,7 @@ func TestNotificationsFeed(t *testing.T) {
assert.True(liked_tweet.IsLikedByCurrentUser) assert.True(liked_tweet.IsLikedByCurrentUser)
notif1, is_ok := feed.TweetTrove.Notifications["FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw"] notif1, is_ok := feed.TweetTrove.Notifications["FDzeDIfVUAIAAAABiJONcqaBFAzeN-n-Luw"]
require.True(is_ok)
require.Len(notif1.TweetIDs, 1) require.Len(notif1.TweetIDs, 1)
assert.Equal(notif1.TweetIDs[0], TweetID(1507883724615999488)) assert.Equal(notif1.TweetIDs[0], TweetID(1507883724615999488))
require.Len(notif1.RetweetIDs, 1) require.Len(notif1.RetweetIDs, 1)

View File

@ -7,6 +7,7 @@ type NotificationType int
const ( const (
// ActionUserID is who "liked" it, Action[Re]TweetID is most recent [re]tweet they "liked". // 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. // 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 NOTIFICATION_TYPE_LIKE NotificationType = iota + 1
// ActionUserID is who retweeted it, Action[Re]TweetID is your [re]tweet they retweeted. // ActionUserID is who retweeted it, Action[Re]TweetID is your [re]tweet they retweeted.

View File

@ -33,6 +33,7 @@ func (p Profile) SaveNotification(n Notification) {
sort_index = max(sort_index, :sort_index), sort_index = max(sort_index, :sort_index),
action_user_id = nullif(:action_user_id, 0), action_user_id = nullif(:action_user_id, 0),
action_tweet_id = nullif(:action_tweet_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, has_detail = has_detail or :has_detail,
last_scraped_at = max(last_scraped_at, :last_scraped_at) last_scraped_at = max(last_scraped_at, :last_scraped_at)
`, n) `, n)
@ -88,6 +89,8 @@ func (p Profile) GetNotification(id NotificationID) Notification {
if err != nil { if err != nil {
panic(err) 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) err = p.DB.Select(&ret.TweetIDs, `select tweet_id from notification_tweets where notification_id = ?`, id)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -35,3 +35,50 @@ func TestGetUnreadNotificationsCount(t *testing.T) {
unread_notifs_count := profile.GetUnreadNotificationsCount(UserID(1488963321701171204), 1724372973735) unread_notifs_count := profile.GetUnreadNotificationsCount(UserID(1488963321701171204), 1724372973735)
assert.Equal(2, unread_notifs_count) 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)
}
}

View File

@ -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 { func create_dummy_tweet() Tweet {
tweet_id := TweetID(rand.Int()) tweet_id := TweetID(rand.Int())
@ -228,7 +228,7 @@ func create_dummy_tweet() Tweet {
return Tweet{ return Tweet{
ID: tweet_id, ID: tweet_id,
UserID: -1, UserID: create_stable_user().ID,
Text: "text", Text: "text",
PostedAt: Timestamp{time.Now().Truncate(1e9)}, // Round to nearest second PostedAt: Timestamp{time.Now().Truncate(1e9)}, // Round to nearest second
NumLikes: 1, NumLikes: 1,