aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-04-19 14:51:48 +0300
committerGravatar Vitaly Takmazov2017-05-05 12:06:28 +0300
commit045ca0848c8ee7c771541a5829157531c782cf30 (patch)
tree9a60049dae790fd01ace40d3757c0bfda74cd220 /juick-www/src/main/java/com/juick/www/controllers
parent9345af5e39695d191b221473ce3a51b03dc09db3 (diff)
juick-www: Home controller uses Pebble, drop ads
wip
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/controllers')
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/Home.java138
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/NewMessage.java6
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java24
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/Tags.java14
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/User.java18
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/UserThread.java7
6 files changed, 75 insertions, 132 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 += "&amp;show=" + paramShow;
- }
- if (paramSearch != null) {
- nextpage += "&amp;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 += "&amp;show=" + paramShow;
}
-
- if (paramShow == null && paramBefore == 0) {
- out.println("<!--/noindex-->");
+ if (paramSearch != null) {
+ nextpage += "&amp;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";
}
}
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("<br/>Спонсоры: " + links);
}
@@ -290,18 +297,9 @@ public class PageTemplates {
}
}
- public void printMessages(PrintWriter out, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) {
+ public void printMessages(PrintWriter out, com.juick.User user, List<Integer> mids, com.juick.User visitor) {
List<com.juick.Message> 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<Integer> blUIDs = new ArrayList<Integer>(20);
if (visitor != null) {
for (Message msg : msgs) {
@@ -320,10 +318,6 @@ public class PageTemplates {
if (msg.getPrivacy() < 0) {
tagsStr += "<a>friends</a>";
}
- if (msg.getMid() == ad_mid) {
- tagsStr += "<a>реклама</a>";
- }
-
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("<section id=\"content\">");
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()) + "</b></a></p>");
}
- 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())) {