aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
diff options
context:
space:
mode:
authorGravatar mykhailo.dubovskyi2018-04-18 15:57:53 +0300
committerGravatar Vitaly Takmazov2018-04-20 11:03:47 +0300
commit842e903d5b987dd23e82a423bddae75acdea0c9c (patch)
tree59eabfb9969b6cbe00703674f8d3ddb7d2e029e0 /juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
parentef1c674b0dc1979e46e970ad67ccc7a9bb67b001 (diff)
Like -> Reaction
Diffstat (limited to 'juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java')
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java72
1 files changed, 65 insertions, 7 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
index c0dccc6a..362a918c 100644
--- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -17,9 +17,7 @@
package com.juick.service;
-import com.juick.Message;
-import com.juick.Tag;
-import com.juick.User;
+import com.juick.*;
import com.juick.server.helpers.PrivacyOpts;
import com.juick.server.helpers.ResponseReply;
import com.juick.util.MessageUtils;
@@ -105,6 +103,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
}
}
+
+
/**
* @see <a href="https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html">Java, JDBC and MySQL Types</a>
*/
@@ -226,12 +226,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Override
public RecommendStatus recommendMessage(final int mid, final int vuid) {
int wasDeleted = getJdbcTemplate()
- .update("DELETE FROM favorites WHERE user_id=? AND message_id=?", vuid, mid);
+ .update("DELETE FROM favorites WHERE user_id=? AND message_id=? and like_id=?", vuid, mid, Reaction.LIKE);
if (wasDeleted > 0) {
return RecommendStatus.Deleted;
} else {
boolean wasAdded = getJdbcTemplate()
- .update("INSERT INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1;
+ .update("INSERT INTO favorites(user_id, message_id, ts, like_id ) VALUES (?, ?, NOW(), ?)", vuid, mid,Reaction.LIKE) == 1;
if (wasAdded) {
return RecommendStatus.Added;
}
@@ -239,6 +239,19 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
return RecommendStatus.Error;
}
+ @Override
+ public RecommendStatus likeMessage(int mid, int vuid, int reactionId) throws IllegalArgumentException {
+ if(reactionId == Reaction.LIKE) throw new IllegalArgumentException("this reaction type does not support here");
+
+ boolean wasAdded = getJdbcTemplate()
+ .update("INSERT INTO favorites(user_id, message_id, ts, like_id ) VALUES (?, ?, NOW(), ?)", vuid, mid, reactionId) == 1;
+ if (wasAdded) {
+ return RecommendStatus.Added;
+ }
+
+ return RecommendStatus.Error;
+ }
+
@Transactional(readOnly = true)
@Override
public boolean canViewThread(final int mid, final int uid) {
@@ -299,6 +312,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
public com.juick.Message getMessage(final int mid) {
+
List<com.juick.Message> list = getJdbcTemplate().query(
"SELECT messages.message_id as mid, 0 as rid, 0 as replyto, "
+ "messages.user_id as uid, users.nick, users.banned as banned, "
@@ -319,8 +333,13 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "privacy, replies, attach, place_id, lat, lon, tags, repliesby, q",
new MessageMapper(),
mid);
-
- return list.isEmpty() ? null : list.get(0);
+ if (!list.isEmpty()) {
+ final Message message = list.get(0);
+ Map<Integer, Set<Reaction>> reactionStats = updateReactionsFor(Collections.singletonList(mid));
+ message.setReactions(reactionStats.get(message.getMid()));
+ return message;
+ }
+ return null;
}
@Transactional(readOnly = true)
@@ -740,6 +759,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Override
public List<com.juick.Message> getMessages(final List<Integer> mids) {
if (CollectionUtils.isNotEmpty(mids)) {
+
List<com.juick.Message> msgs = getNamedParameterJdbcTemplate().query(
"SELECT messages.message_id, 0 as rid, 0 as replyto, "
+ "messages.user_id,users.nick, 0 as banned, "
@@ -758,12 +778,50 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "WHERE messages.message_id IN (:ids) GROUP BY messages.message_id",
new MapSqlParameterSource("ids", mids),
new MessageMapper());
+
+
+ Map<Integer,Set<Reaction>> likes = updateReactionsFor(mids);
+
+ msgs.forEach(i -> i.setReactions(likes.get(i.getMid())));
+
msgs.sort(Comparator.comparing(item -> mids.indexOf(item.getMid())));
+
return msgs;
}
return Collections.emptyList();
}
+
+ @Transactional(readOnly = true)
+ @Override
+ public Map<Integer,Set<Reaction>> updateReactionsFor(final List<Integer> mids) {
+
+ return getNamedParameterJdbcTemplate().query("select f.message_id as mid, f.like_id as lid, " +
+ "r.description as desc, count(f.like_id) as count" +
+ " from favorites f LEFT JOIN reactions r ON f.like_id = r.like_id " +
+ " where f.message_id IN (:mids) " +
+ " group by f.message_id, f.like_id", new MapSqlParameterSource("mids", mids), (ResultSet rs) -> {
+ Map<Integer,Set<Reaction>> results = new HashMap<>();
+
+
+ while (rs.next()) {
+ int messageId = rs.getInt("message_id");
+ int likeId = rs.getInt("like_id");
+ int count = rs.getInt("count");
+ String description = rs.getString("description");
+ Reaction reaction = new Reaction(likeId);
+ reaction.setCount(count);
+ reaction.setDescription(description);
+ results.computeIfAbsent(messageId, HashSet::new);
+ results.get(messageId).add(reaction);
+ }
+
+ return results;
+ });
+
+ }
+
+
@Transactional
@Override
public List<Message> getReplies(final User user, final int mid) {