From 97ffb90f765078aca3e4d6d173a6c93182d9165c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 12 Apr 2018 00:41:57 +0300 Subject: subscription mark all comments read --- juick-common/src/main/java/com/juick/server/CommandsManager.java | 7 ++++--- .../src/main/java/com/juick/service/SubscriptionService.java | 2 +- .../src/main/java/com/juick/service/SubscriptionServiceImpl.java | 8 +++++--- .../src/test/java/com/juick/server/tests/ServerTests.java | 6 +++--- juick-www/src/main/java/com/juick/www/controllers/NewMessage.java | 2 +- juick-www/src/test/java/com/juick/WebAppTests.java | 6 +++--- 6 files changed, 17 insertions(+), 14 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 734d68fb..cde6a4bf 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -102,7 +102,7 @@ public class CommandsManager { String body = TagUtils.toString(tags).equals(input) ? StringUtils.EMPTY : input.substring(TagUtils.toString(tags).length()).trim(); String attachmentType = StringUtils.isNotEmpty(attachment.toString()) ? attachment.toString().substring(attachment.toString().length() - 3) : null; int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags); - subscriptionService.subscribeMessage(mid, user.getUid()); + subscriptionService.subscribeMessage(messagesService.getMessage(mid), user); if (StringUtils.isNotEmpty(attachmentType)) { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); @@ -335,9 +335,10 @@ public class CommandsManager { public CommandResult commandSubscribeMessage(User user, URI attachment, String... args) { boolean subscribe = args[0].equalsIgnoreCase("s"); int mid = NumberUtils.toInt(args[1], 0); - if (messagesService.getMessage(mid) != null) { + Message msg = messagesService.getMessage(mid); + if (msg != null) { if (subscribe) { - if (subscriptionService.subscribeMessage(mid, user.getUid())) { + if (subscriptionService.subscribeMessage(msg, user)) { return CommandResult.fromString("Subscribed"); } } else { diff --git a/juick-common/src/main/java/com/juick/service/SubscriptionService.java b/juick-common/src/main/java/com/juick/service/SubscriptionService.java index 3bd58aed..d5b9f28d 100644 --- a/juick-common/src/main/java/com/juick/service/SubscriptionService.java +++ b/juick-common/src/main/java/com/juick/service/SubscriptionService.java @@ -36,7 +36,7 @@ public interface SubscriptionService { List getUsersSubscribedToUserRecommendations(int uid, int mid, int muid); - boolean subscribeMessage(int mid, int vuid); + boolean subscribeMessage(Message message, User user); boolean unSubscribeMessage(int mid, int vuid); diff --git a/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java index d580f97a..64143c32 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -140,10 +140,12 @@ public class SubscriptionServiceImpl extends BaseJdbcService implements Subscrip @Transactional @Override - public boolean subscribeMessage(final int mid, final int vuid) { + public boolean subscribeMessage(final Message message, final User user) { try { - return getJdbcTemplate().update( - "INSERT INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1; + boolean result = getJdbcTemplate().update( + "INSERT INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", user.getUid(), message.getMid()) == 1; + messagesService.setLastReadComment(user, message.getMid(), message.getReplies()); + return result; } catch (DuplicateKeyException e) { return true; } 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 a7701511..cdf10326 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 @@ -262,8 +262,8 @@ public class ServerTests { assertEquals("weather", msg3.getTags().get(0).getName()); assertEquals(ugnich.getUid(), userService.checkPassword(ugnich.getName(), "x")); assertEquals(-1, userService.checkPassword(ugnich.getName(), "xy")); - subscriptionService.subscribeMessage(msg.getMid(), user.getUid()); - subscriptionService.subscribeMessage(msg.getMid(), ugnich.getUid()); + subscriptionService.subscribeMessage(msg, user); + subscriptionService.subscribeMessage(msg, ugnich); int reply_id = messagesService.createReply(msg.getMid(), 0, ugnich, "comment", null); assertEquals(1, subscriptionService.getUsersSubscribedToComments(msg, messagesService.getReply(msg.getMid(), reply_id)).size()); @@ -751,7 +751,7 @@ public class ServerTests { @Test public void lastReadTests() { int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null); - subscriptionService.subscribeMessage(mid, ugnich.getUid()); + subscriptionService.subscribeMessage(messagesService.getMessage(mid), ugnich); messagesService.createReply(mid, 0, freefd, "new reply", null); BiFunction lastRead = (user, m) -> jdbcTemplate.queryForObject( "SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?", diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 6b97c7c4..c26d7c0c 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -148,7 +148,7 @@ public class NewMessage { String attachmentType = StringUtils.isNotEmpty(attachmentFName.toString()) ? attachmentFName.toString().substring(attachmentFName.toString().length() - 3) : null; int ridnew = messagesService.createReply(mid, rid, visitor, body, attachmentType); - subscriptionService.subscribeMessage(mid, visitor.getUid()); + subscriptionService.subscribeMessage(msg, visitor); Message xmsg = new Message(); xmsg.setFrom(botJid); diff --git a/juick-www/src/test/java/com/juick/WebAppTests.java b/juick-www/src/test/java/com/juick/WebAppTests.java index 37798d08..4f9f4cc1 100644 --- a/juick-www/src/test/java/com/juick/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/WebAppTests.java @@ -336,10 +336,10 @@ public class WebAppTests { String discussionsUrl = "http://localhost:8080/?show=discuss"; HtmlPage discussions = webClient.getPage(discussionsUrl); assertThat(discussions.querySelectorAll("article").size(), is(0)); - subscriptionService.subscribeMessage(mid, freefd.getUid()); + subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd); discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(1)); - subscriptionService.subscribeMessage(midNew, freefd.getUid()); + subscriptionService.subscribeMessage(messagesService.getMessage(midNew), freefd); discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(2)); assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); @@ -353,7 +353,7 @@ public class WebAppTests { assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); for (Integer m : newMids) { - subscriptionService.subscribeMessage(m, freefd.getUid()); + subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd); } discussions = (HtmlPage) discussions.refresh(); assertThat(discussions.querySelectorAll("article").size(), is(20)); -- cgit v1.2.3