From 8f3d85a8afa851f4457163d3aade01c1f63075a0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 25 Jun 2022 01:14:09 +0300 Subject: Fix recommendations counters in html templates * deprecate `likes` message field, use `recommendations` where possible --- src/main/java/com/juick/model/Message.java | 1 + .../com/juick/service/MessagesServiceImpl.java | 26 ++++++++++++++-------- .../resources/templates/views/partial/message.html | 8 +++---- src/main/resources/templates/views/thread.html | 10 ++++----- 4 files changed, 27 insertions(+), 18 deletions(-) (limited to 'src/main') 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, 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> reactionStats = updateReactionsFor(Collections.singletonList(mid)); message.setReactions(reactionStats.get(message.getMid())); message.setEntities(MessageUtils.getEntities(message)); + List> 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 getUserBlogWithRecommendations(final User user, final User visitor, final int privacy, final int before) { + public List 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 @@ - {% if msg.likes > 0 %} {{ msg.likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %}  {{ i18n("messages","message.recommend") }} {% endif %} @@ -47,7 +47,7 @@ - {% if msg.likes > 0 %} {{ msg.likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %}  {{ i18n("messages","message.recommend") }} {% endif %} @@ -57,8 +57,8 @@ - {% if msg.Likes > 0 %} {{ msg.Likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %}  {{ i18n("messages","message.recommend") }} {% endif %} @@ -54,7 +54,7 @@ - {% if msg.Likes > 0 %} {{ msg.Likes }} + {% if msg.recommendations.size() > 0 %} {{ msg.recommendations.size() }} {% else %}  {{ i18n("messages","message.recommend") }} {% endif %} @@ -64,7 +64,7 @@ @{{ rec.name }}{% 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 %} {% endif %} -- cgit v1.2.3