diff options
author | Vitaly Takmazov | 2017-05-05 17:42:27 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-05-23 15:50:10 +0300 |
commit | 237739950a305b13c9f29e3ca25c2e835d563a3f (patch) | |
tree | 14eea79fe63d0a5b9586aebdbaa9795ed858532e /juick-www/src/main/java | |
parent | cde3f26ef2779ec08335b1d2197151231d5cce3f (diff) |
User controller -> pebble
Diffstat (limited to 'juick-www/src/main/java')
-rw-r--r-- | juick-www/src/main/java/com/juick/www/controllers/User.java | 259 |
1 files changed, 96 insertions, 163 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/User.java b/juick-www/src/main/java/com/juick/www/controllers/User.java index bb58a739..6091f398 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/User.java +++ b/juick-www/src/main/java/com/juick/www/controllers/User.java @@ -17,8 +17,8 @@ */ package com.juick.www.controllers; -import com.juick.server.helpers.TagStats; import com.juick.server.util.HttpForbiddenException; +import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; @@ -29,18 +29,21 @@ import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponents; +import ru.sape.Sape; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -60,40 +63,38 @@ public class User { MessagesService messagesService; @Inject PageTemplates templates; + @Inject + private Sape sape; @GetMapping("/{uname}/") - protected void doGetBlog(HttpServletRequest request, HttpServletResponse response, - @PathVariable String uname, - @RequestParam(required = false, defaultValue = "0") Integer before) throws IOException { + protected String doGetBlog( + @RequestParam(required = false, name = "show") String paramShow, + @RequestParam(required = false, name = "tag") String paramTagStr, + @RequestParam(required = false, name = "search") String paramSearch, + @PathVariable String uname, + @RequestParam(required = false, defaultValue = "0") Integer before, + @CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie, + ModelMap model) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (user.isBanned()) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpForbiddenException(); } List<Integer> mids; - String paramShow = request.getParameter("show"); - com.juick.Tag paramTag = null; - String paramTagStr = request.getParameter("tag"); if (paramTagStr != null) { if (paramTagStr.length() < 64) { paramTag = tagService.getTag(paramTagStr, false); } if (paramTag == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } else if (!paramTag.getName().equals(paramTagStr)) { - String url = "/" + user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); - Utils.sendPermanentRedirect(response, url); - return; + String url = user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); + return "redirect:/" + url; } } - - - String paramSearch = request.getParameter("search"); if (paramSearch != null && paramSearch.length() > 64) { paramSearch = null; } @@ -126,80 +127,94 @@ public class User { title = "Фотографии " + user.getName(); mids = messagesService.getUserPhotos(user.getUid(), privacy, before); } else { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + - user.getName() + "\" href=\"//rss.juick.com/" + user.getName() + "/blog\"/>"; - if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { - head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; - } else if (before > 0 || paramShow != null) { - head += "<meta name=\"robots\" content=\"noindex\"/>"; + String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + + user.getName() + "\" href=\"//rss.juick.com/" + user.getName() + "/blog\"/>"; + if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { + head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; + } else if (before > 0 || paramShow != null) { + head += "<meta name=\"robots\" content=\"noindex\"/>"; + } + model.addAttribute("title", title); + model.addAttribute("headers", head); + model.addAttribute("visitor", visitor); + model.addAttribute("user", user); + model.addAttribute("noindex", paramShow == null && before == 0); + model.addAttribute("readonly", messagesService.isReadonly()); + model.addAttribute("isSubscribed", userService.isSubscribed(visitor.getUid(), user.getUid())); + model.addAttribute("isInBL", userService.isInBL(visitor.getUid(), user.getUid())); + model.addAttribute("isInBLAny", userService.isInBLAny(user.getUid(), visitor.getUid())); + model.addAttribute("statsIRead", userService.getStatsIRead(user.getUid())); + model.addAttribute("statsMyReaders", userService.getStatsMyReaders(user.getUid())); + model.addAttribute("statsMessages", userService.getStatsMessages(user.getUid())); + model.addAttribute("statsReplies", userService.getStatsReplies(user.getUid())); + model.addAttribute("iread", userService.getUserReadLeastPopular(user.getUid(), 8)); + model.addAttribute("tagStats", tagService.getUserTagStats(user.getUid()) + .stream().map(t -> t.getTag().getName()).collect(Collectors.toList())); + model.addAttribute("paramTag", paramTag); + List<com.juick.Message> msgs = messagesService.getMessages(mids); + + if (visitor.getUid() != 0) { + List<Integer> blUIDs = userService.checkBL(visitor.getUid(), + msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList())); + msgs.forEach(m -> m.ReadOnly |= blUIDs.contains(m.getUser().getUid())); + } + model.addAttribute("msgs", msgs); + model.addAttribute("tags", tagService.getUserTagStats(user.getUid()).stream() + .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).limit(20).collect(Collectors.toList())); + model.addAttribute("headers", head); + model.addAttribute("showAdv", + paramShow == null && before == 0 && paramSearch == null && visitor.getUid() == 0); + if (mids.size() >= 20) { + String nextpage = "?before=" + mids.get(mids.size() - 1); + if (paramShow != null) { + nextpage += "&show=" + paramShow; } - templates.pageHead(out, visitor, title, head); - templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); - - if (mids.size() > 0) { - out.println("<section id=\"content\">"); - - if (paramTag != null) { - out.println("<p class=\"page\"><a href=\"/tag/" + - URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "\">← Все записи с тегом <b>" + - StringEscapeUtils.escapeHtml4(paramTag.getName()) + "</b></a></p>"); - } - - templates.printMessages(out, user, mids, visitor); - - if (mids.size() >= 20) { - String nextpage = "?before=" + mids.get(mids.size() - 1); - if (paramShow != null) { - nextpage += "&show=" + paramShow; - } - if (paramTag != null) { - nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); - } - if (paramSearch != null) { - nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); - } - out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"); - } - - out.println("</section>"); + if (paramSearch != null) { + nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); } - templates.pageFooter(request, out, visitor, true); - templates.pageEnd(out); + String next = "<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"; + model.addAttribute("next", next); + } + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); + String queryString = builder.getQuery(); + String requestURI = builder.toUri().getPath(); + if (sape != null && visitor.getUid() == 0 && queryString == null) { + String links = sape.getPageLinks(requestURI, sapeCookie).render(); + model.addAttribute("links", links); } + model.addAttribute("isModerator", visitor.getUid() == 3694); + return "views/blog"; } @GetMapping("/{uname}/tags") - protected void doGetTags(HttpServletRequest request, HttpServletResponse response, - @PathVariable String uname) throws IOException { + protected String doGetTags(@PathVariable String uname, ModelMap model) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; - templates.pageHead(out, visitor, "Теги " + user.getName(), head); - templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); - - out.println("<section id=\"content\">"); - out.println("<p>" + pageUserTags(user, visitor, 0) + "</p>"); - out.println("</section>"); - - templates.pageFooter(request, out, visitor, false); - templates.pageEnd(out); - } + model.addAttribute("title", "Теги " + user.getName()); + model.addAttribute("headers", "<meta name=\"robots\" content=\"noindex,nofollow\"/>"); + model.addAttribute("visitor", visitor); + model.addAttribute("user", user); + model.addAttribute("readonly", messagesService.isReadonly()); + model.addAttribute("isSubscribed", userService.isSubscribed(visitor.getUid(), user.getUid())); + model.addAttribute("isInBL", userService.isInBL(visitor.getUid(), user.getUid())); + model.addAttribute("isInBLAny", userService.isInBLAny(user.getUid(), visitor.getUid())); + model.addAttribute("statsIRead", userService.getStatsIRead(user.getUid())); + model.addAttribute("statsMyReaders", userService.getStatsMyReaders(user.getUid())); + model.addAttribute("statsMessages", userService.getStatsMessages(user.getUid())); + model.addAttribute("statsReplies", userService.getStatsReplies(user.getUid())); + model.addAttribute("iread", userService.getUserReadLeastPopular(user.getUid(), 8)); + model.addAttribute("tagStats", tagService.getUserTagStats(user.getUid()) + .stream().map(t -> t.getTag().getName()).collect(Collectors.toList())); + + return "views/blog_tags"; } @GetMapping("/{uname}/friends") @@ -216,7 +231,7 @@ public class User { String head = "<meta name=\"robots\" content=\"noindex\"/>"; templates.pageHead(out, visitor, "Подписки " + user.getName(), head); templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); + //pageUserColumn(out, user, visitor); out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); @@ -253,7 +268,7 @@ public class User { String head = "<meta name=\"robots\" content=\"noindex\"/>"; templates.pageHead(out, visitor, "Читатели " + user.getName(), head); templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); + //pageUserColumn(out, user, visitor); out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); @@ -275,86 +290,4 @@ public class User { templates.pageEnd(out); } } - - public void pageUserColumn(PrintWriter out, com.juick.User user, com.juick.User visitor) { - out.println("<aside id=\"column\">"); - out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUid() + ".png\" alt=\"\"/>" + user.getName() + "</a></div>"); - if (visitor.getUid() > 0 && visitor.getUid() != user.getUid()) { - out.println(" <ul class=\"toolbar\">"); - if (userService.isSubscribed(visitor.getUid(), user.getUid())) { - out.println(" <li><a href=\"/post?body=U+%40" + user.getName() + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>"); - } else { - out.println(" <li><a href=\"/post?body=S+%40" + user.getName() + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>"); - } - if (userService.isInBL(visitor.getUid(), user.getUid())) { - out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>"); - } else { - out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>"); - } - if (!userService.isInBLAny(user.getUid(), visitor.getUid())) { - out.println(" <li><a href=\"/pm/sent?uname=" + user.getName() + "\" title=\"Написать приватное сообщение\"><div style=\"background-position: -112px 0\"></div></a></li>"); - } - out.println(" </ul>"); - } else { - out.println(" <hr/>"); - } - out.println(" <ul>"); - out.println(" <li><a href=\"./\">Блог</a></li>"); - out.println(" <li><a href=\"./?show=recomm\" rel=\"nofollow\">Рекомендации</a></li>"); - out.println(" <li><a href=\"./?show=photos\" rel=\"nofollow\">Фотографии</a></li>"); - out.println(" </ul>"); - out.println(" <hr/>"); - out.println(" <form action=\"./\">"); - out.println(" <p><input type=\"text\" name=\"search\" class=\"inp\" placeholder=\"Поиск\"/></p>"); - out.println(" </form>"); - out.println(" <p class=\"tags\">" + pageUserTags(user, visitor, 20) + "<a href=\"./tags\" rel=\"nofollow\">...</a></p>"); - out.println(" <hr/>"); - out.println(" <div id=\"ustats\"><ul>"); - out.println(" <li><a href=\"./friends\">Я читаю: " + userService.getStatsIRead(user.getUid()) + "</a></li>"); - out.println(" <li><a href=\"./readers\">Мои подписчики: " + userService.getStatsMyReaders(user.getUid()) + "</a></li>"); - out.println(" <li>Сообщений: " + userService.getStatsMessages(user.getUid()) + "</li>"); - out.println(" <li>Комментариев: " + userService.getStatsReplies(user.getUid()) + "</li>"); - out.println(" </ul>"); - - List<com.juick.User> iread = userService.getUserReadLeastPopular(user.getUid(), 8); - if (!iread.isEmpty()) { - out.println("<table class=\"iread\"><tr>"); - for (int i = 0; i < iread.size(); i++) { - if (i == 4) { - out.println("</tr><tr>"); - } - com.juick.User u = iread.get(i); - out.println("<td><a href=\"/" + u.getName() + "/\"><img src=\"//i.juick.com/a/" + u.getUid() + ".png\" alt=\"" + u.getName() + "\"/></a></td>"); - } - out.println("</tr></table>"); - } - - out.println(" </div>"); - out.println("</aside>"); - } - - public String pageUserTags(com.juick.User user, com.juick.User visitor, int cnt) { - List<TagStats> tags = tagService.getUserTagStats(user.getUid()).stream() - .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).collect(Collectors.toList()); - int maxUsageCnt = tags.stream().map(TagStats::getUsageCount).max(Comparator.naturalOrder()).orElse(0); - String ret = StringUtils.EMPTY; - int count = cnt > 0 ? Math.min(tags.size(), cnt) : tags.size(); - for (int i = 0; i < count; i++) { - String tag = StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()); - try { - tag = "<a href=\"./?tag=" + URLEncoder.encode(tags.get(i).getTag().getName(), CharEncoding.UTF_8) + "\" title=\"" - + tags.get(i).getUsageCount() + "\" rel=\"nofollow\">" + tag + "</a>"; - } catch (UnsupportedEncodingException e) { - } - - if (tags.get(i).getUsageCount() > maxUsageCnt / 3 * 2) { - ret += "<big>" + tag + "</big> "; - } else if (tags.get(i).getUsageCount() > maxUsageCnt / 3) { - ret += "<small>" + tag + "</small> "; - } else { - ret += tag + " "; - } - } - return ret; - } } |