aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-08-03 12:38:18 +0300
committerGravatar Vitaly Takmazov2018-08-03 12:38:18 +0300
commit162262eb58c4642b7ffb9ed41bc760e0f009d6a6 (patch)
treeb72b3b1807c561c61c3c40c27f07ba3e012acd73
parent798e8da8737c4fe5fee8dfaa7f98bfa13e2d91da (diff)
replies counter in feeds reflect banned comments
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java14
-rw-r--r--juick-common/src/main/java/com/juick/service/MessagesService.java2
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Messages.java6
-rw-r--r--juick-server/src/main/java/com/juick/server/api/rss/Feeds.java5
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java20
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java18
6 files changed, 40 insertions, 25 deletions
diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java
index 7dd372b1..d3b85bef 100644
--- a/juick-common/src/main/java/com/juick/server/CommandsManager.java
+++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java
@@ -198,7 +198,7 @@ public class CommandsManager {
@UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages")
public CommandResult commandLast(User user_from, URI attachment, String... arguments) {
return CommandResult.fromString("Last messages:\n"
- + printMessages(messagesService.getAll(user_from.getUid(), 0), true));
+ + printMessages(user_from, messagesService.getAll(user_from.getUid(), 0), true));
}
@UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
@@ -279,7 +279,7 @@ public class CommandsManager {
public CommandResult commandFavorites(User currentUser, URI attachment, String... args) {
List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
if (mids.size() > 0) {
- return CommandResult.fromString("Favorite messages: \n" + printMessages(mids, false));
+ return CommandResult.fromString("Favorite messages: \n" + printMessages(currentUser, mids, false));
}
return CommandResult.fromString("No favorite messages, try to \"like\" something ;)");
}
@@ -393,7 +393,7 @@ public class CommandsManager {
if (!blogUser.isAnonymous()) {
List<Integer> mids = messagesService.getUserBlog(blogUser.getUid(), 0, 0);
return CommandResult.fromString(String.format("Last messages from @%s:\n%s", arguments[0],
- printMessages(mids, false)));
+ printMessages(user, mids, false)));
}
return CommandResult.fromString("User not found");
}
@@ -437,7 +437,7 @@ public class CommandsManager {
if (tag != null) {
// TODO: synonyms
List<Integer> mids = messagesService.getTag(tag.TID, user.getUid(), 0, 10);
- return CommandResult.fromString("Last messages with *" + tag.getName() + ":\n" + printMessages(mids, true));
+ return CommandResult.fromString("Last messages with *" + tag.getName() + ":\n" + printMessages(user, mids, true));
}
return CommandResult.fromString("Tag not found");
}
@@ -477,7 +477,7 @@ public class CommandsManager {
int page = arguments[0].length() - 1;
List<Integer> mids = messagesService.getMyFeed(user.getUid(), page, false);
if (mids.size() > 0) {
- return CommandResult.fromString("Your feed: \n" + printMessages(mids, true));
+ return CommandResult.fromString("Your feed: \n" + printMessages(user, mids, true));
}
return CommandResult.fromString("Your feed is empty");
}
@@ -523,8 +523,8 @@ public class CommandsManager {
}
}
- String printMessages(List<Integer> mids, boolean crop) {
- return messagesService.getMessages(mids).stream()
+ String printMessages(User visitor, List<Integer> mids, boolean crop) {
+ return messagesService.getMessages(visitor, mids).stream()
.sorted(Collections.reverseOrder())
.map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n"));
}
diff --git a/juick-common/src/main/java/com/juick/service/MessagesService.java b/juick-common/src/main/java/com/juick/service/MessagesService.java
index 8e69872a..28f59153 100644
--- a/juick-common/src/main/java/com/juick/service/MessagesService.java
+++ b/juick-common/src/main/java/com/juick/service/MessagesService.java
@@ -101,7 +101,7 @@ public interface MessagesService {
List<Integer> getUserSearch(int UID, String search, int privacy, int page);
- List<com.juick.Message> getMessages(List<Integer> mids);
+ List<com.juick.Message> getMessages(User visitor, List<Integer> mids);
Map<Integer,Set<Reaction>> updateReactionsFor(final List<Integer> mid);
diff --git a/juick-server/src/main/java/com/juick/server/api/Messages.java b/juick-server/src/main/java/com/juick/server/api/Messages.java
index eb562103..d7c07391 100644
--- a/juick-server/src/main/java/com/juick/server/api/Messages.java
+++ b/juick-server/src/main/java/com/juick/server/api/Messages.java
@@ -76,7 +76,7 @@ public class Messages {
if (!visitor.isAnonymous()) {
int vuid = visitor.getUid();
List<Integer> mids = messagesService.getMyFeed(vuid, before_mid, true);
- return ResponseEntity.ok(messagesService.getMessages(mids));
+ return ResponseEntity.ok(messagesService.getMessages(visitor, mids));
}
return FORBIDDEN;
}
@@ -138,7 +138,7 @@ public class Messages {
mids = messagesService.getAll(visitor.getUid(), before);
}
}
- return ResponseEntity.ok(messagesService.getMessages(mids));
+ return ResponseEntity.ok(messagesService.getMessages(visitor, mids));
}
@DeleteMapping("/messages")
public CommandResult deleteMessage(@RequestParam int mid, @RequestParam(required = false, defaultValue = "0") int rid) {
@@ -156,7 +156,7 @@ public class Messages {
@GetMapping("/messages/discussions")
public List<Message> getDiscussions(
@RequestParam(required = false, defaultValue = "0") Long to) {
- return messagesService.getMessages(messagesService.getDiscussions(UserUtils.getCurrentUser().getUid(), to));
+ return messagesService.getMessages(UserUtils.getCurrentUser(), messagesService.getDiscussions(UserUtils.getCurrentUser().getUid(), to));
}
@GetMapping("/thread")
public ResponseEntity<List<com.juick.Message>> getThread(
diff --git a/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java
index d2d112d8..ebf5b04e 100644
--- a/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java
+++ b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java
@@ -19,6 +19,7 @@ package com.juick.server.api.rss;
import com.juick.User;
import com.juick.server.util.HttpBadRequestException;
+import com.juick.server.util.UserUtils;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import org.springframework.stereotype.Controller;
@@ -50,7 +51,7 @@ public class Feeds {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("messagesView");
modelAndView.addObject("user", user);
- modelAndView.addObject("messages", messagesService.getMessages(mids));
+ modelAndView.addObject("messages", messagesService.getMessages(UserUtils.getCurrentUser(), mids));
return modelAndView;
}
throw new HttpBadRequestException();
@@ -61,7 +62,7 @@ public class Feeds {
List<Integer> mids = messagesService.getLastMessages(hours);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("messagesView");
- modelAndView.addObject("messages", messagesService.getMessages(mids));
+ modelAndView.addObject("messages", messagesService.getMessages(UserUtils.getCurrentUser(),mids));
return modelAndView;
}
@RequestMapping(value = "/rss/comments", method = RequestMethod.GET)
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index 75493377..f79e4142 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -754,14 +754,20 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
- public List<com.juick.Message> getMessages(final List<Integer> mids) {
+ public List<com.juick.Message> getMessages(final User visitor, 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, "
+ "WITH RECURSIVE banned(message_id, reply_id) "
+ + "AS (SELECT message_id, reply_id FROM replies WHERE replies.message_id IN (:ids) "
+ + "AND EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) "
+ + "UNION ALL SELECT replies.message_id, replies.reply_id FROM replies INNER JOIN banned "
+ + "ON banned.reply_id = replies.replyto AND banned.message_id=replies.message_id "
+ + "WHERE replies.message_id IN (:ids)) "
+ + "SELECT messages.message_id, 0 as rid, 0 as replyto, "
+ "messages.user_id,users.nick, 0 as banned, "
+ "messages.ts,"
- + "messages.readonly,messages.privacy,messages.replies,"
+ + "messages.readonly,messages.privacy, messages.replies-COUNT(DISTINCT banned.reply_id) as replies,"
+ "messages.attach,COUNT(DISTINCT favorites.user_id) AS likes,messages.hidden,"
+ "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q, "
+ "messages.updated, 0 as to_uid, NULL as to_name "
@@ -770,8 +776,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
+ "INNER JOIN users ON messages.user_id=users.id "
+ "LEFT JOIN favorites "
+ "ON messages.message_id = favorites.message_id AND favorites.like_id=1 "
- + "WHERE messages.message_id IN (:ids) GROUP BY messages.message_id",
- new MapSqlParameterSource("ids", mids),
+ + "LEFT JOIN banned "
+ + "ON messages.message_id = banned.message_id "
+ + "WHERE messages.message_id IN (:ids) GROUP BY "
+ + "messages.message_id, rid, replyto, messages.user_id, users.nick, banned, messages.ts, messages.readonly, messages.privacy, messages.attach, messages.hidden, messages_txt.tags, messages_txt.repliesby, messages_txt.txt, q, messages.updated, to_uid, to_name",
+ new MapSqlParameterSource("ids", mids)
+ .addValue("uid", visitor.getUid()),
new MessageMapper());
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index c0141b2c..b468daff 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -878,7 +878,7 @@ public class ServerTests {
Message msg4 = messagesService.getMessage(mid1);
assertThat(msg4.getLikes(), is(0));
- assertThat(messagesService.getMessages(Collections.singletonList(mid1)).get(0).getLikes(), is(0));
+ assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), is(0));
Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2)
.findFirst().orElseThrow(IllegalStateException::new).getCount());
mockMvc.perform(post("/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1"))
@@ -931,18 +931,18 @@ public class ServerTests {
int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg",
Collections.singletonList(banned));
privacyQueriesService.blacklistTag(freefd, banned);
- assertTrue(messagesService.getMessages(messagesService.getAll(freefd.getUid(), 0))
+ assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(freefd.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
- assertFalse(messagesService.getMessages(messagesService.getAll(ugnich.getUid(), 0))
+ assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(ugnich.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
- assertTrue(messagesService.getMessages(messagesService.getPhotos(freefd.getUid(), 0))
+ assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(freefd.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
- assertFalse(messagesService.getMessages(messagesService.getPhotos(ugnich.getUid(), 0))
+ assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(ugnich.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", mid);
- assertTrue(messagesService.getMessages(messagesService.getPopular(freefd.getUid(), 0))
+ assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(freefd.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
- assertFalse(messagesService.getMessages(messagesService.getPopular(ugnich.getUid(), 0))
+ assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(ugnich.getUid(), 0))
.stream().noneMatch(m -> m.getTags().contains(banned)));
}
@@ -1066,9 +1066,13 @@ public class ServerTests {
reply = messagesService.getReply(mid, 5);
assertThat(userService.isReplyToBL(ugnich, reply), is(true));
assertThat(replies.size(), is(1));
+ List<Message> msgs = messagesService.getMessages(ugnich, Collections.singletonList(mid));
+ assertThat(msgs.get(0).getReplies(), is(1));
commandsManager.processCommand(ugnich, "BL @freefd", emptyUri);
messagesService.setRead(ugnich, mid);
assertThat(messagesService.getReplies(ugnich, mid).size(), is(5));
+ List<Message> nonblmsgs = messagesService.getMessages(ugnich, Collections.singletonList(mid));
+ assertThat(nonblmsgs.get(0).getReplies(), is(5));
}
@Test
public void cmykJpegShouldBeProcessedCorrectly() throws Exception {