diff --git a/src/main/java/core_service/modules/follow/repo/FollowDigestRepository.java b/src/main/java/core_service/modules/follow/repo/FollowDigestRepository.java index 661f6df..328a218 100644 --- a/src/main/java/core_service/modules/follow/repo/FollowDigestRepository.java +++ b/src/main/java/core_service/modules/follow/repo/FollowDigestRepository.java @@ -89,6 +89,17 @@ public class FollowDigestRepository { return max != null ? max : 0; } + /** + * The newest hand change after {@code afterId}, or {@code afterId} when there is none. A hand-only digest stops + * there, so the status rows of a sync stay for the digest of that sync. + */ + public long maxHandStatusChangeId(long userId, long afterId) { + Long max = jdbc.queryForObject("select max(id) from user_game_status_change " + + "where user_id = :userId and by_user and id > :afterId", + user(userId).addValue("afterId", afterId), Long.class); + return max != null ? max : afterId; + } + /** * The changes the user made by hand: no old status = a game added by hand, else a status change. */ diff --git a/src/main/java/core_service/modules/follow/service/FollowDigestService.java b/src/main/java/core_service/modules/follow/service/FollowDigestService.java index 6a105b3..81c02c0 100644 --- a/src/main/java/core_service/modules/follow/service/FollowDigestService.java +++ b/src/main/java/core_service/modules/follow/service/FollowDigestService.java @@ -69,7 +69,8 @@ public class FollowDigestService { return; } Cursor cursor = locked.get(); - long upTo = digestRepository.maxStatusChangeId(userId); + long upTo = withSync ? digestRepository.maxStatusChangeId(userId) + : digestRepository.maxHandStatusChangeId(userId, cursor.statusChangeId()); Digest digest = digestRepository.countHand(userId, cursor.statusChangeId(), upTo); Instant syncStartedAt = cursor.syncStartedAt(); long wishlistUpTo = cursor.wishlistChangeId(); diff --git a/src/test/java/core_service/modules/follow/service/FollowDigestServiceTest.java b/src/test/java/core_service/modules/follow/service/FollowDigestServiceTest.java index fd3eb38..7bc3983 100644 --- a/src/test/java/core_service/modules/follow/service/FollowDigestServiceTest.java +++ b/src/test/java/core_service/modules/follow/service/FollowDigestServiceTest.java @@ -439,4 +439,20 @@ class FollowDigestServiceTest extends AbstractIntegrationTest { assertThat(jdbc.queryForObject("select count(*) from inbox_note where achievements = 7 and new_games = 2", Integer.class)).isEqualTo(2); } + + @Test + void handJobBeforeSync_LeavesTheSyncStatusRowsForTheSyncDigest() { + handGame(920_001L, 30); + added(1); + moved(FIRST_APPID + 1); + + service.digestHandChanges(); + event(); + + assertThat(details(FOLLOWER_ONE)).extracting(row -> row.get("kind"), row -> row.get("game_name")) + .containsExactlyInAnyOrder(org.assertj.core.groups.Tuple.tuple("NEW_GAME", "Hand"), + org.assertj.core.groups.Tuple.tuple("NEW_GAME", "Game 1"), + org.assertj.core.groups.Tuple.tuple("STATUS", "Game 1")); + assertThat(notes()).extracting(note -> note.get("status_changes")).containsOnly(0, 1); + } }