diff options
Diffstat (limited to 'src/main/java/com/juick/http/www/User.java')
-rw-r--r-- | src/main/java/com/juick/http/www/User.java | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java new file mode 100644 index 00000000..83601ade --- /dev/null +++ b/src/main/java/com/juick/http/www/User.java @@ -0,0 +1,413 @@ +/* + * 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.http.www; + +import com.juick.server.MessagesQueries; +import com.juick.server.TagQueries; +import com.juick.server.UserQueries; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Ugnich Anton + */ +public class User { + + protected void doGetBlog(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + + ArrayList<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.UName + "/?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.UID == visitor.UID || visitor.UID == 1) { + privacy = -3; + } else if (UserQueries.isInWL(sql, user.UID, visitor.UID)) { + privacy = -2; + } + } + + String title; + if (paramShow == null) { + if (paramTag != null) { + title = "Блог " + user.UName + ": *" + Utils.encodeHTML(paramTag.Name); + mids = MessagesQueries.getUserTag(sql, user.UID, paramTag.TID, privacy, paramBefore); + } else if (paramSearch != null) { + title = "Блог " + user.UName + ": " + Utils.encodeHTML(paramSearch); + mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.UID, Utils.encodeSphinx(paramSearch), privacy, paramBefore); + } else { + title = "Блог " + user.UName; + mids = MessagesQueries.getUserBlog(sql, user.UID, privacy, paramBefore); + } + } else if (paramShow.equals("recomm")) { + title = "Рекомендации " + user.UName; + mids = MessagesQueries.getUserRecommendations(sql, user.UID, paramBefore); + } else if (paramShow.equals("photos")) { + title = "Фотографии " + user.UName; + mids = MessagesQueries.getUserPhotos(sql, user.UID, privacy, paramBefore); + } else { + Errors.doGet404(sql, request, response); + return; + } + + if (visitor == null) { + pageUserRefCookie(request, response, user.UID); + } + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.UName + "\" href=\"//rss.juick.com/" + user.UName + "/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 += "&show=" + paramShow; + } + if (paramTag != null) { + nextpage += "&tag=" + URLEncoder.encode(paramTag.Name, "UTF-8"); + } + if (paramSearch != null) { + nextpage += "&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); + } finally { + out.close(); + } + } + + protected void doGetTags(Connection 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.UID); + } + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; + PageTemplates.pageHead(out, "Теги " + user.UName, 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); + } finally { + out.close(); + } + } + + protected void doGetFriends(Connection 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.UID); + } + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + String head = "<meta name=\"robots\" content=\"noindex\"/>"; + PageTemplates.pageHead(out, "Подписки " + user.UName, head); + PageTemplates.pageNavigation(out, visitor, null); + pageUserColumn(out, sql, user, visitor); + + out.println("<section id=\"content\">"); + out.println("<table class=\"users\"><tr>"); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.user_id=users.id WHERE subscr_users.suser_id=? ORDER BY users.nick"); + stmt.setInt(1, user.UID); + rs = stmt.executeQuery(); + rs.beforeFirst(); + int cnt = 0; + while (rs.next()) { + if (cnt % 3 == 0 && cnt > 0) { + out.print("</tr><tr>"); + } + out.print("<td><a href=\"/" + rs.getString(2) + "/\"><img src=\"//i.juick.com/as/" + rs.getInt(1) + ".png\"/>" + rs.getString(2) + "</a></td>"); + cnt++; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + out.println("</tr></table>"); + out.println("</section>"); + + PageTemplates.pageFooter(request, out, visitor, false); + PageTemplates.pageEnd(out); + } finally { + out.close(); + } + } + + protected void doGetReaders(Connection 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.UID); + } + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + String head = "<meta name=\"robots\" content=\"noindex\"/>"; + PageTemplates.pageHead(out, "Читатели " + user.UName, head); + PageTemplates.pageNavigation(out, visitor, null); + pageUserColumn(out, sql, user, visitor); + + out.println("<section id=\"content\">"); + out.println("<table class=\"users\"><tr>"); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.suser_id=users.id WHERE subscr_users.user_id=? ORDER BY users.nick"); + stmt.setInt(1, user.UID); + rs = stmt.executeQuery(); + rs.beforeFirst(); + int cnt = 0; + while (rs.next()) { + if (cnt % 3 == 0 && cnt > 0) { + out.print("</tr><tr>"); + } + out.print("<td><a href=\"/" + rs.getString(2) + "/\"><img src=\"//i.juick.com/as/" + rs.getInt(1) + ".png\"/>" + rs.getString(2) + "</a></td>"); + cnt++; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + out.println("</tr></table>"); + out.println("</section>"); + + PageTemplates.pageFooter(request, out, visitor, false); + PageTemplates.pageEnd(out); + } finally { + out.close(); + } + } + + 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, Connection 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.UID + ".png\" alt=\"\"/>" + user.UName + "</a></div>"); + if (visitor != null && visitor.UID > 0 && visitor.UID != user.UID) { + out.println(" <ul id=\"ctoolbar\">"); + if (UserQueries.isSubscribed(sql, visitor.UID, user.UID)) { + out.println(" <li><a href=\"/post?body=U+%40" + user.UName + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>"); + } else { + out.println(" <li><a href=\"/post?body=S+%40" + user.UName + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>"); + } + if (UserQueries.isInBL(sql, visitor.UID, user.UID)) { + out.println(" <li><a href=\"/post?body=BL+%40" + user.UName + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>"); + } else { + out.println(" <li><a href=\"/post?body=BL+%40" + user.UName + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>"); + } + if (!UserQueries.isInBLAny(sql, user.UID, visitor.UID)) { + out.println(" <li><a href=\"/pm/sent?uname=" + user.UName + "\" 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.UID) + "</a></li>"); + out.println(" <li><a href=\"./readers\">Мои подписчики: " + UserQueries.getStatsMyReaders(sql, user.UID) + "</a></li>"); + out.println(" <li>Сообщений: " + UserQueries.getStatsMessages(sql, user.UID) + "</li>"); + out.println(" <li>Комментариев: " + UserQueries.getStatsReplies(sql, user.UID) + "</li>"); + out.println(" </ul>"); + + ArrayList<com.juick.User> iread = UserQueries.getUserReadLeastPopular(sql, user.UID, 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.UName + "/\"><img src=\"//i.juick.com/a/" + u.UID + ".png\" alt=\"" + u.UName + "\"/></a></td>"); + } + out.println("</tr></table>"); + } + + out.println(" </div>"); + out.println("</aside>"); + } + + public static String pageUserTags(Connection sql, com.juick.User user, com.juick.User visitor, int cnt) { + com.juick.Tag tags[] = null; + + int maxUsageCnt = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (cnt > 0) { + stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC LIMIT ?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + stmt.setInt(1, user.UID); + stmt.setInt(2, cnt); + } else { + stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + stmt.setInt(1, user.UID); + } + rs = stmt.executeQuery(); + rs.last(); + tags = new com.juick.Tag[rs.getRow()]; + rs.beforeFirst(); + cnt = 0; + while (rs.next()) { + tags[cnt] = new com.juick.Tag(); + tags[cnt].Name = rs.getString(1); + tags[cnt].UsageCnt = rs.getInt(2); + if (tags[cnt].UsageCnt > maxUsageCnt) { + maxUsageCnt = tags[cnt].UsageCnt; + } + cnt++; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (tags != null && cnt > 0) { + Arrays.sort(tags, 0, cnt); + } + + String ret = ""; + for (int i = 0; i < cnt; i++) { + String tag = Utils.encodeHTML(tags[i].Name); + try { + tag = "<a href=\"./?tag=" + URLEncoder.encode(tags[i].Name, "UTF-8") + "\" title=\"" + tags[i].UsageCnt + "\" rel=\"nofollow\">" + tag + "</a>"; + } catch (UnsupportedEncodingException e) { + } + + if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) { + ret += "<big>" + tag + "</big> "; + } else if (tags[i].UsageCnt > maxUsageCnt / 3) { + ret += "<small>" + tag + "</small> "; + } else { + ret += tag + " "; + } + } + return ret; + } +} |