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 ++++++++------------- juick-www/src/main/resources/messages.properties | 10 +- .../src/main/resources/messages_ru.properties | 10 +- juick-www/src/main/webapp/WEB-INF/views/blog.html | 85 +++++++ juick-www/src/main/webapp/WEB-INF/views/index.html | 4 +- .../src/main/webapp/WEB-INF/views/macros/tags.html | 2 +- .../webapp/WEB-INF/views/partial/usercolumn.html | 76 ++++++ .../webapp/WEB-INF/views/partial/usertags.html | 3 + 8 files changed, 280 insertions(+), 169 deletions(-) create mode 100644 juick-www/src/main/webapp/WEB-INF/views/blog.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/partial/usercolumn.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/partial/usertags.html (limited to 'juick-www/src/main') 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; - } } diff --git a/juick-www/src/main/resources/messages.properties b/juick-www/src/main/resources/messages.properties index 6f587853..8ad0fa09 100644 --- a/juick-www/src/main/resources/messages.properties +++ b/juick-www/src/main/resources/messages.properties @@ -48,4 +48,12 @@ message.comment=Comment postForm.writeReply=Write a reply messages.next=Next error.pageNotFound=Page not found -error.pageNotFound.description=Probably, user deleted this post, or this page never existed. \ No newline at end of file +error.pageNotFound.description=Probably, user deleted this post, or this page never existed. + +blog.blog=Blog +blog.recommendations=Recommendations +blog.photos=Photos +blog.iread=I read +blog.readers=My readers +blog.messages=Messages +blog.comments=Comments \ No newline at end of file diff --git a/juick-www/src/main/resources/messages_ru.properties b/juick-www/src/main/resources/messages_ru.properties index 4d8c2c0f..5c00055e 100644 --- a/juick-www/src/main/resources/messages_ru.properties +++ b/juick-www/src/main/resources/messages_ru.properties @@ -48,4 +48,12 @@ message.comment=Комментировать postForm.writeReply=Написать ответ messages.next=Читать далее error.pageNotFound=Страница не найдена -error.pageNotFound.description=Сожалеем, но страницу с этим адресом удалил её автор, либо её никогда не существовало. \ No newline at end of file +error.pageNotFound.description=Сожалеем, но страницу с этим адресом удалил её автор, либо её никогда не существовало. + +blog.blog=Блог +blog.recommendations=Рекомендации +blog.photos=Фотографии +blog.iread=Я читаю +blog.readers=Мои подписчики +blog.messages=Сообщения +blog.comments=Комментарии \ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/blog.html b/juick-www/src/main/webapp/WEB-INF/views/blog.html new file mode 100644 index 00000000..6672adff --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/blog.html @@ -0,0 +1,85 @@ +{% extends "layouts/content" %} +{% import "views/macros/tags" %} +{% block content %} +{% if noindex %} + +{% endif %} +{% if paramTag | default('') is not empty %} +

← Все записи с тегом {{ paramTag.name | escape }}

+{% endif %} +{% if paramTag | default('') is not empty %} +

← Все записи с тегом {{ paramTag.name | escape }}

+{% endif %} +{% for msg in msgs %} +
+
@{{ msg.user.name }}: + +
+ +
+ {{ tags(msg.user.name, msg.tags) }} +
+
+ + {% if msg.AttachmentType is not empty %} +

+ +

+ {% endif %} +

{{ msg | formatMessage }}

+ {% if msg.AttachmentType is not empty %} +
+ {% endif %} + {% if not readonly %} + + {% endif %} + +
+{% endfor %} +{% if (next | default('')) is not empty %} +{{ next | raw }} +{% endif %} +{% endblock %} +{% block "column" %} +{% include "views/partial/usercolumn" %} +{% if noindex %} + +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/index.html b/juick-www/src/main/webapp/WEB-INF/views/index.html index 0e4eb6ba..0bf2a2fd 100644 --- a/juick-www/src/main/webapp/WEB-INF/views/index.html +++ b/juick-www/src/main/webapp/WEB-INF/views/index.html @@ -68,9 +68,7 @@ {% endfor %} -{% if (next | default('')) is not empty %} -{{ next | raw }} -{% endif %} +{{ next | default ('') | raw }} {% endblock %} {% block "column" %} {% include "views/partial/homecolumn" %} diff --git a/juick-www/src/main/webapp/WEB-INF/views/macros/tags.html b/juick-www/src/main/webapp/WEB-INF/views/macros/tags.html index ffd81ddd..b34e6ecc 100644 --- a/juick-www/src/main/webapp/WEB-INF/views/macros/tags.html +++ b/juick-www/src/main/webapp/WEB-INF/views/macros/tags.html @@ -1,5 +1,5 @@ {% macro tags(uname="", tagsList) %} {% for tag in tagsList %} -{{ tag }} +{{ tag.name }} {% endfor %} {% endmacro %} \ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/partial/usercolumn.html b/juick-www/src/main/webapp/WEB-INF/views/partial/usercolumn.html new file mode 100644 index 00000000..7e3b8a64 --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/usercolumn.html @@ -0,0 +1,76 @@ + +{% if visitor is not empty and visitor.uid > 0 and visitor.uid != user.uid %} +
    + {% if isSubscribed %} +
  • + +
    +
    +
  • + {% else %} +
  • + +
    +
    +
  • + {% endif %} + {% if isInBL %} +
  • + +
    +
    +
  • + {% else %} +
  • + +
    +
    +
  • + {% endif %} + {% if not isInBLAny %} +
  • + +
    +
    +
  • + {% endif %} +
+{% else %} +
+{% endif %} + +
+
+

+
+{% include "views/partial/usertags" %} +
+
+ + {% if iread is not empty %} +
+ {% for u in iread %} + + + {{ u.name }} + + + {% endfor %} +
+ {% endif %} + +
diff --git a/juick-www/src/main/webapp/WEB-INF/views/partial/usertags.html b/juick-www/src/main/webapp/WEB-INF/views/partial/usertags.html new file mode 100644 index 00000000..78e1416e --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/usertags.html @@ -0,0 +1,3 @@ +{% import "views/macros/tags" %} +{{ tags(uname=user.name, tagsStats) }} +... \ No newline at end of file -- cgit v1.2.3