1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.juick.http.www;
import com.juick.server.MessagesQueries;
import com.juick.server.UserQueries;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Ugnich Anton
*/
public class UserThread {
protected void doGetThread(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user, int MID) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request);
Locale locale = request.getLocale();
if (!MessagesQueries.canViewThread(sql, MID, visitor != null ? visitor.UID : 0)) {
response.sendError(403);
return;
}
boolean listview = false;
String paramView = request.getParameter("view");
if (paramView != null) {
if (paramView.equals("list")) {
listview = true;
if (visitor != null) {
UserQueries.setUserOptionInt(sql, visitor.UID, "repliesview", 1);
}
} else if (paramView.equals("tree") && visitor != null) {
UserQueries.setUserOptionInt(sql, visitor.UID, "repliesview", 0);
}
} else if (visitor != null && UserQueries.getUserOptionInt(sql, visitor.UID, "repliesview", 0) == 1) {
listview = true;
}
String title = "@" + user.UName + " - #" + MID;
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
PageTemplates.pageHead(out, title, "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.UName + "\" href=\"//rss.juick.com/" + user.UName + "/blog\"/>");
PageTemplates.pageNavigation(out, locale, visitor, null);
//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%\">");
com.juick.Message msg = printMessage(out, sql, user, MID, visitor, locale);
printReplies(out, sql, msg, visitor, locale, listview);
out.println("</div>");
out.println("</div>");
out.println("</div>"); // topwrapper
PageTemplates.pageDialogTemplate(out, locale);
PageTemplates.pageFooter(request, out, locale, visitor, false);
} finally {
out.close();
}
}
public static com.juick.Message printMessage(PrintWriter out, Connection sql, com.juick.User user, int mid, com.juick.User visitor, Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
ResourceBundle rbthread = ResourceBundle.getBundle("UserThread", locale);
com.juick.Message msg = MessagesQueries.getMessage(sql, mid);
msg.VisitorCanComment = visitor != null;
String tags = (msg.Tags.isEmpty()) ? "" : PageTemplates.formatTags(msg.Tags, user);
if (msg.ReadOnly) {
tags += " *readonly";
msg.VisitorCanComment = false;
}
switch (msg.Privacy) {
case 2:
tags += " *public";
break;
case -1:
tags += " *friends";
break;
case -2:
tags += " *friends";
break;
case -3:
tags += " *private";
break;
}
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-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:" + tags + "</div>");
out.println(" <div class=\"msg-ts\">" + PageTemplates.formatJSLocalTime(msg.TimestampString) + "<a href=\"#\" onclick=\"showMessageLinksDialog(" + msg.MID + "); return false\" class=\"msg-menu\"><img src=\"http://static.juick.com/message-menu-icon.png\" alt=\"\"/></a></div>");
if (msg.Place != null) {
out.println(" <div class=\"msg-place\"><a href=\"/places/" + msg.Place.pid + "\">" + msg.Place.name + "</a></div>");
}
out.println(" <div class=\"msg-txt\">" + PageTemplates.formatMessage(msg.Text) + "</div>");
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) {
msg.VisitorCanComment = false;
}
}
}
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=\"" + (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("</div>");
}
out.println(" </li>");
out.println(" <li id=\"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>");
}
} 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(" </ul></li>");
out.println("</ul>");
return msg;
}
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, msg.MID);
ArrayList<Integer> blUIDs = new ArrayList<Integer>();
for (int i = 0; i < replies.size(); i++) {
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 == reply.ReplyTo) {
replies.get(n).childs.add(reply);
added = true;
break;
}
}
if (!added) {
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 {
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>");
out.println("</div>");
out.println("<ul id=\"replies\">");
if (listview) {
printList(out, replies, locale);
} else {
printTree(out, replies, 0, 0, false, locale);
}
out.println("</ul>");
for (int i = 0; i < replies.size(); i++) {
replies.get(i).cleanupChilds();
}
replies.clear();
}
}
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) {
margin = 420;
}
for (int i = 0; i < replies.size(); i++) {
com.juick.Message msg = replies.get(i);
if (msg.ReplyTo == ReplyTo) {
out.print(" <li id=\"" + msg.RID + "\" class=\"msg\" style=\"");
if (margin > 0) {
out.print("margin-left: " + margin + "px;");
}
if (hidden) {
out.print("display:none;");
}
out.println("\">");
if (msg.AttachmentType != null) {
if (msg.AttachmentType.equals("jpg")) {
out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.MID + "-" + msg.RID + ".jpg\"><img src=\"//i.juick.com/photos-512/" + msg.MID + "-" + msg.RID + ".jpg\" alt=\"\"/></a></div>");
} else {
out.println(" <div class=\"msg-media\"><div id=\"video-" + msg.MID + "-" + msg.RID + "\"><a href=\"//i.juick.com/video/" + msg.MID + "-" + msg.RID + ".mp4\" onclick=\"inlinevideo('" + msg.MID + "-" + msg.RID + "'); return false\"><img src=\"//i.juick.com/thumbs/" + msg.MID + "-" + msg.RID + ".jpg\" alt=\"\"/></a></div></div>");
}
}
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-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</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><a href=\"#\" onclick=\"showMessageLinksDialog(" + msg.MID + "," + msg.RID + "); return false\" class=\"msg-menu\"><img src=\"http://static.juick.com/message-menu-icon.png\" alt=\"\"/></a></div>");
out.println(" <div class=\"msg-txt\">" + PageTemplates.formatMessage(msg.Text) + "</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>");
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);
}
}
}
}
public static void printList(PrintWriter out, ArrayList<com.juick.Message> replies, Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
for (int i = 0; i < replies.size(); i++) {
com.juick.Message msg = replies.get(i);
out.print(" <li id=\"" + msg.RID + "\" class=\"msg\">");
if (msg.AttachmentType != null) {
if (msg.AttachmentType.equals("jpg")) {
out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.MID + "-" + msg.RID + ".jpg\"><img src=\"//i.juick.com/photos-512/" + msg.MID + "-" + msg.RID + ".jpg\" alt=\"\"/></a></div>");
} else {
out.println(" <div class=\"msg-media\"><div id=\"video-" + msg.MID + "-" + msg.RID + "\"><a href=\"//i.juick.com/video/" + msg.MID + "-" + msg.RID + ".mp4\" onclick=\"inlinevideo('" + msg.MID + "-" + msg.RID + "'); return false\"><img src=\"//i.juick.com/thumbs/" + msg.MID + "-" + msg.RID + ".jpg\" alt=\"\"/></a></div></div>");
}
}
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-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</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><a href=\"#\" onclick=\"showMessageLinksDialog(" + msg.MID + "," + msg.RID + "); return false\" class=\"msg-menu\"><img src=\"http://static.juick.com/message-menu-icon.png\" alt=\"\"/></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>");
}
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>");
}
}
}
|