diff options
author | Ugnich Anton | 2012-11-07 16:36:17 +0700 |
---|---|---|
committer | Ugnich Anton | 2012-11-07 16:36:17 +0700 |
commit | 52c8d049af6c0963b63dd1e70f66128527a7cba5 (patch) | |
tree | 733aebcdb621089ff9edabb85007dec50de11de2 /src/java/com/juick/http/www/UserThread.java | |
parent | 3b92419424bf770c6199b821ba0b7e98c7b1bd99 (diff) |
JS local time
auto-expand comments
Diffstat (limited to 'src/java/com/juick/http/www/UserThread.java')
-rw-r--r-- | src/java/com/juick/http/www/UserThread.java | 280 |
1 files changed, 148 insertions, 132 deletions
diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index ec3412f3..a0bb3a86 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -22,11 +22,7 @@ import com.juick.server.UserQueries; import java.io.IOException; import java.io.PrintWriter; 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.Locale; import java.util.ResourceBundle; import javax.servlet.ServletException; @@ -70,14 +66,14 @@ public class UserThread { try { PageTemplates.pageHead(out, title, null); PageTemplates.pageNavigation(out, locale, visitor); - PageTemplates.pageUserTitle(out, sql, locale, user, visitor); + //PageTemplates.pageUserTitle(out, sql, locale, user, visitor); out.println("<div id=\"topwrapper\">"); out.println("<div id=\"wrapper\">"); out.println("<div id=\"content\" style=\"margin-left: 0; width: 100%\">"); - printMessage(out, sql, MID, visitor, locale); - printReplies(out, sql, MID, visitor, locale, listview); + com.juick.Message msg = printMessage(out, sql, MID, visitor, locale); + printReplies(out, sql, msg, visitor, locale, listview); out.println("</div>"); out.println("</div>"); @@ -89,154 +85,163 @@ public class UserThread { } } - public static void printMessage(PrintWriter out, Connection sql, int mid, com.juick.User visitor, Locale locale) { + public static com.juick.Message printMessage(PrintWriter out, Connection sql, int mid, com.juick.User visitor, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); ResourceBundle rbthread = ResourceBundle.getBundle("UserThread", locale); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),messages.ts,messages.replies,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - int uid = rs.getInt(2); - String uname = rs.getString(3); - String tags = rs.getString(4); - String txt = rs.getString(7); - // timediff - // timestamp - // replies - // attach - // pid - // pname - // lat - // lon - - boolean cancomment = visitor != null; - - tags = (tags != null) ? PageTemplates.formatTags(new ArrayList<String>(Arrays.asList(tags.split(" ")))) : ""; - if (rs.getInt(5) == 1) { - tags += " *readonly"; - cancomment = false; - } - switch (rs.getInt(6)) { - case 2: - tags += " *public"; - break; - case -1: - tags += " *friends"; - break; - case -2: - tags += " *private"; - break; - } + com.juick.Message msg = MessagesQueries.getMessage(sql, mid); - txt = PageTemplates.formatMessage(txt); + msg.VisitorCanComment = visitor != null; - out.println("<ul>"); - out.println(" <li id=\"msg-" + mid + "\" class=\"msg msgthread\">"); - - out.println(" <div class=\"msg-avatar\"><a href=\"/" + uname + "/\"><img src=\"//i.juick.com/a/" + uid + ".png\" alt=\"" + uname + "\"/></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + uname + "/" + mid + "\" title=\"" + rs.getString(9) + "\">" + PageTemplates.formatDate(rs.getInt(8), rs.getString(9), locale) + "</a></div>"); - out.println(" <div class=\"msg-header\"><a href=\"/" + uname + "/\">@" + uname + "</a>:" + tags + "</div>"); - if (rs.getString(13) != null) { - out.println(" <div class=\"msg-place\">" + rs.getString(14) + "</div>"); - } - out.println(" <div class=\"msg-txt\">" + txt + "</div>"); + String tags = (msg.Tags.isEmpty()) ? "" : PageTemplates.formatTags(msg.Tags); + if (msg.ReadOnly) { + tags += " *readonly"; + msg.VisitorCanComment = false; + } + switch (msg.Privacy) { + case 2: + tags += " *public"; + break; + case -1: + tags += " *friends"; + break; + case -2: + tags += " *private"; + break; + } - if (rs.getString(11) != null) { - if (rs.getString(11).equals("jpg")) { - out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + mid + ".jpg\"><img src=\"//i.juick.com/photos-512/" + mid + ".jpg\" alt=\"\"/></a></div>"); - } else { - out.println(" <div class=\"msg-media\"><div id=\"video-" + mid + "\"><a href=\"//i.juick.com/video/" + mid + ".mp4\" onclick=\"inlinevideo(" + mid + "); return false\"><img src=\"//i.juick.com/thumbs/" + mid + ".jpg\" alt=\"\"/></a></div></div>"); - } - } + out.println("<ul>"); + out.println(" <li id=\"msg-" + mid + "\" class=\"msg msgthread\">"); + out.println(" <div class=\"msg-avatar\"><a href=\"/" + msg.User.UName + "/\"><img src=\"//i.juick.com/a/" + msg.User.UID + ".png\" alt=\"" + msg.User.UName + "\"/></a></div>"); + out.println(" <div class=\"msg-ts\">" + PageTemplates.formatJSLocalTime(msg.TimestampString) + "</div>"); + out.println(" <div class=\"msg-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:" + tags + "</div>"); + if (msg.Place != null && msg.Place.pid > 0) { + out.println(" <div class=\"msg-place\">" + msg.Place.name + "</div>"); + } + out.println(" <div class=\"msg-txt\">" + PageTemplates.formatMessage(msg.Text) + "</div>"); - boolean visitorInBL = UserQueries.isInBL(sql, uid, visitor.UID); + if (msg.AttachmentType != null) { + if (msg.AttachmentType.equals("jpg")) { + out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + mid + ".jpg\"><img src=\"//i.juick.com/photos-512/" + mid + ".jpg\" alt=\"\"/></a></div>"); + } else { + out.println(" <div class=\"msg-media\"><div id=\"video-" + mid + "\"><a href=\"//i.juick.com/video/" + mid + ".mp4\" onclick=\"inlinevideo(" + mid + "); return false\"><img src=\"//i.juick.com/thumbs/" + mid + ".jpg\" alt=\"\"/></a></div></div>"); + } + } + boolean visitorInBL = false; + if (visitor != null) { + if (visitor.UID == msg.User.UID) { + msg.VisitorCanComment = true; + } else { + visitorInBL = UserQueries.isInBL(sql, msg.User.UID, visitor.UID); if (visitorInBL) { - cancomment = false; - } - if (visitor != null && visitor.UID == uid) { - cancomment = true; + msg.VisitorCanComment = false; } + } + } - if (cancomment) { - out.println(" <form action=\"/post\" method=\"POST\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"mid\" value=\"" + mid + "\"/>"); - out.println(" <div class=\"msg-comment\"><textarea name=\"body\" rows=\"1\" class=\"reply\" placeholder=\"" + rb.getString("Add a comment") + "\" onkeypress=\"postformListener(this.form,event)\"></textarea></div>"); - out.println(" </form>"); - } + if (msg.VisitorCanComment) { + out.println(" <form action=\"/post\" method=\"POST\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"mid\" value=\"" + mid + "\"/>"); + out.println(" <div class=\"msg-comment\"><textarea name=\"body\" rows=\"1\" class=\"reply\" placeholder=\"" + rb.getString("Add a comment") + "\" onkeypress=\"postformListener(this.form,event)\"></textarea></div>"); + out.println(" </form>"); + } - ArrayList<String> recomm = MessagesQueries.getMessageRecommendations(sql, mid); - if (!recomm.isEmpty()) { - out.print(" <div class=\"" + (cancomment ? "msg-recomms" : "msg-comments") + "\">" + rb.getString("Recommended by") + " (" + recomm.size() + "): "); - for (int i = 0; i < recomm.size(); i++) { - if (i > 0) { - out.print(", "); - } - out.print("<a href=\"/" + recomm.get(i) + "/\">@" + recomm.get(i) + "</a>"); - } + ArrayList<String> recomm = MessagesQueries.getMessageRecommendations(sql, mid); + if (!recomm.isEmpty()) { + out.print(" <div class=\"" + (msg.VisitorCanComment ? "msg-recomms" : "msg-comments") + "\">" + rb.getString("Recommended by") + " (" + recomm.size() + "): "); + for (int i = 0; i < recomm.size(); i++) { + if (i > 0) { + out.print(", "); } + out.print("<a href=\"/" + recomm.get(i) + "/\">@" + recomm.get(i) + "</a>"); + } + } - out.println(" </li>"); - out.println(" <li class=\"mtoolbar\"><ul>"); - out.println(" <li><a href=\"/" + mid + "\"><div style=\"background-position: -64px 0\"></div>" + mid + "</a></li>"); - if (visitor != null) { - if (visitor.UID != uid) { - if (MessagesQueries.isSubscribed(sql, visitor.UID, mid)) { - out.println(" <li><a href=\"/post?body=U+%23" + mid + "\"><div style=\"background-position: -48px 0\"></div>" + rbthread.getString("(MToolbar) Subcribed") + "</a></li>"); - } else { - out.println(" <li><a href=\"/post?body=S+%23" + mid + "\"><div style=\"background-position: -16px 0\"></div>" + rbthread.getString("(MToolbar) Subcribe") + "</a></li>"); - } - if (!visitorInBL) { - out.println(" <li><a href=\"/post?body=%21+%23" + mid + "\"><div style=\"background-position: -32px 0\"></div>" + rbthread.getString("(MToolbar) Recommend") + "</a></li>"); - } - } else { - out.println(" <li><a href=\"/post?body=D+%23" + mid + "\"><div style=\"background-position: 0\"></div>" + rbthread.getString("(MToolbar) Delete") + "</a></li>"); - } + out.println(" </li>"); + out.println(" <li class=\"mtoolbar\"><ul>"); + out.println(" <li><a href=\"/" + mid + "\"><div style=\"background-position: -64px 0\"></div>" + mid + "</a></li>"); + if (visitor != null) { + if (visitor.UID != msg.User.UID) { + if (MessagesQueries.isSubscribed(sql, visitor.UID, mid)) { + out.println(" <li><a href=\"/post?body=U+%23" + mid + "\"><div style=\"background-position: -48px 0\"></div>" + rbthread.getString("(MToolbar) Subcribed") + "</a></li>"); + } else { + out.println(" <li><a href=\"/post?body=S+%23" + mid + "\"><div style=\"background-position: -16px 0\"></div>" + rbthread.getString("(MToolbar) Subcribe") + "</a></li>"); + } + if (!visitorInBL) { + out.println(" <li><a href=\"/post?body=%21+%23" + mid + "\"><div style=\"background-position: -32px 0\"></div>" + rbthread.getString("(MToolbar) Recommend") + "</a></li>"); } - out.println(" </ul></li>"); - out.println("</ul>"); + } else { + out.println(" <li><a href=\"/post?body=D+%23" + mid + "\"><div style=\"background-position: 0\"></div>" + rbthread.getString("(MToolbar) Delete") + "</a></li>"); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); } + out.println(" </ul></li>"); + out.println("</ul>"); + return msg; } - public static void printReplies(PrintWriter out, Connection sql, int mid, com.juick.User visitor, Locale locale, boolean listview) { + public static void printReplies(PrintWriter out, Connection sql, com.juick.Message msg, com.juick.User visitor, Locale locale, boolean listview) { ResourceBundle rbuser = ResourceBundle.getBundle("User", locale); - ArrayList<com.juick.Message> replies = MessagesQueries.getReplies(sql, mid); + ArrayList<com.juick.Message> replies = MessagesQueries.getReplies(sql, msg.MID); + ArrayList<Integer> blUIDs = new ArrayList<Integer>(); for (int i = 0; i < replies.size(); i++) { - com.juick.Message msg = replies.get(i); - if (msg.ReplyTo > 0) { + com.juick.Message reply = replies.get(i); + if (reply.User.UID != msg.User.UID && !blUIDs.contains(reply.User.UID)) { + blUIDs.add(reply.User.UID); + } + if (reply.ReplyTo > 0) { boolean added = false; for (int n = 0; n < replies.size(); n++) { - if (replies.get(n).RID == msg.ReplyTo) { - replies.get(n).childs.add(msg); + if (replies.get(n).RID == reply.ReplyTo) { + replies.get(n).childs.add(reply); added = true; break; } } if (!added) { - msg.ReplyTo = 0; + reply.ReplyTo = 0; } } } if (!replies.isEmpty()) { + if (visitor != null && msg.User.UID == visitor.UID) { + for (int i = 0; i < replies.size(); i++) { + replies.get(i).VisitorCanComment = true; + } + } else if (visitor != null && msg.VisitorCanComment) { + blUIDs = UserQueries.checkBL(sql, visitor.UID, blUIDs); + for (int i = 0; i < replies.size(); i++) { + com.juick.Message reply = replies.get(i); + reply.VisitorCanComment = reply.User.UID == visitor.UID || !blUIDs.contains(reply.User.UID); + } + } else { + for (int i = 0; i < replies.size(); i++) { + replies.get(i).VisitorCanComment = false; + } + } + + boolean foldable = false; + if (replies.size() > 10) { + for (int i = 0; i < replies.size() - 1; i++) { + if (replies.get(i).getChildsCount() > 1) { + foldable = true; + break; + } + } + } out.println("<div class=\"title2\">"); out.print(" <div class=\"title2-right\">"); if (listview) { out.print("<a href=\"?view=tree\">" + rbuser.getString("View as tree") + "</a>"); } else { - out.print("<span id=\"unfoldall\"><a href=\"#\" onclick=\"$('#replies>li').show(); $('#replies .msg-comments').hide(); $('#unfoldall').hide(); return false\">" + rbuser.getString("Expand all") + "</a> · </span><a href=\"?view=list\">" + rbuser.getString("View as list") + "</a>"); + if (foldable) { + out.print("<span id=\"unfoldall\"><a href=\"#\" onclick=\"$('#replies>li').show(); $('#replies .msg-comments').hide(); $('#unfoldall').hide(); return false\">" + rbuser.getString("Expand all") + "</a> · </span>"); + } + out.print("<a href=\"?view=list\">" + rbuser.getString("View as list") + "</a>"); } out.print("</div>"); out.println(" <h2>" + rbuser.getString("Replies") + " (" + replies.size() + ")</h2>"); @@ -246,18 +251,18 @@ public class UserThread { if (listview) { printList(out, replies, locale); } else { - printTree(out, replies, 0, 0, locale); + printTree(out, replies, 0, 0, false, locale); } out.println("</ul>"); - } - for (int i = 0; i < replies.size(); i++) { - replies.get(i).cleanupChilds(); + for (int i = 0; i < replies.size(); i++) { + replies.get(i).cleanupChilds(); + } + replies.clear(); } - replies.clear(); } - public static void printTree(PrintWriter out, ArrayList<com.juick.Message> replies, int ReplyTo, int margin, Locale locale) { + public static void printTree(PrintWriter out, ArrayList<com.juick.Message> replies, int ReplyTo, int margin, boolean hidden, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); if (margin > 420) { @@ -273,7 +278,10 @@ public class UserThread { out.print("border: 0;"); } if (margin > 0) { - out.print("margin-left: " + margin + "px;display:none;"); + out.print("margin-left: " + margin + "px;"); + } + if (hidden) { + out.print("display:none;"); } out.println("\">"); if (msg.AttachmentType != null) { @@ -284,20 +292,26 @@ public class UserThread { } } out.println(" <div class=\"msg-avatar\"><a href=\"/" + msg.User.UName + "/\"><img src=\"//i.juick.com/a/" + msg.User.UID + ".png\" alt=\"" + msg.User.UName + "\"/></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.MID + "#" + msg.RID + "\" title=\"" + msg.TimestampString + "\">" + PageTemplates.formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a></div>"); + out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.MID + "#" + msg.RID + "\" title=\"" + msg.TimestampString + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a></div>"); out.println(" <div class=\"msg-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:</div>"); out.println(" <div class=\"msg-txt\">" + PageTemplates.formatMessage(msg.Text) + "</div>"); - out.println(" <div class=\"msg-links\"><a href=\"#\" onclick=\"return showCommentForm(" + msg.MID + "," + msg.RID + ")\">" + rb.getString("Comment") + "</a></div>"); - out.println(" <div class=\"msg-comment\" style=\"display: none\"></div>"); - if (ReplyTo == 0) { - int childs = msg.getChildsCount() - 1; - if (childs > 0) { - out.println(" <div class=\"msg-comments\"><a href=\"#\" onclick=\"return showMoreReplies(" + msg.RID + ")\">" + PageTemplates.formatReplies(childs, locale) + " more</a></div>"); - } + if (msg.VisitorCanComment) { + out.println(" <div class=\"msg-links\"><a href=\"#\" onclick=\"return showCommentForm(" + msg.MID + "," + msg.RID + ")\">" + rb.getString("Comment") + "</a></div>"); + out.println(" <div class=\"msg-comment\" style=\"display: none\"></div>"); + } + + int childs = msg.getChildsCount(); + if (ReplyTo == 0 && childs > 1 && replies.size() > 10) { + out.println(" <div class=\"msg-comments\"><a href=\"#\" onclick=\"return showMoreReplies(" + msg.RID + ")\">" + PageTemplates.formatReplies(childs, locale) + " more</a></div>"); + } out.println(" </li>"); - printTree(out, replies, msg.RID, margin + 20, locale); + if (ReplyTo == 0 && childs > 1 && replies.size() > 10) { + printTree(out, msg.childs, msg.RID, margin + 20, true, locale); + } else if (childs > 0) { + printTree(out, msg.childs, msg.RID, margin + 20, hidden, locale); + } } } } @@ -317,15 +331,17 @@ public class UserThread { } } out.println(" <div class=\"msg-avatar\"><a href=\"/" + msg.User.UName + "/\"><img src=\"//i.juick.com/a/" + msg.User.UID + ".png\"></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.MID + "#" + msg.RID + "\" title=\"" + msg.TimestampString + "\">" + PageTemplates.formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a></div>"); + out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.MID + "#" + msg.RID + "\" title=\"" + msg.TimestampString + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a></div>"); out.println(" <div class=\"msg-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:</div>"); out.println(" <div class=\"msg-txt\">" + PageTemplates.formatMessage(msg.Text) + "</div>"); out.print(" <div class=\"msg-links\">/" + msg.RID); if (msg.ReplyTo > 0) { out.print(" " + rb.getString("in reply to") + " <a href=\"#" + msg.ReplyTo + "\">/" + msg.ReplyTo + "</a>"); } - out.println(" · <a href=\"#\" onclick=\"return showCommentForm(" + msg.MID + "," + msg.RID + ")\">" + rb.getString("Comment") + "</a></div>"); - out.println(" <div class=\"msg-comment\" style=\"display: none\"></div>"); + if (msg.VisitorCanComment) { + out.println(" · <a href=\"#\" onclick=\"return showCommentForm(" + msg.MID + "," + msg.RID + ")\">" + rb.getString("Comment") + "</a></div>"); + out.println(" <div class=\"msg-comment\" style=\"display: none\"></div>"); + } out.println(" </li>"); } } |