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
351
|
/*
* 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.www;
import com.juick.server.MessagesQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import com.juick.server.helpers.TagStats;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* @author Ugnich Anton
*/
public class User {
protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
List<Integer> mids;
String paramShow = request.getParameter("show");
com.juick.Tag paramTag = null;
String paramTagStr = request.getParameter("tag");
if (paramTagStr != null) {
if (paramTagStr.length() < 64) {
paramTag = TagQueries.getTag(sql, paramTagStr, false);
}
if (paramTag == null) {
Errors.doGet404(sql, request, response);
return;
} else if (!paramTag.getName().equals(paramTagStr)) {
String url = "/" + user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8);
Utils.sendPermanentRedirect(response, url);
return;
}
}
int paramBefore = 0;
String paramBeforeStr = request.getParameter("before");
if (paramBeforeStr != null) {
try {
paramBefore = Integer.parseInt(paramBeforeStr);
} catch (NumberFormatException e) {
}
}
String paramSearch = request.getParameter("search");
if (paramSearch != null && paramSearch.length() > 64) {
paramSearch = null;
}
int privacy = 0;
if (visitor.getUid() > 0) {
if (user.getUid() == visitor.getUid() || visitor.getUid() == 1) {
privacy = -3;
} else if (UserQueries.isInWL(sql, user.getUid(), visitor.getUid())) {
privacy = -2;
}
}
String title;
if (paramShow == null) {
if (paramTag != null) {
title = "Блог " + user.getName() + ": *" + StringEscapeUtils.escapeHtml4(paramTag.getName());
mids = MessagesQueries.getUserTag(sql, user.getUid(), paramTag.TID, privacy, paramBefore);
} else if (paramSearch != null) {
title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch);
mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.getUid(), Utils.encodeSphinx(paramSearch), privacy, paramBefore);
} else {
title = "Блог " + user.getName();
mids = MessagesQueries.getUserBlog(sql, user.getUid(), privacy, paramBefore);
}
} else if (paramShow.equals("recomm")) {
title = "Рекомендации " + user.getName();
mids = MessagesQueries.getUserRecommendations(sql, user.getUid(), paramBefore);
} else if (paramShow.equals("photos")) {
title = "Фотографии " + user.getName();
mids = MessagesQueries.getUserPhotos(sql, user.getUid(), privacy, paramBefore);
} else {
Errors.doGet404(sql, request, response);
return;
}
if (visitor.getUid() == 0) {
pageUserRefCookie(request, response, user.getUid());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" +
user.getName() + "\" href=\"//rss.juick.com/" + user.getName() + "/blog\"/>";
if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) {
head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
} else if (paramBefore > 0 || paramShow != null) {
head += "<meta name=\"robots\" content=\"noindex\"/>";
}
PageTemplates.pageHead(out, visitor, title, head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
if (mids.size() > 0) {
out.println("<section id=\"content\">");
if (paramTag != null) {
out.println("<p class=\"page\"><a href=\"/tag/" +
URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "\">← Все записи с тегом <b>" +
StringEscapeUtils.escapeHtml4(paramTag.getName()) + "</b></a></p>");
}
PageTemplates.printMessages(out, sql, user, mids, visitor, visitor.getUid() == 0 ? 4 : 5, 0);
if (mids.size() >= 20) {
String nextpage = "?before=" + mids.get(mids.size() - 1);
if (paramShow != null) {
nextpage += "&show=" + paramShow;
}
if (paramTag != null) {
nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8);
}
if (paramSearch != null) {
nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8);
}
out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>");
}
out.println("</section>");
}
PageTemplates.pageFooter(request, out, visitor, true);
PageTemplates.pageEnd(out);
}
}
protected void doGetTags(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (visitor.getUid() == 0) {
pageUserRefCookie(request, response, user.getUid());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
PageTemplates.pageHead(out, visitor, "Теги " + user.getName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<p>" + pageUserTags(sql, user, visitor, 0) + "</p>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
protected void doGetFriends(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (visitor.getUid() == 0) {
pageUserRefCookie(request, response, user.getUid());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex\"/>";
PageTemplates.pageHead(out, visitor, "Подписки " + user.getName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<table class=\"users\"><tr>");
List<com.juick.User> friends = UserQueries.getUserFriends(sql, user.getUid());
for (int i = 0; i < friends.size(); i++) {
if (i % 3 == 0 && i > 0) {
out.print("</tr><tr>");
}
out.print("<td><a href=\"/" + friends.get(i).getName()
+ "/\"><img src=\"//i.juick.com/as/" + friends.get(i).getUid() + ".png\"/>"
+ friends.get(i).getName() + "</a></td>");
}
out.println("</tr></table>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
protected void doGetReaders(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (visitor.getUid() == 0) {
pageUserRefCookie(request, response, user.getUid());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex\"/>";
PageTemplates.pageHead(out, visitor, "Читатели " + user.getName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<table class=\"users\"><tr>");
List<com.juick.User> readers = UserQueries.getUserReaders(sql, user.getUid());
for (int i = 0; i < readers.size(); i++) {
if (i % 3 == 0 && i > 0) {
out.print("</tr><tr>");
}
out.print("<td><a href=\"/" + readers.get(i).getName()
+ "/\"><img src=\"//i.juick.com/as/" + readers.get(i).getUid() + ".png\"/>"
+ readers.get(i).getName() + "</a></td>");
}
out.println("</tr></table>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
public static void pageUserRefCookie(HttpServletRequest request, HttpServletResponse response, int uid) {
String hReferer = request.getHeader("Referer");
String ref = Utils.getCookie(request, "ref");
if (ref == null && (hReferer == null || !(hReferer.startsWith("http://juick.com/") || hReferer.startsWith("https://juick.com/")))) {
Cookie c = new Cookie("ref", Integer.toString(uid));
c.setMaxAge(7 * 24 * 60 * 60);
c.setPath("/");
response.addCookie(c);
}
}
public static void pageUserColumn(PrintWriter out, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) {
out.println("<aside id=\"column\">");
out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUid() + ".png\" alt=\"\"/>" + user.getName() + "</a></div>");
if (visitor.getUid() > 0 && visitor.getUid() != user.getUid()) {
out.println(" <ul id=\"ctoolbar\">");
if (UserQueries.isSubscribed(sql, visitor.getUid(), user.getUid())) {
out.println(" <li><a href=\"/post?body=U+%40" + user.getName() + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>");
} else {
out.println(" <li><a href=\"/post?body=S+%40" + user.getName() + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>");
}
if (UserQueries.isInBL(sql, visitor.getUid(), user.getUid())) {
out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>");
} else {
out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>");
}
if (!UserQueries.isInBLAny(sql, user.getUid(), visitor.getUid())) {
out.println(" <li><a href=\"/pm/sent?uname=" + user.getName() + "\" title=\"Написать приватное сообщение\"><div style=\"background-position: -112px 0\"></div></a></li>");
}
out.println(" </ul>");
} else {
out.println(" <hr/>");
}
out.println(" <ul>");
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>");
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(" <hr/>");
out.println(" <div id=\"ustats\"><ul>");
out.println(" <li><a href=\"./friends\">Я читаю: " + UserQueries.getStatsIRead(sql, user.getUid()) + "</a></li>");
out.println(" <li><a href=\"./readers\">Мои подписчики: " + UserQueries.getStatsMyReaders(sql, user.getUid()) + "</a></li>");
out.println(" <li>Сообщений: " + UserQueries.getStatsMessages(sql, user.getUid()) + "</li>");
out.println(" <li>Комментариев: " + UserQueries.getStatsReplies(sql, user.getUid()) + "</li>");
out.println(" </ul>");
List<com.juick.User> iread = UserQueries.getUserReadLeastPopular(sql, user.getUid(), 8);
if (!iread.isEmpty()) {
out.println("<table class=\"iread\"><tr>");
for (int i = 0; i < iread.size(); i++) {
if (i == 4) {
out.println("</tr><tr>");
}
com.juick.User u = iread.get(i);
out.println("<td><a href=\"/" + u.getName() + "/\"><img src=\"//i.juick.com/a/" + u.getUid() + ".png\" alt=\"" + u.getName() + "\"/></a></td>");
}
out.println("</tr></table>");
}
out.println(" </div>");
out.println("</aside>");
}
public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) {
List<TagStats> tags = TagQueries.getTagsStats(sql, user.getUid()).stream()
.sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).collect(Collectors.toList());
int maxUsageCnt = tags.stream().map(TagStats::getUsageCount).max(Comparator.naturalOrder()).orElse(0);
String ret = "";
int count = cnt > 0 ? Math.min(tags.size(), cnt) : tags.size();
for (int i = 0; i < count; i++) {
String tag = StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName());
try {
tag = "<a href=\"./?tag=" + URLEncoder.encode(tags.get(i).getTag().getName(), CharEncoding.UTF_8) + "\" title=\""
+ tags.get(i).getUsageCount() + "\" rel=\"nofollow\">" + tag + "</a>";
} catch (UnsupportedEncodingException e) {
}
if (tags.get(i).getUsageCount() > maxUsageCnt / 3 * 2) {
ret += "<big>" + tag + "</big> ";
} else if (tags.get(i).getUsageCount() > maxUsageCnt / 3) {
ret += "<small>" + tag + "</small> ";
} else {
ret += tag + " ";
}
}
return ret;
}
}
|