diff options
author | 2017-08-18 13:58:34 +0300 | |
---|---|---|
committer | 2017-08-18 14:34:21 +0300 | |
commit | 9c0f37b2fd7535a46c324ff7eb89ed9454aee88f (patch) | |
tree | bf663780bbd15deee547c79aa8b42b9735368d67 /juick-www | |
parent | f5a87d2b7072426cfe36527db1270e5f85b73e01 (diff) |
www: no more ugnich templates
Diffstat (limited to 'juick-www')
10 files changed, 145 insertions, 586 deletions
diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 5802a5c1..3198df2d 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -17,15 +17,12 @@ package com.juick.www.configuration; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.www.HelpService; import com.juick.www.WebApp; -import com.juick.www.controllers.PageTemplates; import org.apache.commons.io.IOUtils; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; @@ -33,7 +30,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import javax.annotation.Resource; import javax.inject.Inject; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -64,10 +60,6 @@ public class WwwAppConfiguration { return app; } @Bean - public PageTemplates templates() { - return new PageTemplates(); - } - @Bean public CaffeineCacheManager cacheManager() { return new CaffeineCacheManager("help"); } 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 ec1b6310..79102f18 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 @@ -18,23 +18,16 @@ package com.juick.www.controllers; import com.juick.Status; import com.juick.Tag; -import com.juick.server.helpers.TagStats; -import com.juick.server.util.HttpBadRequestException; -import com.juick.server.util.HttpForbiddenException; -import com.juick.server.util.HttpNotFoundException; -import com.juick.server.util.HttpUtils; -import com.juick.server.util.ImageUtils; +import com.juick.server.util.*; import com.juick.service.*; -import com.juick.server.util.UserUtils; -import com.juick.www.Utils; import com.juick.www.WebApp; -import org.apache.commons.codec.CharEncoding; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -46,16 +39,12 @@ import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; 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.URI; import java.net.URISyntaxException; import java.net.URL; -import java.net.URLEncoder; import java.util.List; +import java.util.stream.Collectors; /** * @author Ugnich Anton @@ -77,119 +66,48 @@ public class NewMessage { CrosspostService crosspostService; @Inject WebApp webApp; - @Inject - PageTemplates templates; private static final Logger logger = LoggerFactory.getLogger(NewMessage.class); @GetMapping("/post") - protected void doGetNewMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { + protected String postAction(@RequestParam(required = false) String body, ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { - Utils.sendTemporaryRedirect(response, "/login"); - return; - } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - templates.pageHead(out, visitor, "Написать", "<script src=\"//maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAVVtPtxkw4soCEHg44FsNChRB4OFYjAXt73He16Zkp6a_0tPs2RTU6i6UlcMs4QvPBYvIY8rWvcxqOg\" type=\"text/javascript\"></script>" + model.addAttribute("title", "Написать"); + model.addAttribute("headers", "<script src=\"//maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAVVtPtxkw4soCEHg44FsNChRB4OFYjAXt73He16Zkp6a_0tPs2RTU6i6UlcMs4QvPBYvIY8rWvcxqOg\" type=\"text/javascript\"></script>" + "<script src=\"//static.juick.com/mc.js\" type=\"text/javascript\" defer=\"defer\"></script>" + "<script src=\"//static.juick.com/maps.js?2010111500\" type=\"text/javascript\" defer=\"defer\"></script>" + "<script src=\"//static.juick.com/post3.js\" type=\"text/javascript\" defer=\"defer\"></script>"); - templates.pageNavigation(out, visitor, null); - - out.println("<section id=\"content\" class=\"pagetext\">"); - - out.println("<form action=\"/post2\" method=\"post\" id=\"postmsg\" enctype=\"multipart/form-data\">"); - out.println("<p style=\"text-align: left\"><b>Место: <span id=\"location\"></span></b> <span id=\"locationclear\">— <a href=\"#\" onclick=\"clearLocation()\">Отменить</a></span></p>"); - out.println("<p style=\"text-align: left\"><b>Фото:</b> <span id=\"attachmentfile\"><input type=\"file\" name=\"attach\"/> <i>(JPG, PNG, до 10Мб)</i></span></p>"); - - String body = request.getParameter("body"); - if (body == null) { - body = StringUtils.EMPTY; - } else { - if (body.length() > 4096) { - body = body.substring(0, 4096); - } - body = StringEscapeUtils.escapeHtml4(body); - } - out.println("<p><textarea name=\"body\" class=\"newmessage\" rows=\"7\" cols=\"10\">" + body + "</textarea><br/>"); - - out.println("<input type=\"hidden\" name=\"place_id\"/>" + "" + "<input type=\"submit\" class=\"subm\" value=\" Отправить \"/></p>"); - out.println("</form>"); - out.println("<div id=\"geomap\"></div>"); - out.println("<p style=\"text-align: left\"><b>Теги:</b></p>"); - printUserTags(out, visitor); - out.println("</section>"); - - templates.pageFooter(request, out, visitor, false); - templates.pageEnd(out); - } - } - - void printUserTags(PrintWriter out, com.juick.User visitor) { - List<TagStats> tags = tagService.getUserTagStats(visitor.getUid()); - - if (tags.isEmpty()) { - return; - } - - int min = tags.get(0).getUsageCount(); - int max = tags.get(0).getUsageCount(); - for (int i = 1; i < tags.size(); i++) { - int usagecnt = tags.get(i).getUsageCount(); - if (usagecnt < min) { - min = usagecnt; - } - if (usagecnt > max) { - max = usagecnt; - } - } - max -= min; - - out.print("<p style=\"text-align: justify\">"); - for (int i = 0; i < tags.size(); i++) { - if (i > 0) { - out.print(" "); - } - String taglink = StringUtils.EMPTY; - try { - taglink = "<a onclick=\"return addTag('" + StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()) + "')\" href=\"/" + - visitor.getName() + "/?tag=" + URLEncoder.encode(tags.get(i).getTag().getName(), CharEncoding.UTF_8) + - "\" title=\"" + tags.get(i).getUsageCount() + "\">" + StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()) + "</a>"; - } catch (UnsupportedEncodingException e) { - } - int usagecnt = tags.get(i).getUsageCount(); - if (usagecnt <= max / 5 + min) { - out.print("<span style=\"font-size: small\">" + taglink + "</span>"); - } else if (usagecnt <= max / 5 * 2 + min) { - out.print(taglink); - } else if (usagecnt <= max / 5 * 3 + min) { - out.print("<span style=\"font-size: large\">" + taglink + "</span>"); - } else if (usagecnt <= max / 5 * 4 + min) { - out.print("<span style=\"font-size: x-large\">" + taglink + "</span>"); - } else { - out.print("<span style=\"font-size: xx-large\">" + taglink + "</span>"); + model.addAttribute("visitor", visitor); + if (body == null) { + body = StringUtils.EMPTY; + } else { + if (body.length() > 4096) { + body = body.substring(0, 4096); } + body = StringEscapeUtils.escapeHtml4(body); } - out.println("</p>"); + model.addAttribute("body", body); + model.addAttribute("visitor", visitor); + model.addAttribute("tags", tagService.getUserTagStats(visitor.getUid()).stream() + .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).map(t -> t.getTag().getName()).collect(Collectors.toList())); + return "views/post"; } @PostMapping("/post") - public void doPostMessage(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String img, + public String postResult(@RequestParam(required = false) String img, @RequestParam String body, - @RequestParam(required = false) MultipartFile attach) throws IOException { + @RequestParam(required = false, name = "tags") String tagsStr, + @RequestParam(required = false) MultipartFile attach, ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; + throw new HttpForbiddenException(); } if (body == null || body.length() < 1 || body.length() > 4096) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + throw new HttpBadRequestException(); } body = body.replace("\r", StringUtils.EMPTY); - List<Tag> tags = webApp.parseTags(request.getParameter("tags")); + List<Tag> tags = webApp.parseTags(tagsStr); String attachmentFName = HttpUtils.receiveMultiPartFile(attach, webApp.getTmpDir()); @@ -249,50 +167,37 @@ public class NewMessage { // - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - templates.pageHead(out, visitor, "Сообщение опубликовано", null); - templates.pageNavigation(out, visitor, null); - - String hashtags = StringUtils.EMPTY; - String tagscomma = StringUtils.EMPTY; - for (int i = 0; i < jmsg.getTags().size(); i++) { - if (i > 0) { - hashtags += " "; - tagscomma += ","; - } - hashtags += "#" + jmsg.getTags().get(i); - tagscomma += jmsg.getTags().get(i); + model.addAttribute("title", "Сообщение опубликовано"); + model.addAttribute("visitor", visitor); + model.addAttribute("user", visitor); + + String hashtags = StringUtils.EMPTY; + String tagscomma = StringUtils.EMPTY; + for (int i = 0; i < jmsg.getTags().size(); i++) { + if (i > 0) { + hashtags += " "; + tagscomma += ","; } + hashtags += "#" + jmsg.getTags().get(i); + tagscomma += jmsg.getTags().get(i); + } + + model.addAttribute("url", "http://juick.com/" + mid); - String url = URLEncoder.encode("http://juick.com/" + mid, CharEncoding.UTF_8); + if (crosspostService.getTwitterTokens(visitor.getUid()).isPresent()) { String sharetwi = hashtags + " " + body; if (sharetwi.length() > 115) { sharetwi = sharetwi.substring(0, 114) + "…"; } sharetwi += " http://juick.com/" + mid; - String sharelj = URLEncoder.encode(body + "\n", CharEncoding.UTF_8) + url; - - out.println("<section id=\"content\">"); - out.println("<h1>Сообщение опубликовано</h1>"); - out.println("<p>Поделитесь своим новым постом в социальных сетях:</p>"); - if (crosspostService.getTwitterTokens(visitor.getUid()).isPresent()) { - out.println("<p class=\"social\"><a href=\"https://twitter.com/intent/tweet?text=" + URLEncoder.encode(sharetwi, CharEncoding.UTF_8) + "\" class=\"ico32-twi sharenew\">Отправить в Twitter</a></p>"); - } - out.println("<p class=\"social\"><a href=\"http://www.livejournal.com/update.bml?subject=" + URLEncoder.encode(hashtags, CharEncoding.UTF_8) + "&event=" + sharelj + "&prop_taglist=" + URLEncoder.encode(tagscomma, CharEncoding.UTF_8) + "\" target=\"_blank\" class=\"ico32-lj sharenew\">Отправить в LiveJournal</a></p>"); - out.println("<p class=\"social\"><a href=\"https://vk.com/share.php?url=" + url + "\" class=\"ico32-vk sharenew\">Отправить в ВКонтакте</a></p>"); - if (crosspostService.getFacebookToken(visitor.getUid()).isPresent()) { - out.println("<p class=\"social\"><a href=\"https://www.facebook.com/sharer/sharer.php?u=" + url + "\" class=\"ico32-fb sharenew\">Отправить в Facebook</a></p>"); - } - out.println("<p class=\"social\"><a href=\"https://plus.google.com/share?url=" + url + "\" class=\"ico32-gp sharenew\">Отправить в Google+</a></p>"); - out.println("<p>Ссылка на сообщение: <a href=\"http://juick.com/" + mid + "\">http://juick.com/" + mid + "</a></p>"); - out.println("</section>"); - - templates.pageHomeColumn(out, visitor); - - templates.pageFooter(request, out, visitor, false); - templates.pageEnd(out); + model.addAttribute("sharetwi", sharetwi); + } + if (crosspostService.getFacebookToken(visitor.getUid()).isPresent()) { + model.addAttribute("facebook", 1); } + model.addAttribute("tags", tagService.getUserTagStats(visitor.getUid()).stream() + .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).map(t -> t.getTag().getName()).collect(Collectors.toList())); + return "views/post_success"; } @PostMapping("/comment") diff --git a/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java deleted file mode 100644 index a01cbc55..00000000 --- a/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java +++ /dev/null @@ -1,380 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.www.controllers; - -import com.juick.Message; -import com.juick.server.helpers.TagStats; -import com.juick.service.MessagesService; -import com.juick.service.TagService; -import com.juick.service.UserService; -import com.juick.util.MessageUtils; -import com.juick.www.WebApp; -import org.apache.commons.codec.CharEncoding; -import org.apache.commons.text.StringEscapeUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; -import org.springframework.web.util.WebUtils; -import ru.sape.Sape; - -import javax.inject.Inject; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.stream.Collectors; - -/** - * @author Ugnich Anton - */ -public class PageTemplates { - - private static final Logger logger = LoggerFactory.getLogger(PageTemplates.class); - - @Inject - private Sape sape; - protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); - private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy"); - private static String tagsHTML = null; - - @Inject - TagService tagService; - @Inject - MessagesService messagesService; - @Inject - UserService userService; - @Inject - WebApp webApp; - - public void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) { - out.println("<!DOCTYPE html>"); - out.print("<html>"); - out.print("<head>"); - out.println("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"); - out.print("<link rel=\"stylesheet\" href=\"/" + webApp.getStyleUrl() + "\"/>"); - out.print("<script type=\"text/javascript\" src=\"/" + webApp.getScriptsUrl() + "\"></script>"); - if (headers != null) { - out.print(headers); - } - out.print("<title>" + title + "</title>"); - out.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>"); - out.println("<meta name=\"msapplication-config\" content=\"//i.juick.com/browserconfig.xml\"/>"); - out.println("<meta name=\"msapplication-TileColor\" content=\"#ffffff\"/>"); - out.println("<meta name=\"msapplication-TileImage\" content=\"//i.juick.com/ms-icon-144x144.png\"/>"); - out.println("<meta name=\"theme-color\" content=\"#ffffff\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"57x57\" href=\"//i.juick.com/apple-icon-57x57.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"60x60\" href=\"//i.juick.com/apple-icon-60x60.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"72x72\" href=\"//i.juick.com/apple-icon-72x72.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"76x76\" href=\"//i.juick.com/apple-icon-76x76.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"114x114\" href=\"//i.juick.com/apple-icon-114x114.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"120x120\" href=\"//i.juick.com/apple-icon-120x120.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"144x144\" href=\"//i.juick.com/apple-icon-144x144.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"152x152\" href=\"//i.juick.com/apple-icon-152x152.png\"/>"); - out.println("<link rel=\"apple-touch-icon\" sizes=\"180x180\" href=\"//i.juick.com/apple-icon-180x180.png\"/>"); - out.println("<link rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"//i.juick.com/android-icon-192x192.png\"/>"); - out.println("<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"//i.juick.com/favicon-32x32.png\"/>"); - out.println("<link rel=\"icon\" type=\"image/png\" sizes=\"96x96\" href=\"//i.juick.com/favicon-96x96.png\"/>"); - out.println("<link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"//i.juick.com/favicon-16x16.png\"/>"); - out.println("<link rel=\"manifest\" href=\"//i.juick.com/manifest.json\"/>"); - out.println("<!--[if lt IE 9 & (!IEMobile 7)]>"); - out.println("<script src=\"//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js\"></script>"); - out.println("<![endif]-->"); - out.println("</head>"); - out.flush(); - if (visitor.getUid() > 0) { - out.println("<body id=\"body\" data-hash=\"" + visitor.getAuthHash() + "\">"); - } else { - out.println("<body id=\"body\">"); - } - } - - public void pageNavigation(PrintWriter out, com.juick.User visitor, String search) { - out.println("<header>"); - out.println(" <div id=\"logo\"><a href=\"/\">Juick</a></div>"); - out.print(" <nav id=\"global\"><ul>"); - out.print("<li><a href=\"/\">Популярные</a></li>"); - out.print("<li><a href=\"/?show=all\" rel=\"nofollow\">Все сообщения</a></li>"); - out.print("<li><a href=\"/?show=photos\" rel=\"nofollow\">Фотографии</a></li>"); - out.println("</ul></nav>"); - out.print(" <div id=\"search\"><form action=\"/\"><input type=\"text\" name=\"search\" class=\"text\" placeholder=\"Поиск\""); - if (search != null) { - out.print(" value=\"" + StringEscapeUtils.escapeHtml4(search) + "\""); - } - out.println("/></form></div>"); - out.println(" <section id=\"headdiv\">"); - if (visitor.getUid() > 0) { - out.print(" <nav id=\"user\"><ul>"); - out.print("<li><a href=\"/?show=my\">Моя лента</a></li>"); - out.print("<li><a href=\"/pm/inbox\">Приватные</a></li>"); - out.print("<li><a href=\"/?show=discuss\">Обсуждения</a></li>"); - out.print("<li><a href=\"/?show=recommended\">Рекомендации</a></li>"); - out.println("</ul></nav>"); - out.print(" <nav id=\"actions\"><ul>"); - out.print("<li><a href=\"/#post\">Написать</a></li>"); - out.print("<li><a href=\"/" + visitor.getName() + "\">@" + visitor.getName() + "</a></li>"); - out.print("<li><a href=\"/logout\">Выйти</a></li>"); - out.println("</ul></nav>"); - } else { - out.println("<p>Чтобы добавлять сообщения и комментарии, <a href=\"#\" class=\"a-login\">представьтесь</a>.</p>"); - } - out.println(" </section>"); - out.println("</header>"); - } - - public void pageHomeColumn(PrintWriter out, com.juick.User visitor) { - pageHomeColumn(out, visitor, false); - } - - public void pageHomeColumn(PrintWriter out, com.juick.User visitor, boolean showAdv) { - if (tagsHTML == null) { - tagsHTML = formatPopularTags(80); - } - - out.println("<aside id=\"column\">"); - out.print(" <p class=\"tags\">" + tagsHTML); - if (showAdv) { - out.print(" <a href=\"http://ru.wix.com/\">конструктор сайтов</a>"); - } - out.println("</p>"); -// if (visitor != null) { -// printContestRating(out, sql); -// } - out.println("</aside>"); - } - - public String formatPopularTags(int cnt) { - List<String> popularTags = tagService.getPopularTags().stream() - .map(t -> "<a href=\"/tag/" + URLEncoder.encode(t) + "\">" + StringEscapeUtils.escapeHtml4(t) + "</a>").collect(Collectors.toList()); - return StringUtils.collectionToDelimitedString(popularTags, " "); - } - - public void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { - out.println("<div id=\"footer\">"); - out.println(" <div id=\"footer-right\"><a href=\"/settings\" rel=\"nofollow\">Настройки</a> · <a href=\"/help/ru/contacts\" rel=\"nofollow\">Контакты</a> · <a href=\"/help/\" rel=\"nofollow\">Справка</a> · <a href=\"/help/ru/adv\" rel=\"nofollow\">Реклама</a></div>"); - out.print(" <div id=\"footer-social\">"); - out.print("<a href=\"https://twitter.com/Juick\" rel=\"nofollow\" class=\"ico32-twi\">Twitter</a>"); - out.print("<a href=\"https://vk.com/juick\" rel=\"nofollow\" class=\"ico32-vk\">ВКонтакте</a>"); - out.print("<a href=\"https://www.facebook.com/JuickCom\" rel=\"nofollow\" class=\"ico32-fb\">Facebook</a>"); - out.println("</div>"); - out.print(" <div id=\"footer-left\">juick.com © 2008-2017"); - - String queryString = request.getQueryString(); - String requestURI = request.getRequestURI(); - if (sapeon && sape != null && (visitor.getUid() == 0 || visitor.getUid() == 1) && queryString == null) { - String cookieValue = null; - Cookie sapeCookie = WebUtils.getCookie(request, "sape_cookie"); - if (sapeCookie != null) { - cookieValue = sapeCookie.getValue(); - } - String links = sape.getPageLinks(requestURI, cookieValue).render(); - if (links != null && !links.isEmpty()) { - out.print("<br/>Спонсоры: " + links); - } - } - - out.println("</div>"); - out.println("</div>"); - - if (sapeon) { - out.println("<script>"); - out.println("var _acic={dataProvider:10};"); - out.println("(function(){"); - out.println("var e=document.createElement('script');e.type='text/javascript';e.async=true;e.src='//www2.aci'+'nt.net/aci.js';"); - out.println("var t=document.getElementsByTagName('script')[0];t.parentNode.insertBefore(e,t);"); - out.println("})();"); - out.println("</script>"); - } - } - - public void pageEnd(PrintWriter out) { - out.println("</body></html>"); - } - - public String formatTags(List<TagStats> tags) { - String ret = org.apache.commons.lang3.StringUtils.EMPTY; - for (TagStats tag : tags) { - String tagName = StringEscapeUtils.escapeHtml4(tag.getTag().getName()); - try { - ret += "<a href=\"/tag/" + URLEncoder.encode(tag.getTag().getName(), CharEncoding.UTF_8) + "\""; - if (tag.getUsageCount() < 2) { - ret += " rel=\"nofollow\""; - } - ret += ">" + tagName + "</a>"; - } catch (UnsupportedEncodingException e) { - } - } - - return ret; - } - - public String formatDate(int minutes, Date fulldate) { - if (minutes < 1) { - return "сейчас"; - } else if (minutes < 60) { - String unit; - int ld = minutes % 10; - if ((minutes < 10 || minutes > 20) && ld == 1) { - unit = "минуту"; - } else if ((minutes < 10 || minutes > 20) && ld > 1 && ld < 5) { - unit = "минуты"; - } else { - unit = "минут"; - } - return minutes + " " + unit + " назад"; - } else if (minutes < 1440) { - int hours = (minutes / 60); - String unit; - int ld = hours % 10; - if ((hours < 10 || hours > 20) && ld == 1) { - unit = "час"; - } else if ((hours < 10 || hours > 20) && ld > 1 && ld < 5) { - unit = "часа"; - } else { - unit = "часов"; - } - return hours + " " + unit + " назад"; - } else if (minutes < 20160) { - int days = (minutes / 1440); - String unit; - int ld = days % 10; - if ((days < 10 || days > 20) && ld == 1) { - unit = "день"; - } else if ((days < 10 || days > 20) && ld > 1 && ld < 5) { - unit = "дня"; - } else { - unit = "дней"; - } - return days + " " + unit + " назад"; - } else { - String ret = sdfFull.format(fulldate); - synchronized (sdfSQL) { - try { - Calendar c = Calendar.getInstance(); - int curyear = c.get(Calendar.YEAR); - c.setTime(fulldate); - if (c.get(Calendar.YEAR) == curyear) { - ret = sdfSimple.format(fulldate); - } else { - ret = sdfFull.format(fulldate); - } - } catch (Exception e) { - logger.error("PARSE EXCEPTION: {}, exception {}", fulldate, e); - } - } - return ret; - } - } - - public String formatReplies(int replies) { - int ld = replies % 10; - int lh = replies % 100; - if ((lh < 10 || lh > 20) && ld == 1) { - return replies + " ответ"; - } else if ((lh < 10 || lh > 20) && ld > 1 && ld < 5) { - return replies + " ответа"; - } else { - return replies + " ответов"; - } - } - - public void printMessages(PrintWriter out, com.juick.User user, List<Integer> mids, com.juick.User visitor) { - List<com.juick.Message> msgs = messagesService.getMessages(mids); - - List<Integer> blUIDs = new ArrayList<Integer>(20); - if (visitor != null) { - for (Message msg : msgs) { - blUIDs.add(msg.getUser().getUid()); - } - blUIDs = userService.checkBL(visitor.getUid(), blUIDs); - } - - for (Message msg : msgs) { - - List<TagStats> tags = tagService.getMessageTags(msg.getMid()); - String tagsStr = formatTags(tags); - if (msg.ReadOnly) { - tagsStr += "<a>readonly</a>"; - } - if (msg.getPrivacy() < 0) { - tagsStr += "<a>friends</a>"; - } - String txt; - if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) { - txt = MessageUtils.formatMessageCode(msg.getText()); - } else { - txt = MessageUtils.formatMessage(msg.getText()); - } - - out.println("<article data-mid=\"" + msg.getMid() + "\">"); - out.println(" <header class=\"h\">"); - out.println(" @<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:"); - out.println(" <div class=\"msg-avatar\"><a href=\"/" + msg.getUser().getName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUid() + ".png\" alt=\"" + msg.getUser().getName() + "\"/></a></div>"); - out.println(" <div class=\"msg-menu\"><a href=\"#\"></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getUser().getName() + "/" + msg.getMid() + "\"><time datetime=\"" + sdfSQL.format(msg.getDate()) + "Z\" title=\"" + sdfSQL.format(msg.getDate()) + " GMT\">" + formatDate(msg.TimeAgo, msg.getDate()) + "</time></a></div>"); - - out.println(" <div class=\"msg-tags\">" + tagsStr + "</div>"); - out.println(" </header>"); - - if (msg.getAttachmentType() != null) { - String fname = msg.getMid() + "." + msg.getAttachmentType(); - out.println(" <p class=\"ir\"><a href=\"//i.juick.com/photos-512/" + fname + "\" data-fname=\"" + fname + "\"><img src=\"//i.juick.com/photos-512/" + fname + "\" alt=\"\"/></a></p>"); - } - out.println(" <p>" + txt + "</p>"); - if (msg.getAttachmentType() != null) { - out.println(" <div class=\"irbr\"></div>"); - } - out.print(" <nav class=\"l\">"); - msg.ReadOnly |= blUIDs.contains(msg.getUser().getUid()); - if (visitor.getUid() == 0) { - out.print("<a href=\"#\" class=\"a-login\">Рекомендовать</a>"); - } else { - out.print("<a href=\"/post?body=!+%23" + msg.getMid() + "\" class=\"a-like\">Рекомендовать</a>"); - } - if (visitor.getUid() == 0 && !msg.ReadOnly) { - out.print("<a href=\"/" + msg.getMid() + "\" class=\"a-login\">Комментировать</a> "); - } else if (visitor.getUid() > 0 && (!msg.ReadOnly || visitor.getUid() == msg.getUser().getUid())) { - out.print("<a class=\"a-comment\" href=\"/" + msg.getMid() + "\">Комментировать</a> "); - } - if (visitor.getUid() > 0 && msg.getPrivacy() < 0 && msg.getUser().getUid() == visitor.getUid()) { - out.print(" <a href=\"#\" class=\"a-privacy\">Открыть доступ</a>"); - } - if (visitor.getUid() > 0 && visitor.getUid() == 3694) { - out.print(" <a href=\"#\" class=\"a-popular-plus\">+</a>"); - out.print(" <a href=\"#\" class=\"a-popular-minus\">-</a>"); - out.print(" <a href=\"#\" class=\"a-popular-delete\">x</a>"); - } - out.println("</nav>"); - - out.print(" <nav class=\"s\">"); - if (msg.getLikes() > 0) { - out.print("<a href=\"/" + msg.getUser().getName() + "/" + msg.getMid() + "\" class=\"likes\"><i data-icon=\"ei-heart\" data-size=\"s\"></i> " + msg.getLikes() + "</a>"); - } - if (msg.getReplies() > 0) { - out.print("<a href=\"/" + msg.getUser().getName() + "/" + msg.getMid() + "\" class=\"replies\"><i data-icon=\"ei-comment\" data-size=\"s\"></i> " + msg.getReplies() + "</a>"); - } - out.println("</nav>"); - out.print("</article>"); - } - } -} diff --git a/juick-www/src/main/java/com/juick/www/controllers/Tags.java b/juick-www/src/main/java/com/juick/www/controllers/Tags.java index 29e2e598..acab1005 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Tags.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Tags.java @@ -16,105 +16,97 @@ */ package com.juick.www.controllers; +import com.juick.server.util.HttpNotFoundException; +import com.juick.server.util.UserUtils; import com.juick.service.MessagesService; import com.juick.service.TagService; -import com.juick.server.util.UserUtils; -import com.juick.www.Utils; -import com.juick.www.WebApp; +import com.juick.service.UserService; import org.apache.commons.codec.CharEncoding; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.PrintWriter; import java.net.URLEncoder; import java.util.List; +import java.util.stream.Collectors; /** - * * @author Ugnich Anton */ @Controller public class Tags { @Inject - WebApp webApp; - @Inject MessagesService messagesService; @Inject TagService tagService; @Inject - PageTemplates templates; + UserService userService; @GetMapping("/tag/{tagName}") - protected void doGet(HttpServletRequest request, - @PathVariable String tagName, - @RequestParam(required = false, defaultValue = "0") int before, - HttpServletResponse response) throws IOException { + protected String tagAction(HttpServletRequest request, + @PathVariable String tagName, + @RequestParam(required = false, defaultValue = "0") int before, + ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); String paramTagStr = StringEscapeUtils.unescapeHtml4(tagName); com.juick.Tag paramTag = tagService.getTag(paramTagStr, false); if (paramTag == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) { com.juick.Tag synTag = tagService.getTag(paramTag.SynonymID); String url = "/tag/" + URLEncoder.encode(StringEscapeUtils.escapeHtml4(synTag.getName()), CharEncoding.UTF_8); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); } - Utils.sendPermanentRedirect(response, url); - return; + return "redirect:" + url; } else if (!paramTag.getName().equals(paramTagStr)) { String url = "/tag/" + URLEncoder.encode(StringEscapeUtils.escapeHtml4(paramTag.getName()), CharEncoding.UTF_8); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); } - Utils.sendPermanentRedirect(response, url); - return; + return "redirect:" + url; } int visitor_uid = visitor.getUid(); String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName()); + model.addAttribute("title", title); List<Integer> mids = messagesService.getTag(paramTag.TID, visitor_uid, before, (visitor_uid == 0) ? 40 : 20); - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - String head = StringUtils.EMPTY; - if (tagService.getTagNoIndex(paramTag.TID)) { - head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; - } else if (before > 0 || mids.size() < 5) { - head = "<meta name=\"robots\" content=\"noindex\"/>"; - } - templates.pageHead(out, visitor, title, head); - templates.pageNavigation(out, visitor, null); - - out.println("<section id=\"content\">"); - - if (mids.size() > 0) { - templates.printMessages(out, null, mids, visitor); - } - - if (mids.size() >= 20) { - String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "?before=" + mids.get(mids.size() - 1); - out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"); - } - - out.println("</section>"); - - templates.pageHomeColumn(out, visitor); + String head = StringUtils.EMPTY; + if (tagService.getTagNoIndex(paramTag.TID)) { + head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; + } else if (before > 0 || mids.size() < 5) { + head = "<meta name=\"robots\" content=\"noindex\"/>"; + } + model.addAttribute("headers", head); + model.addAttribute("visitor", visitor); - templates.pageFooter(request, out, visitor, true); + List<com.juick.Message> msgs = messagesService.getMessages(mids); - templates.pageEnd(out); + 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.getPopularTags()); + model.addAttribute("headers", head); + model.addAttribute("showAdv",before == 0 && visitor.getUid() == 0); + model.addAttribute("isModerator", visitor.getUid() == 3694); + if (mids.size() >= 20) { + String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "?before=" + mids.get(mids.size() - 1); + String next = "<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"; + model.addAttribute("next", next); } + return "views/index"; } } 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 532e0caa..0daad082 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 @@ -58,8 +58,6 @@ public class User { @Inject MessagesService messagesService; @Inject - PageTemplates templates; - @Inject private Sape sape; void fillUserModel(ModelMap model, com.juick.User user, com.juick.User visitor) { diff --git a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java index 9ab45333..51d715cf 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java @@ -50,10 +50,6 @@ public class UserThread { MessagesService messagesService; @Inject UserService userService; - @Inject - TagService tagService; - @Inject - PageTemplates templates; @GetMapping("/{uname}/{mid}") protected String threadAction(ModelMap model, diff --git a/juick-www/src/main/webapp/WEB-INF/views/post.html b/juick-www/src/main/webapp/WEB-INF/views/post.html new file mode 100644 index 00000000..51b044da --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/post.html @@ -0,0 +1,15 @@ +{% extends "layouts/content" %} +{% import "views/macros/tags" %} +{% block content %} +<form action="/post2" method="post" id="postmsg" enctype="multipart/form-data"> + <p style="text-align: left"><b>Фото:</b> <span id="attachmentfile"> + <input type="file" name="attach"/> <i>(JPG, PNG, до 10Мб)</i></span></p> + + <p><textarea name="body" class="newmessage" rows="7" cols="10">{{ body }}</textarea><br/> + <input type="submit" class="subm" value=" Отправить "/></p> +</form> +<p style="text-align: left"><b>Теги:</b></p> +{{ tags(visitor.name, tags) }} +{% endblock %} +{% block "column" %} +{% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/post_success.html b/juick-www/src/main/webapp/WEB-INF/views/post_success.html new file mode 100644 index 00000000..dd96495f --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/post_success.html @@ -0,0 +1,22 @@ +{% extends "layouts/content" %} +{% block content %} +<h1>Сообщение опубликовано</h1> +<p>Поделитесь своим новым постом в социальных сетях:</p> +{%if sharetwi | default('') is not empty %} +<p class="social"> + <a href="https://twitter.com/intent/tweet?text={{ sharetwi }}" + class="ico32-twi sharenew">Отправить в Twitter</a></p> +{% endif %} +<p class="social"> + <a href="https://vk.com/share.php?url={{ url | urlencode }}" + class="ico32-vk sharenew">Отправить в ВКонтакте</a></p> +{%if facebook | default('') is not empty %} +<p class="social"> + <a href="https://www.facebook.com/sharer/sharer.php?u={{ url | urlencode }}" + class="ico32-fb sharenew">Отправить в Facebook</a></p> +{% endif %} +<p>Ссылка на сообщение: <a href="{{ url | urlencode }}">{{ url | raw }}</a></p> +{% endblock %} +{% block "column" %} +{% include "views/partial/usercolumn" %} +{% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/test.html b/juick-www/src/main/webapp/WEB-INF/views/test.html new file mode 100644 index 00000000..7700be6f --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/test.html @@ -0,0 +1,2 @@ +{% import "views/macros/tags" %} +{{ tags("ugnich", tagsList)}}
\ No newline at end of file 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 98df3660..d09bded2 100644 --- a/juick-www/src/test/java/com/juick/www/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/www/WebAppTests.java @@ -22,6 +22,7 @@ import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.css.StyleElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.juick.Message; +import com.juick.Tag; import com.juick.User; import com.juick.configuration.MockDataConfiguration; import com.juick.server.configuration.BaseWebConfiguration; @@ -31,6 +32,10 @@ import com.juick.test.util.MockUtils; import com.juick.www.configuration.SapeConfiguration; import com.juick.www.configuration.WwwAppConfiguration; import com.juick.www.configuration.WwwServletConfiguration; +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.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -44,9 +49,9 @@ import org.springframework.web.context.WebApplicationContext; import javax.inject.Inject; import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.io.StringWriter; +import java.io.Writer; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.StreamSupport; @@ -81,6 +86,9 @@ public class WebAppTests { @Inject MessagesService messagesService; + @Inject + PebbleEngine pebbleEngine; + @Before public void setup() { webClient = MockMvcWebClientBuilder.webAppContextSetup(this.wac).build(); @@ -176,6 +184,15 @@ public class WebAppTests { return display == null || !display.getValue().equals("none"); }).count(); assertThat(visibleItems, equalTo(1L)); - + } + @Test + public void correctTagsEscaping() throws PebbleException, IOException { + PebbleTemplate template = pebbleEngine.getTemplate("views/test"); + Writer writer = new StringWriter(); + template.evaluate(writer, + Collections.singletonMap("tagsList", + Collections.singletonList(StringEscapeUtils.escapeHtml4(new Tag(">_<").getName())))); + String output = writer.toString().trim(); + assertThat(output, equalTo("<a href=\"/ugnich/?tag=%26gt%3B_%26lt%3B\">>_<</a>")); } } |