diff options
-rw-r--r-- | src/main/java/com/juick/service/MessagesService.java | 2 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 10 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 14 |
3 files changed, 13 insertions, 13 deletions
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index 5574dbbf..6f262dd7 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -76,7 +76,7 @@ public interface MessagesService { Message getReplyByUri(String replyUri); - List<Pair<Integer, User>> getMessagesRecommendations(Collection<Integer> mids); + List<Pair<Integer, User>> getMessagesRecommendations(int visitorUid, Collection<Integer> mids); @Cacheable(value = "discover", key="{ #visitorUid, #before }") List<Integer> getAll(int visitorUid, int before); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 78197a0f..8292f4fd 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -490,16 +490,18 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List<Pair<Integer, User>> getMessagesRecommendations(final Collection<Integer> mids) { + public List<Pair<Integer, User>> getMessagesRecommendations(final int visitorUid, final Collection<Integer> mids) { return getNamedParameterJdbcTemplate().query( "SELECT DISTINCT favorites.message_id, users.id, users.nick, favorites.user_uri FROM favorites " + "INNER JOIN users ON (favorites.user_id = users.id) " + "INNER JOIN messages m ON favorites.message_id=m.message_id WHERE favorites.like_id=1 " + "AND NOT EXISTS (SELECT 1 FROM bl_users WHERE " + "(user_id = favorites.user_id AND bl_user_id = m.user_id) " - + "OR (user_id = m.user_id AND bl_user_id = favorites.user_id)) " + + "OR (user_id = m.user_id AND bl_user_id = favorites.user_id) " + + (visitorUid > 0 ? " OR (user_id = :visitorUid AND favorites.user_id = bl_user_id)" : "") + + ") " + "AND favorites.message_id IN (:mids)", - new MapSqlParameterSource("mids", mids), (rs, rowNum) -> { + new MapSqlParameterSource("mids", mids).addValue("visitorUid", visitorUid), (rs, rowNum) -> { User user = new User(); user.setUid(rs.getInt(2)); user.setName(rs.getString(3)); @@ -824,7 +826,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msgs.forEach(i -> i.setReactions(likes.get(i.getMid()))); msgs.sort(Comparator.comparing(item -> mids.indexOf(item.getMid()))); msgs.forEach(i -> i.setEntities(MessageUtils.getEntities(i))); - List<Pair<Integer, User>> allRecommendations = getMessagesRecommendations(mids); + List<Pair<Integer, User>> allRecommendations = getMessagesRecommendations(uid, mids); msgs.forEach(m -> { m.setRecommendations(new HashSet<>(allRecommendations.stream() .filter(r -> r.getLeft().equals(m.getMid())).map(Pair::getRight).toList())); diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 9a46f691..81718cab 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -1580,27 +1580,25 @@ public class ServerTests { User monstreek = userService.createUser("monstreek", "secret").orElseThrow(IllegalStateException::new); User pogo = userService.createUser("pogo", "secret").orElseThrow(IllegalStateException::new); User fmap = userService.createUser("fmap", "secret").orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(monstreek.getUid(), "KURWA", null, Set.of()); + int mid = messagesService.createMessage(fmap.getUid(), "KURWA", null, Set.of()); assertThat(messagesService.recommendMessage(mid, ermine.getUid()), is(MessagesService.RecommendStatus.Added)); - assertThat(messagesService.recommendMessage(mid, fmap.getUid()), - is(MessagesService.RecommendStatus.Added)); assertThat(messagesService.recommendMessage(mid, pogo.getUid()), is(MessagesService.RecommendStatus.Added)); jdbcTemplate.update("INSERT INTO favorites(user_id, user_uri, message_id, like_id, ts) " + "values (0, 'http://example.com/u/test', ?, 1, ?)", mid, Date.from(Instant.now())); - assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(4)); + assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(3)); assertThat(CollectionUtils.isEqualCollection( - messagesService.getMessagesRecommendations(Collections.singletonList(mid)).stream() + messagesService.getMessagesRecommendations(monstreek.getUid(), Collections.singletonList(mid)).stream() .map(Pair::getRight).map(User::getName).toList(), - Arrays.asList("fmap", "ermine", "pogo", "Anonymous")), is(true)); + Arrays.asList("ermine", "pogo", "Anonymous")), is(true)); privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo")); assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(3)); assertThat(CollectionUtils.isEqualCollection( - messagesService.getMessagesRecommendations(Collections.singletonList(mid)).stream() + messagesService.getMessagesRecommendations(monstreek.getUid(), Collections.singletonList(mid)).stream() .map(Pair::getRight).map(User::getName).toList(), - Arrays.asList("fmap", "ermine", "Anonymous")), is(true)); + Arrays.asList("ermine", "Anonymous")), is(true)); jdbcTemplate.execute("DELETE FROM favorites"); } |