aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/User.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/User.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/User.java343
1 files changed, 343 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/User.java b/juick-www/src/main/java/com/juick/www/User.java
new file mode 100644
index 00000000..549e38af
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/User.java
@@ -0,0 +1,343 @@
+/*
+ * Juick
+ * Copyright (C) 2008-2011, Ugnich Anton
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package com.juick.www;
+
+import com.juick.Tag;
+import com.juick.server.MessagesQueries;
+import com.juick.server.TagQueries;
+import com.juick.server.UserQueries;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+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.URLEncoder;
+import java.util.List;
+
+/**
+ *
+ * @author Ugnich Anton
+ */
+public class User {
+
+ protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
+ com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+
+ List<Integer> mids;
+
+ String paramShow = request.getParameter("show");
+
+ com.juick.Tag paramTag = null;
+ String paramTagStr = request.getParameter("tag");
+ if (paramTagStr != null) {
+ if (paramTagStr.length() < 64) {
+ paramTag = TagQueries.getTag(sql, paramTagStr, false);
+ }
+ if (paramTag == null) {
+ Errors.doGet404(sql, request, response);
+ return;
+ } else if (!paramTag.Name.equals(paramTagStr)) {
+ String url = "/" + user.getUName() + "/?tag=" + URLEncoder.encode(paramTag.Name, "UTF-8");
+ Utils.sendPermanentRedirect(response, url);
+ return;
+ }
+ }
+
+ int paramBefore = 0;
+ String paramBeforeStr = request.getParameter("before");
+ if (paramBeforeStr != null) {
+ try {
+ paramBefore = Integer.parseInt(paramBeforeStr);
+ } catch (NumberFormatException e) {
+ }
+ }
+
+ String paramSearch = request.getParameter("search");
+ if (paramSearch != null && paramSearch.length() > 64) {
+ paramSearch = null;
+ }
+
+ int privacy = 0;
+ if (visitor != null) {
+ if (user.getUID() == visitor.getUID() || visitor.getUID() == 1) {
+ privacy = -3;
+ } else if (UserQueries.isInWL(sql, user.getUID(), visitor.getUID())) {
+ privacy = -2;
+ }
+ }
+
+ String title;
+ if (paramShow == null) {
+ if (paramTag != null) {
+ title = "Блог " + user.getUName() + ": *" + Utils.encodeHTML(paramTag.Name);
+ mids = MessagesQueries.getUserTag(sql, user.getUID(), paramTag.TID, privacy, paramBefore);
+ } else if (paramSearch != null) {
+ title = "Блог " + user.getUName() + ": " + Utils.encodeHTML(paramSearch);
+ mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.getUID(), Utils.encodeSphinx(paramSearch), privacy, paramBefore);
+ } else {
+ title = "Блог " + user.getUName();
+ mids = MessagesQueries.getUserBlog(sql, user.getUID(), privacy, paramBefore);
+ }
+ } else if (paramShow.equals("recomm")) {
+ title = "Рекомендации " + user.getUName();
+ mids = MessagesQueries.getUserRecommendations(sql, user.getUID(), paramBefore);
+ } else if (paramShow.equals("photos")) {
+ title = "Фотографии " + user.getUName();
+ mids = MessagesQueries.getUserPhotos(sql, user.getUID(), privacy, paramBefore);
+ } else {
+ Errors.doGet404(sql, request, response);
+ return;
+ }
+
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.getUID());
+ }
+
+ response.setContentType("text/html; charset=UTF-8");
+ try (PrintWriter out = response.getWriter()) {
+ String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.getUName() + "\" href=\"//rss.juick.com/" + user.getUName() + "/blog\"/>";
+ if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) {
+ head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
+ } else if (paramBefore > 0 || paramShow != null) {
+ head += "<meta name=\"robots\" content=\"noindex\"/>";
+ }
+ PageTemplates.pageHead(out, title, head);
+ PageTemplates.pageNavigation(out, visitor, null);
+ pageUserColumn(out, sql, user, visitor);
+
+ if (mids.size() > 0) {
+ out.println("<section id=\"content\">");
+
+ if (paramTag != null) {
+ out.println("<p class=\"page\"><a href=\"/tag/" + URLEncoder.encode(paramTag.Name, "UTF-8") + "\">← Все записи с тегом <b>" + Utils.encodeHTML(paramTag.Name) + "</b></a></p>");
+ }
+
+ PageTemplates.printMessages(out, sql, user, mids, visitor, visitor == null ? 4 : 5, 0);
+
+ if (mids.size() >= 20) {
+ String nextpage = "?before=" + mids.get(mids.size() - 1);
+ if (paramShow != null) {
+ nextpage += "&amp;show=" + paramShow;
+ }
+ if (paramTag != null) {
+ nextpage += "&amp;tag=" + URLEncoder.encode(paramTag.Name, "UTF-8");
+ }
+ if (paramSearch != null) {
+ nextpage += "&amp;search=" + URLEncoder.encode(paramSearch, "UTF-8");
+ }
+ out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>");
+ }
+
+ out.println("</section>");
+ }
+
+ PageTemplates.pageFooter(request, out, visitor, true);
+ PageTemplates.pageEnd(out);
+ }
+ }
+
+ protected void doGetTags(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
+ com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.getUID());
+ }
+
+ response.setContentType("text/html; charset=UTF-8");
+ try (PrintWriter out = response.getWriter()) {
+ String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
+ PageTemplates.pageHead(out, "Теги " + user.getUName(), head);
+ PageTemplates.pageNavigation(out, visitor, null);
+ pageUserColumn(out, sql, user, visitor);
+
+ out.println("<section id=\"content\">");
+ out.println("<p>" + pageUserTags(sql, user, visitor, 0) + "</p>");
+ out.println("</section>");
+
+ PageTemplates.pageFooter(request, out, visitor, false);
+ PageTemplates.pageEnd(out);
+ }
+ }
+
+ protected void doGetFriends(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
+ com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.getUID());
+ }
+
+ response.setContentType("text/html; charset=UTF-8");
+ try (PrintWriter out = response.getWriter()) {
+ String head = "<meta name=\"robots\" content=\"noindex\"/>";
+ PageTemplates.pageHead(out, "Подписки " + user.getUName(), head);
+ PageTemplates.pageNavigation(out, visitor, null);
+ pageUserColumn(out, sql, user, visitor);
+
+ out.println("<section id=\"content\">");
+ out.println("<table class=\"users\"><tr>");
+
+ List<com.juick.User> friends = UserQueries.getUserFriends(sql, user.getUID());
+ for (int i = 0; i < friends.size(); i++) {
+ if (i % 3 == 0 && i > 0) {
+ out.print("</tr><tr>");
+ }
+ out.print("<td><a href=\"/" + friends.get(i).getUName()
+ + "/\"><img src=\"//i.juick.com/as/" + friends.get(i).getUID() + ".png\"/>"
+ + friends.get(i).getUName() + "</a></td>");
+ }
+
+ out.println("</tr></table>");
+ out.println("</section>");
+
+ PageTemplates.pageFooter(request, out, visitor, false);
+ PageTemplates.pageEnd(out);
+ }
+ }
+
+ protected void doGetReaders(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
+ com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.getUID());
+ }
+
+ response.setContentType("text/html; charset=UTF-8");
+ try (PrintWriter out = response.getWriter()) {
+ String head = "<meta name=\"robots\" content=\"noindex\"/>";
+ PageTemplates.pageHead(out, "Читатели " + user.getUName(), head);
+ PageTemplates.pageNavigation(out, visitor, null);
+ pageUserColumn(out, sql, user, visitor);
+
+ out.println("<section id=\"content\">");
+ out.println("<table class=\"users\"><tr>");
+
+ List<com.juick.User> readers = UserQueries.getUserReaders(sql, user.getUID());
+ for (int i = 0; i < readers.size(); i++) {
+ if (i % 3 == 0 && i > 0) {
+ out.print("</tr><tr>");
+ }
+ out.print("<td><a href=\"/" + readers.get(i).getUName()
+ + "/\"><img src=\"//i.juick.com/as/" + readers.get(i).getUID() + ".png\"/>"
+ + readers.get(i).getUName() + "</a></td>");
+ }
+
+ out.println("</tr></table>");
+ out.println("</section>");
+
+ PageTemplates.pageFooter(request, out, visitor, false);
+ PageTemplates.pageEnd(out);
+ }
+ }
+
+ public static void pageUserRefCookie(HttpServletRequest request, HttpServletResponse response, int uid) {
+ String hReferer = request.getHeader("Referer");
+ String ref = Utils.getCookie(request, "ref");
+
+ if (ref == null && (hReferer == null || !(hReferer.startsWith("http://juick.com/") || hReferer.startsWith("https://juick.com/")))) {
+ Cookie c = new Cookie("ref", Integer.toString(uid));
+ c.setMaxAge(7 * 24 * 60 * 60);
+ c.setPath("/");
+ response.addCookie(c);
+ }
+ }
+
+ public static void pageUserColumn(PrintWriter out, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) {
+ out.println("<aside id=\"column\">");
+ out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUID() + ".png\" alt=\"\"/>" + user.getUName() + "</a></div>");
+ if (visitor != null && visitor.getUID() > 0 && visitor.getUID() != user.getUID()) {
+ out.println(" <ul id=\"ctoolbar\">");
+ if (UserQueries.isSubscribed(sql, visitor.getUID(), user.getUID())) {
+ out.println(" <li><a href=\"/post?body=U+%40" + user.getUName() + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>");
+ } else {
+ out.println(" <li><a href=\"/post?body=S+%40" + user.getUName() + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>");
+ }
+ if (UserQueries.isInBL(sql, visitor.getUID(), user.getUID())) {
+ out.println(" <li><a href=\"/post?body=BL+%40" + user.getUName() + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>");
+ } else {
+ out.println(" <li><a href=\"/post?body=BL+%40" + user.getUName() + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>");
+ }
+ if (!UserQueries.isInBLAny(sql, user.getUID(), visitor.getUID())) {
+ out.println(" <li><a href=\"/pm/sent?uname=" + user.getUName() + "\" title=\"Написать приватное сообщение\"><div style=\"background-position: -112px 0\"></div></a></li>");
+ }
+ out.println(" </ul>");
+ } else {
+ out.println(" <hr/>");
+ }
+ out.println(" <ul>");
+ out.println(" <li><a href=\"./\">Блог</a></li>");
+ out.println(" <li><a href=\"./?show=recomm\" rel=\"nofollow\">Рекомендации</a></li>");
+ out.println(" <li><a href=\"./?show=photos\" rel=\"nofollow\">Фотографии</a></li>");
+ out.println(" </ul>");
+ out.println(" <hr/>");
+ out.println(" <form action=\"./\">");
+ out.println(" <p><input type=\"text\" name=\"search\" class=\"inp\" placeholder=\"Поиск\"/></p>");
+ out.println(" </form>");
+ out.println(" <p class=\"tags\">" + pageUserTags(sql, user, visitor, 20) + "<a href=\"./tags\" rel=\"nofollow\">...</a></p>");
+ out.println(" <hr/>");
+ out.println(" <div id=\"ustats\"><ul>");
+ out.println(" <li><a href=\"./friends\">Я читаю: " + UserQueries.getStatsIRead(sql, user.getUID()) + "</a></li>");
+ out.println(" <li><a href=\"./readers\">Мои подписчики: " + UserQueries.getStatsMyReaders(sql, user.getUID()) + "</a></li>");
+ out.println(" <li>Сообщений: " + UserQueries.getStatsMessages(sql, user.getUID()) + "</li>");
+ out.println(" <li>Комментариев: " + UserQueries.getStatsReplies(sql, user.getUID()) + "</li>");
+ out.println(" </ul>");
+
+ List<com.juick.User> iread = UserQueries.getUserReadLeastPopular(sql, user.getUID(), 8);
+ if (!iread.isEmpty()) {
+ out.println("<table class=\"iread\"><tr>");
+ for (int i = 0; i < iread.size(); i++) {
+ if (i == 4) {
+ out.println("</tr><tr>");
+ }
+ com.juick.User u = iread.get(i);
+ out.println("<td><a href=\"/" + u.getUName() + "/\"><img src=\"//i.juick.com/a/" + u.getUID() + ".png\" alt=\"" + u.getUName() + "\"/></a></td>");
+ }
+ out.println("</tr></table>");
+ }
+
+ out.println(" </div>");
+ out.println("</aside>");
+ }
+
+ public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) {
+ List<Tag> tags = TagQueries.getUserTagsAll(sql, user.getUID());
+ int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).orElse(0);
+ String ret = "";
+ int count = Math.min(tags.size(), cnt);
+ for (int i = 0; i < count; i++) {
+ String tag = Utils.encodeHTML(tags.get(i).Name);
+ try {
+ tag = "<a href=\"./?tag=" + URLEncoder.encode(tags.get(i).Name, "UTF-8") + "\" title=\""
+ + tags.get(i).UsageCnt + "\" rel=\"nofollow\">" + tag + "</a>";
+ } catch (UnsupportedEncodingException e) {
+ }
+
+ if (tags.get(i).UsageCnt > maxUsageCnt / 3 * 2) {
+ ret += "<big>" + tag + "</big> ";
+ } else if (tags.get(i).UsageCnt > maxUsageCnt / 3) {
+ ret += "<small>" + tag + "</small> ";
+ } else {
+ ret += tag + " ";
+ }
+ }
+ return ret;
+ }
+}