From 9c0f37b2fd7535a46c324ff7eb89ed9454aee88f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 Aug 2017 13:58:34 +0300 Subject: www: no more ugnich templates --- .../www/configuration/WwwAppConfiguration.java | 8 - .../java/com/juick/www/controllers/NewMessage.java | 189 +++------- .../com/juick/www/controllers/PageTemplates.java | 380 --------------------- .../main/java/com/juick/www/controllers/Tags.java | 84 +++-- .../main/java/com/juick/www/controllers/User.java | 2 - .../java/com/juick/www/controllers/UserThread.java | 4 - juick-www/src/main/webapp/WEB-INF/views/post.html | 15 + .../main/webapp/WEB-INF/views/post_success.html | 22 ++ juick-www/src/main/webapp/WEB-INF/views/test.html | 2 + .../src/test/java/com/juick/www/WebAppTests.java | 25 +- src/test/java/com/juick/tests/ApiTests.java | 13 +- 11 files changed, 148 insertions(+), 596 deletions(-) delete mode 100644 juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java create mode 100644 juick-www/src/main/webapp/WEB-INF/views/post.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/post_success.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/test.html 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, "Написать", "" + model.addAttribute("title", "Написать"); + model.addAttribute("headers", "" + "" + "" + ""); - templates.pageNavigation(out, visitor, null); - - out.println("
"); - - out.println("
"); - out.println("

Место: Отменить

"); - out.println("

Фото: (JPG, PNG, до 10Мб)

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


"); - - out.println("" + "" + "

"); - out.println("
"); - out.println("
"); - out.println("

Теги:

"); - printUserTags(out, visitor); - out.println("
"); - - templates.pageFooter(request, out, visitor, false); - templates.pageEnd(out); - } - } - - void printUserTags(PrintWriter out, com.juick.User visitor) { - List 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("

"); - for (int i = 0; i < tags.size(); i++) { - if (i > 0) { - out.print(" "); - } - String taglink = StringUtils.EMPTY; - try { - taglink = "" + StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()) + ""; - } catch (UnsupportedEncodingException e) { - } - int usagecnt = tags.get(i).getUsageCount(); - if (usagecnt <= max / 5 + min) { - out.print("" + taglink + ""); - } else if (usagecnt <= max / 5 * 2 + min) { - out.print(taglink); - } else if (usagecnt <= max / 5 * 3 + min) { - out.print("" + taglink + ""); - } else if (usagecnt <= max / 5 * 4 + min) { - out.print("" + taglink + ""); - } else { - out.print("" + taglink + ""); + 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("

"); + 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 tags = webApp.parseTags(request.getParameter("tags")); + List 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("
"); - out.println("

Сообщение опубликовано

"); - out.println("

Поделитесь своим новым постом в социальных сетях:

"); - if (crosspostService.getTwitterTokens(visitor.getUid()).isPresent()) { - out.println("

Отправить в Twitter

"); - } - out.println("

Отправить в LiveJournal

"); - out.println("

Отправить в ВКонтакте

"); - if (crosspostService.getFacebookToken(visitor.getUid()).isPresent()) { - out.println("

Отправить в Facebook

"); - } - out.println("

Отправить в Google+

"); - out.println("

Ссылка на сообщение: http://juick.com/" + mid + "

"); - out.println("
"); - - 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 . - */ -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(""); - out.print(""); - out.print(""); - out.println(""); - out.print(""); - out.print(""); - if (headers != null) { - out.print(headers); - } - out.print("" + title + ""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.flush(); - if (visitor.getUid() > 0) { - out.println(""); - } else { - out.println(""); - } - } - - public void pageNavigation(PrintWriter out, com.juick.User visitor, String search) { - out.println("
"); - out.println(" "); - out.print(" "); - out.print("
"); - out.println("
"); - if (visitor.getUid() > 0) { - out.print(" "); - out.print(" "); - } else { - out.println("

Чтобы добавлять сообщения и комментарии, представьтесь.

"); - } - out.println("
"); - out.println("
"); - } - - 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(""); - } - - public String formatPopularTags(int cnt) { - List popularTags = tagService.getPopularTags().stream() - .map(t -> "" + StringEscapeUtils.escapeHtml4(t) + "").collect(Collectors.toList()); - return StringUtils.collectionToDelimitedString(popularTags, " "); - } - - public void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { - out.println("
"); - out.println(" "); - out.print("
"); - out.print("Twitter"); - out.print("ВКонтакте"); - out.print("Facebook"); - out.println("
"); - out.print("
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("
Спонсоры: " + links); - } - } - - out.println("
"); - out.println("
"); - - if (sapeon) { - out.println(""); - } - } - - public void pageEnd(PrintWriter out) { - out.println(""); - } - - public String formatTags(List tags) { - String ret = org.apache.commons.lang3.StringUtils.EMPTY; - for (TagStats tag : tags) { - String tagName = StringEscapeUtils.escapeHtml4(tag.getTag().getName()); - try { - ret += ""; - } 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 mids, com.juick.User visitor) { - List msgs = messagesService.getMessages(mids); - - List blUIDs = new ArrayList(20); - if (visitor != null) { - for (Message msg : msgs) { - blUIDs.add(msg.getUser().getUid()); - } - blUIDs = userService.checkBL(visitor.getUid(), blUIDs); - } - - for (Message msg : msgs) { - - List tags = tagService.getMessageTags(msg.getMid()); - String tagsStr = formatTags(tags); - if (msg.ReadOnly) { - tagsStr += "readonly"; - } - if (msg.getPrivacy() < 0) { - tagsStr += "friends"; - } - 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("
"); - out.println("
"); - out.println(" @" + msg.getUser().getName() + ":"); - out.println("
\""
"); - out.println("
"); - out.println(" "); - - out.println("
" + tagsStr + "
"); - out.println("
"); - - if (msg.getAttachmentType() != null) { - String fname = msg.getMid() + "." + msg.getAttachmentType(); - out.println("

\"\"/

"); - } - out.println("

" + txt + "

"); - if (msg.getAttachmentType() != null) { - out.println("
"); - } - out.print(" "); - - out.print(" "); - out.print("
"); - } - } -} 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 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 = ""; - } else if (before > 0 || mids.size() < 5) { - head = ""; - } - templates.pageHead(out, visitor, title, head); - templates.pageNavigation(out, visitor, null); - - out.println("
"); - - 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("

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

"); - } - - out.println("
"); - - templates.pageHomeColumn(out, visitor); + String head = StringUtils.EMPTY; + if (tagService.getTagNoIndex(paramTag.TID)) { + head = ""; + } else if (before > 0 || mids.size() < 5) { + head = ""; + } + model.addAttribute("headers", head); + model.addAttribute("visitor", visitor); - templates.pageFooter(request, out, visitor, true); + List msgs = messagesService.getMessages(mids); - templates.pageEnd(out); + 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.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 = "

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

"; + 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 %} +
+

Фото: + (JPG, PNG, до 10Мб)

+ +


+

+
+

Теги:

+{{ 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 %} +

Сообщение опубликовано

+

Поделитесь своим новым постом в социальных сетях:

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

Ссылка на сообщение: {{ url | raw }}

+{% 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(">_<")); } } diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index bd8547a2..03204957 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -32,7 +32,9 @@ import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.service.search.SearchService; import com.juick.www.WebApp; -import com.juick.www.controllers.PageTemplates; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.loader.StringLoader; +import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.StringUtils; import org.junit.Before; @@ -92,11 +94,6 @@ public class ApiTests { } } - @Bean - public PageTemplates templates() { - return new PageTemplates(); - } - @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); @@ -156,8 +153,6 @@ public class ApiTests { JdbcTemplate jdbcTemplate; @Inject JuickProtocol juickProtocol; - @Inject - PageTemplates templates; @Mock ProtocolListener listener; @@ -209,8 +204,6 @@ public class ApiTests { htmlTagStats.setTag(htmlTag); String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName); assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName()); - assertEquals("template should encode escaped tag in url and show escaped tag in name", - ">_<", templates.formatTags(Collections.singletonList(htmlTagStats))); int mid4 = messagesService.createMessage(user_id, "yoyoyo", null, null); Message msg4 = messagesService.getMessage(mid4); assertEquals("tags string should be empty", StringUtils.EMPTY, msg4.getTagsString()); -- cgit v1.2.3