From 237739950a305b13c9f29e3ca25c2e835d563a3f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 5 May 2017 17:42:27 +0300 Subject: User controller -> pebble --- .../main/java/com/juick/www/controllers/User.java | 259 ++++++++------------- 1 file changed, 96 insertions(+), 163 deletions(-) (limited to 'juick-www/src/main/java') 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 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 = ""; - if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { - head += ""; - } else if (before > 0 || paramShow != null) { - head += ""; + String head = ""; + if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { + head += ""; + } else if (before > 0 || paramShow != null) { + head += ""; + } + 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 msgs = messagesService.getMessages(mids); + + if (visitor.getUid() != 0) { + List 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("
"); - - if (paramTag != null) { - out.println("

← Все записи с тегом " + - StringEscapeUtils.escapeHtml4(paramTag.getName()) + "

"); - } - - 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("

Читать дальше →

"); - } - - out.println("
"); + if (paramSearch != null) { + nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); } - templates.pageFooter(request, out, visitor, true); - templates.pageEnd(out); + String next = "

Читать дальше →

"; + 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 = ""; - templates.pageHead(out, visitor, "Теги " + user.getName(), head); - templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); - - out.println("
"); - out.println("

" + pageUserTags(user, visitor, 0) + "

"); - out.println("
"); - - templates.pageFooter(request, out, visitor, false); - templates.pageEnd(out); - } + model.addAttribute("title", "Теги " + user.getName()); + model.addAttribute("headers", ""); + 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 = ""; templates.pageHead(out, visitor, "Подписки " + user.getName(), head); templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); + //pageUserColumn(out, user, visitor); out.println("
"); out.println(""); @@ -253,7 +268,7 @@ public class User { String head = ""; templates.pageHead(out, visitor, "Читатели " + user.getName(), head); templates.pageNavigation(out, visitor, null); - pageUserColumn(out, user, visitor); + //pageUserColumn(out, user, visitor); out.println("
"); out.println("
"); @@ -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("
"); - for (int i = 0; i < iread.size(); i++) { - if (i == 4) { - out.println(""); - } - com.juick.User u = iread.get(i); - out.println(""); - } - out.println("
\""
"); - } - - out.println(" "); - out.println(""); - } - - public String pageUserTags(com.juick.User user, com.juick.User visitor, int cnt) { - List 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 = "" + tag + ""; - } catch (UnsupportedEncodingException e) { - } - - if (tags.get(i).getUsageCount() > maxUsageCnt / 3 * 2) { - ret += "" + tag + " "; - } else if (tags.get(i).getUsageCount() > maxUsageCnt / 3) { - ret += "" + tag + " "; - } else { - ret += tag + " "; - } - } - return ret; - } } -- cgit v1.2.3