From 045ca0848c8ee7c771541a5829157531c782cf30 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 19 Apr 2017 14:51:48 +0300 Subject: juick-www: Home controller uses Pebble, drop ads wip --- .../main/java/com/juick/www/controllers/Home.java | 138 ++++++++------------- .../java/com/juick/www/controllers/NewMessage.java | 6 +- .../com/juick/www/controllers/PageTemplates.java | 24 ++-- .../main/java/com/juick/www/controllers/Tags.java | 14 +-- .../main/java/com/juick/www/controllers/User.java | 18 +-- .../java/com/juick/www/controllers/UserThread.java | 7 +- juick-www/src/main/java/ru/sape/Sape.java | 6 +- juick-www/src/main/java/ru/sape/SapePageLinks.java | 23 +--- juick-www/src/main/webapp/WEB-INF/views/index.html | 75 +++++++++++ .../src/main/webapp/WEB-INF/views/macros/tags.html | 5 + .../main/webapp/WEB-INF/views/partial/footer.html | 2 +- .../webapp/WEB-INF/views/partial/newmessage.html | 14 +++ 12 files changed, 176 insertions(+), 156 deletions(-) create mode 100644 juick-www/src/main/webapp/WEB-INF/views/index.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/macros/tags.html create mode 100644 juick-www/src/main/webapp/WEB-INF/views/partial/newmessage.html (limited to 'juick-www/src/main') 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 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 = ""; - } - templates.pageHead(out, visitor, title, head); - templates.pageNavigation(out, visitor, paramSearch); - - out.println("
"); - - if (paramShow == null && paramBefore == 0) { - out.println(""); - } - - if (visitor.getUid() > 0) { - out.println("
"); - out.println("
"); - if (messagesService.isReadonly()) { - out.println("

Комментирование временно недоступно

"); - } else { - out.println(" "); - out.println("
"); - out.println(" " + - "или загрузить
"); - out.println("
"); - out.println(" "); - out.println("
"); - } - out.println("
"); - out.println("
"); - } - - 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("

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

"); + String head = StringUtils.EMPTY; + if (paramBefore > 0 || paramShow != null) { + head = ""; + } + 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(""); + if (paramSearch != null) { + nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); } - out.println("
"); - - templates.pageHomeColumn(out, visitor, paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); - - 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().toString(); + if (sape != null && visitor.getUid() == 0 && queryString == null) { + String links = sape.getPageLinks(requestURI, sapeCookie).render(); + model.addAttribute("links", links); } + return "views/index"; } } 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 f7279fb8..d23544ba 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 @@ -185,15 +185,14 @@ public class NewMessage { } @PostMapping("/post") - public void doPostMessage(HttpServletRequest request, HttpServletResponse response, - @RequestParam(required = false) String img, + public void doPostMessage(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) String img, + @RequestParam String body, @RequestParam(required = false) MultipartFile attach) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } - String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; @@ -318,6 +317,7 @@ public class NewMessage { } @PostMapping("/comment") + @ResponseBody public String doPostComment( @RequestParam(required = false, defaultValue = "0") Integer mid, @RequestParam(required = false, defaultValue = "0") Integer rid, 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 index 27ad35b8..261efff3 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java @@ -28,9 +28,11 @@ import org.apache.commons.lang3.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; @@ -163,7 +165,12 @@ public class PageTemplates { String queryString = request.getQueryString(); String requestURI = request.getRequestURI(); if (sapeon && sape != null && (visitor.getUid() == 0 || visitor.getUid() == 1) && queryString == null) { - String links = sape.getPageLinks(requestURI, request.getCookies()).render(); + 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); } @@ -290,18 +297,9 @@ public class PageTemplates { } } - public void printMessages(PrintWriter out, com.juick.User user, List mids, com.juick.User visitor, int YandexID, int ad_mid) { + public void printMessages(PrintWriter out, com.juick.User user, List mids, com.juick.User visitor) { List msgs = messagesService.getMessages(mids); - for (int i = 0; i < msgs.size(); i++) { - com.juick.Message msg = msgs.get(i); - if (msg.getMid() == ad_mid) { - msgs.remove(i); - msgs.add(0, msg); - break; - } - } - List blUIDs = new ArrayList(20); if (visitor != null) { for (Message msg : msgs) { @@ -320,10 +318,6 @@ public class PageTemplates { if (msg.getPrivacy() < 0) { tagsStr += "friends"; } - if (msg.getMid() == ad_mid) { - tagsStr += "реклама"; - } - String txt; if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) { txt = MessageUtils.formatMessageCode(msg.getText()); 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 3116fd93..bc2679ef 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 @@ -17,7 +17,6 @@ */ package com.juick.www.controllers; -import com.juick.service.AdsService; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.util.UserUtils; @@ -52,8 +51,6 @@ public class Tags { @Inject TagService tagService; @Inject - AdsService adsService; - @Inject PageTemplates templates; @GetMapping("/tag/{tagName}") @@ -104,16 +101,7 @@ public class Tags { out.println("
"); if (mids.size() > 0) { - int vuid = visitor.getUid(); - int 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_uid == 0 ? 2 : 3, ad_mid); + templates.printMessages(out, null, mids, visitor); } if (mids.size() >= 20) { 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 3e566df9..bc5ebfe3 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 @@ -18,6 +18,7 @@ package com.juick.www.controllers; import com.juick.server.helpers.TagStats; +import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; @@ -28,10 +29,11 @@ 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.web.bind.annotation.*; +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.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -150,7 +152,7 @@ public class User { StringEscapeUtils.escapeHtml4(paramTag.getName()) + "

"); } - templates.printMessages(out, user, mids, visitor, visitor.getUid() == 0 ? 4 : 5, 0); + templates.printMessages(out, user, mids, visitor); if (mids.size() >= 20) { String nextpage = "?before=" + mids.get(mids.size() - 1); @@ -202,12 +204,11 @@ public class User { @GetMapping("/{uname}/friends") protected void doGetFriends(HttpServletRequest request, HttpServletResponse response, - @PathVariable String uname) throws ServletException, IOException { + @PathVariable String uname) 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 HttpForbiddenException(); } response.setContentType("text/html; charset=UTF-8"); @@ -240,12 +241,11 @@ public class User { @GetMapping("/{uname}/readers") protected void doGetReaders(HttpServletRequest request, HttpServletResponse response, - @PathVariable String uname) throws ServletException, IOException { + @PathVariable String uname) 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 HttpForbiddenException(); } response.setContentType("text/html; charset=UTF-8"); 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 0899f928..25460666 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 @@ -29,12 +29,9 @@ import com.juick.www.WebApp; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.*; import javax.inject.Inject; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -63,7 +60,7 @@ public class UserThread { @GetMapping("/{uname}/{mid}") protected void doGetThread(HttpServletRequest request, HttpServletResponse response, @PathVariable String uname, - @PathVariable int mid) throws ServletException, IOException { + @PathVariable int mid) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (!messagesService.canViewThread(mid, visitor.getUid())) { diff --git a/juick-www/src/main/java/ru/sape/Sape.java b/juick-www/src/main/java/ru/sape/Sape.java index c00054ae..38577c45 100644 --- a/juick-www/src/main/java/ru/sape/Sape.java +++ b/juick-www/src/main/java/ru/sape/Sape.java @@ -3,8 +3,6 @@ */ package ru.sape; -import javax.servlet.http.Cookie; - public class Sape { private final String sapeUser; @@ -19,7 +17,7 @@ public class Sape { } public boolean debug = false; - public SapePageLinks getPageLinks(String requestUri, Cookie[] cookies) { - return new SapePageLinks(sapePageLinkConnection, sapeUser, requestUri, cookies, debug); + public SapePageLinks getPageLinks(String requestUri, String cookie) { + return new SapePageLinks(sapePageLinkConnection, sapeUser, requestUri, cookie, debug); } } diff --git a/juick-www/src/main/java/ru/sape/SapePageLinks.java b/juick-www/src/main/java/ru/sape/SapePageLinks.java index 458358b8..e89b4e71 100644 --- a/juick-www/src/main/java/ru/sape/SapePageLinks.java +++ b/juick-www/src/main/java/ru/sape/SapePageLinks.java @@ -1,19 +1,18 @@ package ru.sape; import java.util.*; -import javax.servlet.http.Cookie; public class SapePageLinks { private boolean showCode; - public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, Cookie[] cookies) { - this(sapeConnection, sapeUser, requestUri, cookies, false); + public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie) { + this(sapeConnection, sapeUser, requestUri, sapeCookie, false); } @SuppressWarnings("unchecked") - public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, Cookie[] cookies, boolean showCode) { - if (sapeUser.equals(getCookieValue(cookies, "sape_cookie"))) { + public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie, boolean showCode) { + if (sapeUser.equals(sapeCookie)) { showCode = true; } @@ -74,18 +73,4 @@ public class SapePageLinks { return s.toString(); } - - private static String getCookieValue(Cookie[] cookies, String name) { - if (cookies == null) { - return null; - } - - for (Cookie cookie : cookies) { - if (cookie.getName().equals(name)) { - return cookie.getValue(); - } - } - - return null; - } } diff --git a/juick-www/src/main/webapp/WEB-INF/views/index.html b/juick-www/src/main/webapp/WEB-INF/views/index.html new file mode 100644 index 00000000..09297ef9 --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/index.html @@ -0,0 +1,75 @@ +{% extends "layouts/content" %} +{% import "views/macros/tags" %} +{% block content %} +{% if noindex %} + +{% endif %} +{% if visitor.getUid() > 0 %} +{% include "views/partial/newmessage" %} +{% endif %} +{% for msg in msgs %} +
+
@{{ msg.getUser().getName() }}: + +
+ +
+ {{ tags(msg.getUser().getName(), msg.getTags()) }} +
+
+ + {%if msg.AttachmentType is not empty %} +

+ +

+ {% endif %} +

{{ msg.getText() | raw }}

+ {%if msg.AttachmentType is not empty %} +
+ {% endif %} + {% if not readonly %} + + {% endif %} + +
+{% endfor %} +{% if next is not empty %} +{{ next | raw }} +{% endif %} +{% endblock %} +{% block "column" %} +{% include "views/partial/homecolumn" %} +{% if noindex %} + +{% endif %} +{% endblock %} \ No newline at end of file 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 new file mode 100644 index 00000000..ffd81ddd --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/macros/tags.html @@ -0,0 +1,5 @@ +{% macro tags(uname="", tagsList) %} +{% for tag in tagsList %} +{{ tag }} +{% endfor %} +{% endmacro %} \ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html b/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html index 71d675de..491cf140 100644 --- a/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html @@ -11,7 +11,7 @@ Facebook diff --git a/juick-www/src/main/webapp/WEB-INF/views/partial/newmessage.html b/juick-www/src/main/webapp/WEB-INF/views/partial/newmessage.html new file mode 100644 index 00000000..452d1267 --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/newmessage.html @@ -0,0 +1,14 @@ +
+
+ {% if readonly %} +

Комментирование временно недоступно

+ {% else %} + +
+ или загрузить
+
+ +
+ {% endif %} +
+
\ No newline at end of file -- cgit v1.2.3