diff options
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/controllers/Home.java')
-rw-r--r-- | juick-www/src/main/java/com/juick/www/controllers/Home.java | 138 |
1 files changed, 51 insertions, 87 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/Home.java b/juick-www/src/main/java/com/juick/www/controllers/Home.java index 4e6330a8..a36d0d09 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Home.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Home.java @@ -18,8 +18,8 @@ package com.juick.www.controllers; import com.juick.server.util.HttpNotFoundException; -import com.juick.service.AdsService; import com.juick.service.MessagesService; +import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.UserUtils; import com.juick.util.WebUtils; @@ -30,13 +30,17 @@ import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +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.net.URLEncoder; import java.util.List; @@ -51,9 +55,9 @@ public class Home { @Inject MessagesService messagesService; @Inject - AdsService adsService; + TagService tagService; @Inject - PageTemplates templates; + private Sape sape; @Inject WebApp webApp; @@ -89,15 +93,18 @@ public class Home { } @GetMapping("/") - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - String tag = request.getParameter("tag"); + protected String doGet( + @RequestParam(required = false) String tag, + @RequestParam(name = "show", required = false) String paramShow, + @RequestParam(name = "search", required = false) String paramSearch, + @RequestParam(name = "before", required = false, defaultValue = "0") Integer paramBefore, + @CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie, + ModelMap model) throws IOException { if (tag != null) { - Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, CharEncoding.UTF_8)); + return "redirect:/tag/" + URLEncoder.encode(tag, CharEncoding.UTF_8); } com.juick.User visitor = UserUtils.getCurrentUser(); - int paramBefore = NumberUtils.toInt(request.getParameter("before"), 0); - String paramSearch = request.getParameter("search"); if (paramSearch != null && paramSearch.length() > 64) { paramSearch = null; } @@ -105,7 +112,6 @@ public class Home { String title; List<Integer> mids; - String paramShow = request.getParameter("show"); if (paramSearch != null) { title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch); mids = messagesService.getSearch(Utils.encodeSphinx(paramSearch), paramBefore); @@ -119,8 +125,7 @@ public class Home { } } else if (paramShow.equals("top")) { - Utils.sendPermanentRedirect(response, "/"); - return; + return "redirect:/"; } else if (paramShow.equals("my") && visitor != null) { title = "Моя лента"; mids = messagesService.getMyFeed(visitor.getUid(), paramBefore); @@ -148,83 +153,42 @@ public class Home { mids = messagesService.getAll(0, paramBefore); } } 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 = StringUtils.EMPTY; - if (paramBefore > 0 || paramShow != null) { - head = "<meta name=\"robots\" content=\"noindex\"/>"; - } - templates.pageHead(out, visitor, title, head); - templates.pageNavigation(out, visitor, paramSearch); - - out.println("<section id=\"content\">"); - - if (paramShow == null && paramBefore == 0) { - out.println("<!--noindex-->"); - } - - if (visitor.getUid() > 0) { - out.println("<form action=\"/post\" method=\"post\" enctype=\"multipart/form-data\">"); - out.println("<section id=\"newmessage\">"); - if (messagesService.isReadonly()) { - out.println("<p>Комментирование временно недоступно</p>"); - } else { - out.println(" <textarea name=\"body\" placeholder=\"Новое сообщение...\"></textarea>"); - out.println(" <div>"); - out.println(" <input type=\"text\" class=\"img\" name=\"img\" " + - "placeholder=\"Ссылка на изображение (JPG/PNG, до 10Мб)\"/> " + - "или <a href=\"#\">загрузить</a><br/>"); - out.println(" <input type=\"text\" class=\"tags\" name=\"tags\" " + - "placeholder=\"Теги (через пробел)\"/><br/>"); - out.println(" <input type=\"submit\" class=\"subm\" value=\"Отправить\"/>"); - out.println(" </div>"); - } - out.println("</section>"); - out.println("</form>"); - } - - if (mids.size() > 0) { - int ad_mid = 0; - if (paramShow == null || paramShow.equals("top") || paramShow.equals("all")) { - int vuid = visitor.getUid(); - ad_mid = adsService.getAdMid(vuid); - if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) { - mids.add(0, ad_mid); - adsService.logAdMid(vuid, ad_mid); - } else { - ad_mid = 0; - } - } - - templates.printMessages(out, null, mids, visitor, visitor.getUid() == 0 ? 2 : 3, ad_mid); - } - - if (mids.size() >= 20) { - String nextpage = "?before=" + mids.get(mids.size() - 1); - if (paramShow != null) { - nextpage += "&show=" + paramShow; - } - if (paramSearch != null) { - nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); - } - - out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"); + String head = StringUtils.EMPTY; + if (paramBefore > 0 || paramShow != null) { + head = "<meta name=\"robots\" content=\"noindex\"/>"; + } + model.addAttribute("title", title); + model.addAttribute("headers", head); + model.addAttribute("visitor", visitor); + model.addAttribute("readonly", messagesService.isReadonly()); + model.addAttribute("noindex", paramShow == null && paramBefore == 0); + model.addAttribute("msgs", messagesService.getMessages(mids)); + model.addAttribute("tags", tagService.getPopularTags()); + model.addAttribute("headers", head); + model.addAttribute("showAdv", + paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); + if (mids.size() >= 20) { + String nextpage = "?before=" + mids.get(mids.size() - 1); + if (paramShow != null) { + nextpage += "&show=" + paramShow; } - - if (paramShow == null && paramBefore == 0) { - out.println("<!--/noindex-->"); + if (paramSearch != null) { + nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); } - out.println("</section>"); - - templates.pageHomeColumn(out, visitor, paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); - - templates.pageFooter(request, out, visitor, true); - templates.pageEnd(out); + String next = "<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>"; + model.addAttribute("next", next); + } + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); + String queryString = builder.getQuery(); + String requestURI = builder.toUri().toString(); + if (sape != null && visitor.getUid() == 0 && queryString == null) { + String links = sape.getPageLinks(requestURI, sapeCookie).render(); + model.addAttribute("links", links); } + return "views/index"; } } |