diff options
author | Vitaly Takmazov | 2022-06-25 01:14:09 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2022-06-25 01:14:09 +0300 |
commit | 8f3d85a8afa851f4457163d3aade01c1f63075a0 (patch) | |
tree | b1ea8dd6a3c4335b96136604cb5acf9e3f4f4315 | |
parent | 4ebc9ab55553a33ef7c3bedb8628ffaf74333f84 (diff) |
Fix recommendations counters in html templates
* deprecate `likes` message field, use `recommendations` where possible
-rw-r--r-- | pom.xml | 5 | ||||
-rw-r--r-- | src/main/java/com/juick/model/Message.java | 1 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 26 | ||||
-rw-r--r-- | src/main/resources/templates/views/partial/message.html | 8 | ||||
-rw-r--r-- | src/main/resources/templates/views/thread.html | 10 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 16 |
6 files changed, 40 insertions, 26 deletions
@@ -79,6 +79,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-devtools</artifactId> + <optional>true</optional> + </dependency> + <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <scope>test</scope> diff --git a/src/main/java/com/juick/model/Message.java b/src/main/java/com/juick/model/Message.java index 5ad4fb26..0ae756b1 100644 --- a/src/main/java/com/juick/model/Message.java +++ b/src/main/java/com/juick/model/Message.java @@ -258,6 +258,7 @@ public class Message implements Comparable<Message>, Serializable { } @XmlTransient + @Deprecated public int getLikes() { return likes; } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 4cbd09ee..77200077 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -394,6 +394,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Map<Integer, Set<Reaction>> reactionStats = updateReactionsFor(Collections.singletonList(mid)); message.setReactions(reactionStats.get(message.getMid())); message.setEntities(MessageUtils.getEntities(message)); + List<Pair<Integer, User>> allRecommendations = getMessagesRecommendations(List.of(mid)); + + message.setRecommendations(new HashSet<>(allRecommendations.stream() + .filter(r -> r.getLeft().equals(mid)).map(Pair::getRight).collect(Collectors.toList()))); + message.getRecommendations().forEach(r -> r.setAvatar(webApp.getAvatarUrl(r))); return Optional.of(message); } return Optional.empty(); @@ -703,21 +708,24 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List<Integer> getUserBlogWithRecommendations(final User user, final User visitor, final int privacy, final int before) { + public List<Integer> getUserBlogWithRecommendations(final User user, final User visitor, final int privacy, + final int before) { SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", user.getUid()) - .addValue("vid", visitor.getUid()) - .addValue("privacy", privacy).addValue("before", before); + .addValue("uid", user.getUid()) + .addValue("vid", visitor.getUid()) + .addValue("privacy", privacy).addValue("before", before); return getNamedParameterJdbcTemplate() - .queryForList("SELECT message_id FROM " + "(SELECT favorites.message_id FROM favorites " + .queryForList("SELECT message_id FROM " + "(SELECT favorites.message_id FROM favorites " + " INNER JOIN messages ON messages.message_id = favorites.message_id" - + " INNER JOIN users ON messages.user_id = users.id" + " WHERE favorites.user_id = :uid AND users.banned = 0" + + " INNER JOIN users ON messages.user_id = users.id" + + " WHERE favorites.user_id = :uid AND users.banned = 0" + " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = messages.user_id) " + " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " + "(SELECT tag_id FROM messages_tags WHERE message_id = favorites.message_id) and :vid = bt.user_id)" + (before > 0 ? " AND messages.message_id < :before " : StringUtils.EMPTY) - + " ORDER BY messages.message_id DESC LIMIT 20) as r" + " UNION ALL " + "SELECT message_id FROM " + + " ORDER BY messages.message_id DESC LIMIT 20) as r" + " UNION ALL " + + "SELECT message_id FROM " + "(SELECT message_id FROM messages" + " INNER JOIN users" + " ON messages.user_id = users.id" + " WHERE user_id = :uid AND users.banned = 0" + " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = messages.user_id) " @@ -945,8 +953,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } else { // only archive author replies return getNamedParameterJdbcTemplate().update( - "UPDATE replies SET user_id=:archive_uid WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid", - parameters) > 0; + "UPDATE replies SET user_id=:archive_uid WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid", + parameters) > 0; } } return false; diff --git a/src/main/resources/templates/views/partial/message.html b/src/main/resources/templates/views/partial/message.html index 47dfb6c5..a41a3064 100644 --- a/src/main/resources/templates/views/partial/message.html +++ b/src/main/resources/templates/views/partial/message.html @@ -37,7 +37,7 @@ <a href="/{{ msg.mid }}" class="a-like msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.likes > 0 %} {{ msg.likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} @@ -47,7 +47,7 @@ <a href="/post?body=!+%23{{ msg.mid }}" class="a-like msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.likes > 0 %} {{ msg.likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} @@ -57,8 +57,8 @@ <a href="/login" class="a-login msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.likes > 0 %} - {{ msg.likes }} + {% if msg.recommendations.size() > 0 %} + {{ msg.recommendations.size() }} {% else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} diff --git a/src/main/resources/templates/views/thread.html b/src/main/resources/templates/views/thread.html index f1563a7c..6215aa9a 100644 --- a/src/main/resources/templates/views/thread.html +++ b/src/main/resources/templates/views/thread.html @@ -44,7 +44,7 @@ <a href="/{{ msg.mid }}" class="a-like msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.Likes > 0 %} {{ msg.Likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} @@ -54,7 +54,7 @@ <a href="/post?body=!+%23{{ msg.mid }}" class="a-like msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.Likes > 0 %} {{ msg.Likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} @@ -64,7 +64,7 @@ <a href="/login" class="a-login msg-button"> <span class="msg-button-icon"> <i data-icon="ei-heart" data-size="s"></i> - {% if msg.Likes > 0 %} {{ msg.Likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {%else %} <span> {{ i18n("messages","message.recommend") }}</span> {% endif %} @@ -111,8 +111,8 @@ <a href="{{ rec.uri }}" data-user-uri="1">@{{ rec.name }}</a>{% if loop.index < (loop.length - 1) %}, {% endif %} {% endif %} {% endfor %} - {% if msg.likes > recomm.size() %} - {{ i18n("messages","message.recommendedOthers", msg.likes - recomm.size()) }} + {% if msg.recommendations.size() > recomm.size() %} + {{ i18n("messages","message.recommendedOthers", msg.recommendations.size() - recomm.size()) }} {% endif %} </div> {% endif %} diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 36483ce8..d5db7269 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -410,10 +410,10 @@ public class ServerTests { assertThat(messagesService.getMyFeed(freefd.getUid(), mid2, true).get(0), equalTo(mid0)); assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added)); - assertThat(messagesService.getMessage(mid0).orElseThrow(IllegalStateException::new).getLikes(), equalTo(1)); + assertThat(messagesService.getMessage(mid0).orElseThrow(IllegalStateException::new).getRecommendations().size(), equalTo(1)); assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted)); - assertThat(messagesService.getMessage(mid0).get().getLikes(), equalTo(0)); + assertThat(messagesService.getMessage(mid0).get().getRecommendations().size(), equalTo(0)); assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3)); Tag yoTag = tagService.getTag("yoyo", true); assertThat(tagService.getTag("YOYO", false), equalTo(yoTag)); @@ -540,7 +540,7 @@ public class ServerTests { messagesService.likeMessage(mid, freefdId, 1); Message msg4 = messagesService.getMessage(mid).get(); - assertThat(msg4.getLikes(), equalTo(1)); + assertThat(msg4.getRecommendations().size(), equalTo(1)); assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2).findFirst() .orElseThrow(IllegalStateException::new).getCount()); @@ -1048,9 +1048,9 @@ public class ServerTests { .andExpect(status().isOk()); Message msg4 = messagesService.getMessage(mid1).get(); - assertThat(msg4.getLikes(), is(0)); + assertThat(msg4.getRecommendations().size(), is(0)); assertThat( - messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), + messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getRecommendations().size(), is(0)); assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2).findFirst() .orElseThrow(IllegalStateException::new).getCount()); @@ -1058,7 +1058,7 @@ public class ServerTests { .andExpect(status().isOk()); mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash + "&reactionId=1")) .andExpect(status().isOk()); - assertThat(messagesService.getMessage(mid1).get().getLikes(), is(1)); + assertThat(messagesService.getMessage(mid1).get().getRecommendations().size(), is(1)); } @Test @@ -1575,13 +1575,13 @@ public class ServerTests { 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, now())", mid); - assertThat(messagesService.getMessage(mid).get().getLikes(), is(4)); + assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(4)); assertThat(CollectionUtils.isEqualCollection( messagesService.getMessagesRecommendations(Collections.singletonList(mid)).stream() .map(p -> p.getRight()).map(User::getName).collect(Collectors.toList()), Arrays.asList("fmap", "ermine", "pogo", "Anonymous")), is(true)); privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo")); - assertThat(messagesService.getMessage(mid).get().getLikes(), is(4)); + assertThat(messagesService.getMessage(mid).get().getRecommendations().size(), is(4)); assertThat(CollectionUtils.isEqualCollection( messagesService.getMessagesRecommendations(Collections.singletonList(mid)).stream() .map(p -> p.getRight()).map(User::getName).collect(Collectors.toList()), |