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 | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index 1f2b7dc0..5d06045c 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -17,25 +17,21 @@ */ package com.juick.http.www; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.JSONSerializer; import com.juick.Tag; import com.juick.server.Endpoints; 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.json.JSONArray; +import org.json.JSONObject; 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; @@ -183,7 +179,7 @@ public class User { pageUserColumn(out, sql, user, visitor); out.println("<section id=\"content\">"); - out.println("<p>" + pageUserTags(sql, user, visitor, 0) + "</p>"); + out.println("<p>" + pageUserTags(sql, user, 0) + "</p>"); out.println("</section>"); PageTemplates.pageFooter(request, out, visitor, false); @@ -306,12 +302,16 @@ public class User { 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>"); + if (visitor != null && visitor.getUID() == user.getUID()) { + out.println(" <li><a href=\"/?show=mycomments\" rel=\"nofollow\">Мои комментарии</a></li>"); + out.println(" <li><a href=\"/?show=unanswered\" 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(" <p class=\"tags\">" + pageUserTags(sql, user, 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>"); @@ -337,7 +337,28 @@ public class User { out.println("</aside>"); } - public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { + public static String pageUserTags(JdbcTemplate sql, com.juick.User user, int cnt) { + List<Tag> tags = getUserTags(sql, user, cnt); + + int maxUsageCnt = tags.size() > 0 ? tags.stream().mapToInt(tag -> tag.UsageCnt).max().getAsInt() : 1; + + return tags.stream().map(tag -> "<a href=\"./?tag=" + URLEncoder.encode(tag.Name) + + "\" title=\"" + tag.UsageCnt + "\" rel=\"nofollow\">" + tag.Name + "</a>") + .collect(Collectors.joining(" ")); + + /* + todo: + 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 + " "; + } + }*/ + } + + public static List<Tag> getUserTags(JdbcTemplate sql, com.juick.User user, int cnt) { List<Tag> tags; if (cnt > 0) { tags = sql.query("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt " + @@ -361,22 +382,22 @@ public class User { return t; }), user.getUID()); } + return tags; + } - int maxUsageCnt = tags.size() > 0 ? tags.stream().mapToInt(tag -> tag.UsageCnt).max().getAsInt() : 1; - - return tags.stream().map(tag -> "<a href=\"./?tag=" + URLEncoder.encode(tag.Name) + - "\" title=\"" + tag.UsageCnt + "\" rel=\"nofollow\">" + tag.Name + "</a>") - .collect(Collectors.joining(" ")); - - /* - todo: - 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 + " "; + public void doMyTagsJson(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor != null) { + List<Tag> userTags = getUserTags(sql, visitor, 200); + JSONArray arr = new JSONArray(); + for (Tag userTag : userTags) { + arr.put(userTag.Name); } - }*/ + response.setContentType("text/json"); + response.setCharacterEncoding("UTF-8"); + response.getOutputStream().write(arr.toString().getBytes("UTF-8")); + } else { + response.sendError(404); + } } } |