From 3b92419424bf770c6199b821ba0b7e98c7b1bd99 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 2 Nov 2012 18:33:10 +0700 Subject: BL-sensible comment form --- src/java/com/juick/http/www/PageTemplates.java | 134 ++++++++++--------------- src/java/com/juick/http/www/UserThread.java | 77 ++++++-------- 2 files changed, 87 insertions(+), 124 deletions(-) (limited to 'src/java/com/juick/http/www') 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(""); } - public static String formatTags(String tags) { + public static String formatTags(ArrayList 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 += " *" + tag + ""; + ret += " *" + tag + ""; } catch (UnsupportedEncodingException e) { } } @@ -277,85 +277,61 @@ public class PageTemplates { public static void printMessages(PrintWriter out, Connection sql, ArrayList 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 msgs = MessagesQueries.getMessages(sql, mids); - txt = formatMessage(txt); + ArrayList blUIDs = new ArrayList(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("
  • "); - out.println("
    \""
    "); - out.println(" "); - out.println("
    @" + uname + ":" + tags + "
    "); - if (rs.getString(14) != null) { - out.println("
    " + rs.getString(14) + "
    "); - } - out.println("
    " + txt + "
    "); - - if (rs.getString(12) != null) { - if (rs.getString(12).equals("jpg")) { - out.println("
    \"\"/
    "); - } else { - out.println("
    \"\"/
    "); - } - } + 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("
  • "); + out.println("
    \""
    "); + out.println(" "); + out.println(" "); + if (msg.Place != null) { + out.println("
    " + msg.Place.name + "
    "); + } + out.println("
    " + formatMessage(msg.Text) + "
    "); - if (rs.getInt(10) > 0) { - String repliesby = rs.getString(11); - if (repliesby == null) { - repliesby = "..."; - } - out.println("
    " + formatReplies(rs.getInt(10), locale) + " " + rb.getString("(replies) by") + " " + repliesby + "
    "); - } else if (cancomment) { - out.println("
    "); - out.println("
    "); - out.println("
    "); + if (msg.AttachmentType != null) { + if (msg.AttachmentType.equals("jpg")) { + out.println("
    \"\"/
    "); + } else { + out.println("
    \"\"/
    "); } - out.println("
  • "); } - } 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("
    " + formatReplies(msg.Replies, locale) + " " + rb.getString("(replies) by") + " " + repliesby + "
    "); + } else if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) { + out.println("
    "); + out.println("
    "); + out.println("
    "); + } + out.println(" "); } } } 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("
    "); printMessage(out, sql, MID, visitor, locale); - printReplies(out, sql, MID, visitor, locale, listview); out.println("
    "); @@ -115,7 +115,7 @@ public class UserThread { boolean cancomment = visitor != null; - tags = (tags != null) ? PageTemplates.formatTags(tags) : ""; + tags = (tags != null) ? PageTemplates.formatTags(new ArrayList(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("
    "); out.println("
    "); @@ -180,7 +189,9 @@ public class UserThread { } else { out.println("
  • " + rbthread.getString("(MToolbar) Subcribe") + "
  • "); } - out.println("
  • " + rbthread.getString("(MToolbar) Recommend") + "
  • "); + if (!visitorInBL) { + out.println("
  • " + rbthread.getString("(MToolbar) Recommend") + "
  • "); + } } else { out.println("
  • " + rbthread.getString("(MToolbar) Delete") + "
  • "); } @@ -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 replies = new ArrayList(); - 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 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("
    \""
    "); - out.println(" "); + out.println(" "); out.println(" "); - out.println("
    " + msg.Text + "
    "); + out.println("
    " + PageTemplates.formatMessage(msg.Text) + "
    "); out.println(" "); out.println("
    "); if (ReplyTo == 0) { @@ -330,9 +317,9 @@ public class UserThread { } } out.println("
    "); - out.println(" "); + out.println(" "); out.println(" "); - out.println("
    " + msg.Text + "
    "); + out.println("
    " + PageTemplates.formatMessage(msg.Text) + "
    "); out.print("
    /" + msg.RID); if (msg.ReplyTo > 0) { out.print(" " + rb.getString("in reply to") + " /" + msg.ReplyTo + ""); -- cgit v1.2.3