diff options
author | 2012-11-02 18:33:10 +0700 | |
---|---|---|
committer | 2012-11-02 18:33:10 +0700 | |
commit | 3b92419424bf770c6199b821ba0b7e98c7b1bd99 (patch) | |
tree | 56051d952599dc13a8c26d94fd9f4ac6d1d44651 /src/java/com/juick/http | |
parent | b895a6e7a29193754a97e33b6af8bd66f1006fc4 (diff) |
BL-sensible comment form
Diffstat (limited to 'src/java/com/juick/http')
-rw-r--r-- | src/java/com/juick/http/www/PageTemplates.java | 134 | ||||
-rw-r--r-- | src/java/com/juick/http/www/UserThread.java | 77 |
2 files changed, 87 insertions, 124 deletions
diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java index ba5665ed..41646e30 100644 --- a/src/java/com/juick/http/www/PageTemplates.java +++ b/src/java/com/juick/http/www/PageTemplates.java @@ -17,6 +17,7 @@ */ package com.juick.http.www; +import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -161,15 +162,14 @@ public class PageTemplates { out.println("</div></div>"); } - public static String formatTags(String tags) { + public static String formatTags(ArrayList<String> tags) { String ret = ""; - String tagsarr[] = tags.split(" "); - for (int i = 0; i < tagsarr.length; i++) { - String tag = tagsarr[i]; + for (int i = 0; i < tags.size(); i++) { + String tag = tags.get(i); tag = tag.replaceAll("<", "<"); tag = tag.replaceAll(">", ">"); try { - ret += " *<a href=\"/?tag=" + URLEncoder.encode(tagsarr[i], "utf-8") + "\">" + tag + "</a>"; + ret += " *<a href=\"/?tag=" + URLEncoder.encode(tags.get(i), "utf-8") + "\">" + tag + "</a>"; } catch (UnsupportedEncodingException e) { } } @@ -277,85 +277,61 @@ public class PageTemplates { public static void printMessages(PrintWriter out, Connection sql, ArrayList<Integer> mids, com.juick.User visitor, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", 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_txt.repliesby,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 IN (" + Utils.convertArray2String(mids) + ") ORDER BY messages.message_id DESC"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - int mid = rs.getInt(1); - int uid = rs.getInt(2); - String uname = rs.getString(3); - String tags = rs.getString(4); - String txt = rs.getString(7); - // timediff - // timestamp - // replies - // 11 repliesby - // attach - // pid - // pname - // lat - // lon - - boolean cancomment = visitor != null; - - tags = (tags != null) ? formatTags(tags) : ""; - if (rs.getInt(5) == 1) { - tags += " *readonly"; - if (visitor != null && visitor.UID != uid) { - cancomment = false; - } - } - switch (rs.getInt(6)) { - case 2: - tags += " *public"; - break; - case -1: - tags += " *friends"; - break; - case -2: - tags += " *private"; - break; - } + ArrayList<com.juick.Message> msgs = MessagesQueries.getMessages(sql, mids); - txt = formatMessage(txt); + ArrayList<Integer> blUIDs = new ArrayList<Integer>(20); + for (int i = 0; i < mids.size(); i++) { + blUIDs.add(msgs.get(i).User.UID); + } + blUIDs = UserQueries.checkBL(sql, visitor.UID, blUIDs); - out.println(" <li id=\"msg-" + mid + "\" class=\"msg\">"); - 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) + "\">" + 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(14) != null) { - out.println(" <div class=\"msg-place\">" + rs.getString(14) + "</div>"); - } - out.println(" <div class=\"msg-txt\">" + txt + "</div>"); - - if (rs.getString(12) != null) { - if (rs.getString(12).equals("jpg")) { - out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/photos-1024/" + 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>"); - } - } + for (int i = 0; i < msgs.size(); i++) { + com.juick.Message msg = msgs.get(i); + + String tags = msg.Tags.isEmpty() ? "" : formatTags(msg.Tags); + if (msg.ReadOnly) { + tags += " *readonly"; + } + switch (msg.Privacy) { + case 2: + tags += " *public"; + break; + case -1: + tags += " *friends"; + break; + case -2: + tags += " *private"; + break; + } + + out.println(" <li id=\"msg-" + msg.MID + "\" class=\"msg\">"); + 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.User.UName + "/" + msg.MID + "\" title=\"" + msg.TimestampString + "\">" + formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a></div>"); + out.println(" <div class=\"msg-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:" + tags + "</div>"); + if (msg.Place != null) { + out.println(" <div class=\"msg-place\">" + msg.Place.name + "</div>"); + } + out.println(" <div class=\"msg-txt\">" + formatMessage(msg.Text) + "</div>"); - if (rs.getInt(10) > 0) { - String repliesby = rs.getString(11); - if (repliesby == null) { - repliesby = "..."; - } - out.println(" <div class=\"msg-comments\"><a href=\"/" + uname + "/" + mid + "\">" + formatReplies(rs.getInt(10), locale) + "</a> " + rb.getString("(replies) by") + " " + repliesby + "</div>"); - } else 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.AttachmentType != null) { + if (msg.AttachmentType.equals("jpg")) { + out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/photos-1024/" + msg.MID + ".jpg\"><img src=\"//i.juick.com/photos-512/" + msg.MID + ".jpg\" alt=\"\"/></a></div>"); + } else { + out.println(" <div class=\"msg-media\"><div id=\"video-" + msg.MID + "\"><a href=\"//i.juick.com/video/" + msg.MID + ".mp4\" onclick=\"inlinevideo(" + msg.MID + "); return false\"><img src=\"//i.juick.com/thumbs/" + msg.MID + ".jpg\" alt=\"\"/></a></div></div>"); } - out.println(" </li>"); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + + msg.ReadOnly |= blUIDs.contains(msg.User.UID); + + if (msg.Replies > 0) { + String repliesby = (msg.RepliesBy != null) ? msg.RepliesBy : "..."; + out.println(" <div class=\"msg-comments\"><a href=\"/" + msg.User.UName + "/" + msg.MID + "\">" + formatReplies(msg.Replies, locale) + "</a> " + rb.getString("(replies) by") + " " + repliesby + "</div>"); + } else if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) { + out.println(" <form action=\"/post\" method=\"POST\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"mid\" value=\"" + msg.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>"); + } + out.println(" </li>"); } } } diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index d90d7e94..ec3412f3 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -26,6 +26,7 @@ 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; @@ -76,7 +77,6 @@ public class UserThread { out.println("<div id=\"content\" style=\"margin-left: 0; width: 100%\">"); printMessage(out, sql, MID, visitor, locale); - printReplies(out, sql, MID, visitor, locale, listview); out.println("</div>"); @@ -115,7 +115,7 @@ public class UserThread { boolean cancomment = visitor != null; - tags = (tags != null) ? PageTemplates.formatTags(tags) : ""; + tags = (tags != null) ? PageTemplates.formatTags(new ArrayList<String>(Arrays.asList(tags.split(" ")))) : ""; if (rs.getInt(5) == 1) { tags += " *readonly"; cancomment = false; @@ -153,6 +153,15 @@ public class UserThread { } } + boolean visitorInBL = UserQueries.isInBL(sql, uid, visitor.UID); + + if (visitorInBL) { + cancomment = false; + } + if (visitor != null && visitor.UID == uid) { + cancomment = true; + } + 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>"); @@ -180,7 +189,9 @@ public class UserThread { } else { out.println(" <li><a href=\"/post?body=S+%23" + mid + "\"><div style=\"background-position: -16px 0\"></div>" + rbthread.getString("(MToolbar) Subcribe") + "</a></li>"); } - out.println(" <li><a href=\"/post?body=%21+%23" + mid + "\"><div style=\"background-position: -32px 0\"></div>" + rbthread.getString("(MToolbar) Recommend") + "</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>"); } @@ -198,48 +209,24 @@ public class UserThread { public static void printReplies(PrintWriter out, Connection sql, int mid, com.juick.User visitor, Locale locale, boolean listview) { ResourceBundle rbuser = ResourceBundle.getBundle("User", locale); - ArrayList<com.juick.Message> replies = new ArrayList<com.juick.Message>(); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts,replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - msg.MID = mid; - msg.RID = rs.getInt(1); - msg.ReplyTo = rs.getInt(2); - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(3); - msg.User.UName = rs.getString(4); - msg.Text = PageTemplates.formatMessage(rs.getString(5)); - msg.MinutesAgo = rs.getInt(6); - msg.TimestampString = rs.getString(7); - msg.AttachmentType = rs.getString(8); - - replies.add(msg); - - if (msg.ReplyTo > 0) { - boolean added = false; - for (int i = 0; i < replies.size(); i++) { - if (replies.get(i).RID == msg.ReplyTo) { - replies.get(i).childs.add(msg); - added = true; - break; - } - } - if (!added) { - msg.ReplyTo = 0; + ArrayList<com.juick.Message> replies = MessagesQueries.getReplies(sql, mid); + + for (int i = 0; i < replies.size(); i++) { + com.juick.Message msg = replies.get(i); + if (msg.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); + added = true; + break; } } + if (!added) { + msg.ReplyTo = 0; + } } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); } if (!replies.isEmpty()) { @@ -297,9 +284,9 @@ 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.MinutesAgo, msg.TimestampString, locale) + "</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-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:</div>"); - out.println(" <div class=\"msg-txt\">" + msg.Text + "</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) { @@ -330,9 +317,9 @@ 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.MinutesAgo, msg.TimestampString, locale) + "</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-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:</div>"); - out.println(" <div class=\"msg-txt\">" + msg.Text + "</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>"); |