diff options
author | Vitaly Takmazov | 2018-03-01 19:18:03 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-03-01 19:49:10 +0300 |
commit | 70a50b2252ed313e3aee1a9ee2f33be8a46cb028 (patch) | |
tree | cf1f15f826ef21f2f431e242b9db50b9bcbf3dbf /juick-www/src | |
parent | f92fb49bfab851d6de2480cf0359920516bfe050 (diff) |
sort discussions by last replay date
Diffstat (limited to 'juick-www/src')
-rw-r--r-- | juick-www/src/main/java/com/juick/www/controllers/Messages.java | 8 | ||||
-rw-r--r-- | juick-www/src/test/java/com/juick/www/WebAppTests.java | 61 |
2 files changed, 62 insertions, 7 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/Messages.java b/juick-www/src/main/java/com/juick/www/controllers/Messages.java index e74f7486..c83bb356 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Messages.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Messages.java @@ -41,6 +41,9 @@ import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.URLEncoder; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; import java.util.function.BooleanSupplier; @@ -117,6 +120,7 @@ public class Messages { @RequestParam(name = "show", required = false) String paramShow, @RequestParam(name = "search", required = false) String paramSearch, @RequestParam(name = "before", required = false, defaultValue = "0") Integer paramBefore, + @RequestParam(name = "to", required = false, defaultValue = "0") Long paramTo, @CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie, ModelMap model) throws IOException { if (tag != null) { @@ -156,7 +160,7 @@ public class Messages { mids = messagesService.getPrivate(visitor.getUid(), paramBefore); } else if (paramShow.equals("discuss") && !visitor.isAnonymous()) { title = "Обсуждения"; - mids = messagesService.getDiscussions(visitor.getUid(), paramBefore); + mids = messagesService.getDiscussions(visitor.getUid(), paramTo); } else if (paramShow.equals("recommended") && !visitor.isAnonymous()) { title = "Рекомендации"; mids = messagesService.getRecommended(visitor.getUid(), paramBefore); @@ -194,7 +198,7 @@ public class Messages { model.addAttribute("showAdv", paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); if (mids.size() >= 20) { - String nextpage = "?before=" + mids.get(mids.size() - 1); + String nextpage = (paramShow != null && paramShow.equals("discuss")) ? "?to=" + msgs.get(msgs.size() - 1).getUpdated().toEpochMilli() : "?before=" + mids.get(mids.size() - 1); if (paramShow != null) { nextpage += "&show=" + paramShow; } diff --git a/juick-www/src/test/java/com/juick/www/WebAppTests.java b/juick-www/src/test/java/com/juick/www/WebAppTests.java index af6b3f89..9d822d77 100644 --- a/juick-www/src/test/java/com/juick/www/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java @@ -28,11 +28,7 @@ import com.juick.Tag; import com.juick.User; import com.juick.configuration.DataConfiguration; import com.juick.configuration.RepositoryConfiguration; -import com.juick.service.ImagesService; -import com.juick.service.MessagesService; -import com.juick.service.MockImagesService; -import com.juick.service.PrivacyQueriesService; -import com.juick.service.UserService; +import com.juick.service.*; import com.juick.util.MessageUtils; import com.juick.www.configuration.SapeConfiguration; import com.juick.www.configuration.WebSecurityConfig; @@ -42,6 +38,7 @@ import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.apache.commons.text.StringEscapeUtils; +import org.eclipse.jetty.websocket.common.message.MessageAppender; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -72,6 +69,8 @@ import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.StreamSupport; @@ -119,6 +118,8 @@ public class WebAppTests { private PrivacyQueriesService privacyQueriesService; @Inject private JdbcTemplate jdbcTemplate; + @Inject + private SubscriptionService subscriptionService; @Inject private PebbleEngine pebbleEngine; @@ -333,4 +334,54 @@ public class WebAppTests { public void nonExistentBlogShouldReturn404() throws Exception { mockMvc.perform(get("/ololoe/")).andExpect(status().isNotFound()); } + @Test + public void discussionsShouldBePageableByTimestamp() throws Exception { + String msgText = "Привет, я снова Угнич"; + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); + MvcResult loginResult = mockMvc.perform(post("/login") + .param("username", freefdName) + .param("password", freefdPassword)).andReturn(); + Cookie loginCookie = loginResult.getResponse().getCookie("juick-remember-me"); + webClient.setCookieManager(new CookieManager()); + webClient.getCookieManager().addCookie( + new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), + loginCookie.getName(), + loginCookie.getValue())); + String discussionsUrl = "http://localhost:8080/?show=discuss"; + HtmlPage discussions = webClient.getPage(discussionsUrl); + assertThat(discussions.querySelectorAll("article").size(), is(0)); + subscriptionService.subscribeMessage(mid, freefd.getUid()); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(1)); + subscriptionService.subscribeMessage(midNew, freefd.getUid()); + 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))); + messagesService.createReply(mid, 0, freefd.getUid(), "I'm replied", null); + 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(mid))); + Message msg = messagesService.getMessage(mid); + HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli()); + assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); + assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); + List<Integer> 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()); + } + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article").size(), is(20)); + assertThat(discussions.querySelectorAll("article") + .get(19).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); + messagesService.createReply(midNew, 0, freefd.getUid(), "I'm replied", null); + discussions = (HtmlPage) discussions.refresh(); + assertThat(discussions.querySelectorAll("article") + .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); + Message old = messagesService.getMessage(newMids.get(0)); + discussionsOld = webClient.getPage(discussionsUrl + "&to=" + old.getUpdated().toEpochMilli()); + assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); + assertThat(discussionsOld.querySelectorAll("article") + .get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid))); + } } |