aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-04-04 20:34:45 +0300
committerGravatar Vitaly Takmazov2019-04-04 20:34:45 +0300
commit0ea0026b4ac5a26b2312e329476a632548ad5fee (patch)
tree450e56545d02912ad4adce3eaa71c7a0aff8617e /src/main/java/com
parent55e6924b822417940f7aae2075acae3492684bbb (diff)
Recommendations in feeds
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/juick/server/api/Messages.java10
-rw-r--r--src/main/java/com/juick/server/www/controllers/MessagesWWW.java6
-rw-r--r--src/main/java/com/juick/service/MessagesService.java3
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java41
4 files changed, 35 insertions, 25 deletions
diff --git a/src/main/java/com/juick/server/api/Messages.java b/src/main/java/com/juick/server/api/Messages.java
index 5a4d8c31..60e05d6d 100644
--- a/src/main/java/com/juick/server/api/Messages.java
+++ b/src/main/java/com/juick/server/api/Messages.java
@@ -31,6 +31,7 @@ import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.Resource;
@@ -43,6 +44,7 @@ import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import java.io.IOException;
import java.util.*;
+import java.util.stream.Collectors;
/**
* @author ugnich
@@ -182,10 +184,10 @@ public class Messages {
throw new HttpNotFoundException();
}
msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser()));
- msg.setRecommendations(new HashSet<>(messagesService.getMessageRecommendations(msg.getMid())));
- msg.getRecommendations().forEach(r -> {
- r.setAvatar(webApp.getAvatarUrl(r));
- });
+ msg.setRecommendations(new HashSet<>(messagesService.getMessagesRecommendations(
+ Collections.singletonList(msg.getMid()))
+ .stream().map(Pair::getRight).collect(Collectors.toList())));
+ msg.getRecommendations().forEach(r -> r.setAvatar(webApp.getAvatarUrl(r)));
List<com.juick.Message> replies = messagesService.getReplies(visitor, mid);
replies.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
if (!visitor.isAnonymous()) {
diff --git a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
index 4410f591..5d82b686 100644
--- a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
+++ b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
@@ -29,6 +29,7 @@ import com.juick.service.*;
import com.juick.util.MessageUtils;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -47,6 +48,7 @@ import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -572,7 +574,9 @@ public class MessagesWWW {
model.addAttribute("headers", headers);
model.addAttribute("visitorSubscribed", messagesService.isSubscribed(visitor.getUid(), msg.getMid()));
model.addAttribute("visitorInBL", userService.isInBL(msg.getUser().getUid(), visitor.getUid()));
- model.addAttribute("recomm", messagesService.getMessageRecommendations(msg.getMid()));
+ model.addAttribute("recomm", messagesService.getMessagesRecommendations(
+ Collections.singletonList(msg.getMid())).stream()
+ .map(Pair::getRight).collect(Collectors.toList()));
List<Integer> blUIDs = new ArrayList<>();
for (Message reply : replies) {
if (reply.getUser().getUid() != msg.getUser().getUid()
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java
index 922170db..080319cc 100644
--- a/src/main/java/com/juick/service/MessagesService.java
+++ b/src/main/java/com/juick/service/MessagesService.java
@@ -21,6 +21,7 @@ import com.juick.Message;
import com.juick.Reaction;
import com.juick.User;
import com.juick.model.ResponseReply;
+import org.apache.commons.lang3.tuple.Pair;
import java.net.URI;
import java.util.*;
@@ -68,7 +69,7 @@ public interface MessagesService {
User getMessageAuthor(int mid);
- List<User> getMessageRecommendations(int mid);
+ List<Pair<Integer, User>> getMessagesRecommendations(Collection<Integer> mids);
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 7dbfe1dd..2ec78494 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -28,6 +28,8 @@ import com.juick.server.www.WebApp;
import com.juick.util.MessageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -50,15 +52,7 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -490,21 +484,23 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
- public List<User> getMessageRecommendations(final int mid) {
- return getJdbcTemplate().query(
- "SELECT DISTINCT users.id, users.nick, favorites.user_uri FROM favorites " +
- "INNER JOIN users ON (favorites.message_id = ? AND favorites.user_id = users.id) " +
+ public List<Pair<Integer, User>> getMessagesRecommendations(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)) " +
+ "AND favorites.message_id IN (:mids)",
+ new MapSqlParameterSource("mids", mids),
(rs, rowNum) -> {
User user = new User();
- user.setUid(rs.getInt(1));
- user.setName(rs.getString(2));
- user.setUri(URI.create(rs.getString(3)));
- return user;
- }, mid);
+ user.setUid(rs.getInt(2));
+ user.setName(rs.getString(3));
+ user.setUri(URI.create(rs.getString(4)));
+ return new ImmutablePair<>(rs.getInt(1), user);
+ });
}
@Transactional(readOnly = true)
@@ -916,6 +912,13 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msgs.forEach(i -> i.setEntities(MessageUtils.getEntities(i)));
+ List<Pair<Integer, User>> allRecommendations = getMessagesRecommendations(mids);
+
+ msgs.forEach(m -> {
+ m.setRecommendations(new HashSet<User>(allRecommendations.stream()
+ .filter(r -> r.getLeft().equals(m.getMid())).map(Pair::getRight).collect(Collectors.toList())));
+ });
+
return msgs;
}
return Collections.emptyList();