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 --- .../java/com/juick/www/controllers/NewMessage.java | 189 +++++---------------- 1 file changed, 47 insertions(+), 142 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www/controllers/NewMessage.java') 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") -- cgit v1.2.3