/* * 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 . */ package com.juick.http.www; import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; import com.juick.xmpp.Stream; import org.apache.commons.lang3.tuple.Pair; import org.springframework.jdbc.core.JdbcTemplate; 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 java.util.List; import java.util.stream.Collectors; 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(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 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 = ""; if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) { head += ""; } else if (paramBefore > 0 || paramShow != null) { head += ""; } PageTemplates.pageHead(out, title, head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); if (mids.size() > 0) { out.println("
"); if (paramTag != null) { out.println("

← Все записи с тегом " + Utils.encodeHTML(paramTag.Name) + "

"); } 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("

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

"); } out.println("
"); } 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 = ""; PageTemplates.pageHead(out, "Теги " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); out.println("
"); out.println("

" + pageUserTags(sql, user, visitor, 0) + "

"); out.println("
"); 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 = ""; PageTemplates.pageHead(out, "Подписки " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); out.println("
"); out.println(""); List> friends = sql.query("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", (rs, num) -> { return Pair.of(rs.getInt(1), rs.getString(2)); }, user.getUID()); for (int cnt = 0; cnt < friends.size(); cnt++) { if (cnt % 3 == 0 && cnt > 0) { out.print(""); } out.print(""); } out.println("
" + friends.get(cnt).getRight() + "
"); out.println("
"); 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 = ""; PageTemplates.pageHead(out, "Читатели " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); out.println("
"); out.println(""); List> readers = sql.query("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", (rs, num) -> { return Pair.of(rs.getInt(1), rs.getString(2)); }, user.getUID()); for (int cnt = 0; cnt < readers.size(); cnt++) { if (cnt % 3 == 0 && cnt > 0) { out.print(""); } out.print(""); } out.println("
" + readers.get(cnt).getRight() + "
"); out.println("
"); 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(""); } public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { List tags; if (cnt > 0) { tags = sql.query("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 ?", (rs, num) -> { Tag t = new Tag(); t.Name = rs.getString(1); t.UsageCnt = rs.getInt(2); return t; }, user.getUID(), cnt); } else { tags = sql.query("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", ((rs, rowNum) -> { Tag t = new Tag(); t.Name = rs.getString(1); t.UsageCnt = rs.getInt(2); return t; }), user.getUID()); } int maxUsageCnt = tags.stream().mapToInt(tag -> tag.UsageCnt).max().getAsInt(); return tags.stream().map(tag -> "" + tag + "") .collect(Collectors.joining(" ")); /* todo: if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) { ret += "" + tag + " "; } else if (tags[i].UsageCnt > maxUsageCnt / 3) { ret += "" + tag + " "; } else { ret += tag + " "; } }*/ } }