aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/User.java
blob: 3e566df97836c3f8c65b8aca0c5dd5d816e7f79d (plain) (blame)
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
352
353
354
355
356
357
358
359
360
/*
 * 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.controllers;

import com.juick.server.helpers.TagStats;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.UserUtils;
import com.juick.www.Utils;
import com.juick.www.WebApp;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.ServletException;
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
 */
@Controller
public class User {
    @Inject
    WebApp webApp;
    @Inject
    UserService userService;
    @Inject
    TagService tagService;
    @Inject
    MessagesService messagesService;
    @Inject
    PageTemplates templates;

    @GetMapping("/{uname}/")
    protected void doGetBlog(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable String uname,
                             @RequestParam(required = false, defaultValue = "0") Integer before) throws IOException {
        com.juick.User user = userService.getUserByName(uname);
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (user.isBanned()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        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 = tagService.getTag(paramTagStr, false);
            }
            if (paramTag == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            } else if (!paramTag.getName().equals(paramTagStr)) {
                String url = "/" + user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8);
                Utils.sendPermanentRedirect(response, url);
                return;
            }
        }


        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 (userService.isInWL(user.getUid(), visitor.getUid())) {
                privacy = -2;
            }
        }

        String title;
        if (paramShow == null) {
            if (paramTag != null) {
                title = "Блог " + user.getName() + ": *" + StringEscapeUtils.escapeHtml4(paramTag.getName());
                mids = messagesService.getUserTag(user.getUid(), paramTag.TID, privacy, before);
            } else if (paramSearch != null) {
                title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch);
                mids = messagesService.getUserSearch(user.getUid(), Utils.encodeSphinx(paramSearch), privacy, before);
            } else {
                title = "Блог " + user.getName();
                mids = messagesService.getUserBlog(user.getUid(), privacy, before);
            }
        } else if (paramShow.equals("recomm")) {
            title = "Рекомендации " + user.getName();
            mids = messagesService.getUserRecommendations(user.getUid(), before);
        } else if (paramShow.equals("photos")) {
            title = "Фотографии " + user.getName();
            mids = messagesService.getUserPhotos(user.getUid(), privacy, before);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        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 && tagService.getTagNoIndex(paramTag.TID)) {
                head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
            } else if (before > 0 || paramShow != null) {
                head += "<meta name=\"robots\" content=\"noindex\"/>";
            }
            templates.pageHead(out, visitor, title, head);
            templates.pageNavigation(out, visitor, null);
            pageUserColumn(out, 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>");
                }

                templates.printMessages(out, 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 += "&amp;show=" + paramShow;
                    }
                    if (paramTag != null) {
                        nextpage += "&amp;tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8);
                    }
                    if (paramSearch != null) {
                        nextpage += "&amp;search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8);
                    }
                    out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>");
                }

                out.println("</section>");
            }

            templates.pageFooter(request, out, visitor, true);
            templates.pageEnd(out);
        }
    }

    @GetMapping("/{uname}/tags")
    protected void doGetTags(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable String uname) throws IOException {
        com.juick.User user = userService.getUserByName(uname);
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.isBanned()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
            templates.pageHead(out, visitor, "Теги " + user.getName(), head);
            templates.pageNavigation(out, visitor, null);
            pageUserColumn(out, user, visitor);

            out.println("<section id=\"content\">");
            out.println("<p>" + pageUserTags(user, visitor, 0) + "</p>");
            out.println("</section>");

            templates.pageFooter(request, out, visitor, false);
            templates.pageEnd(out);
        }
    }

    @GetMapping("/{uname}/friends")
    protected void doGetFriends(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable String uname) throws ServletException, IOException {
        com.juick.User user = userService.getUserByName(uname);
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.isBanned()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String head = "<meta name=\"robots\" content=\"noindex\"/>";
            templates.pageHead(out, visitor, "Подписки " + user.getName(), head);
            templates.pageNavigation(out, visitor, null);
            pageUserColumn(out, user, visitor);

            out.println("<section id=\"content\">");
            out.println("<table class=\"users\"><tr>");

            List<com.juick.User> friends = userService.getUserFriends(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>");

            templates.pageFooter(request, out, visitor, false);
            templates.pageEnd(out);
        }
    }

    @GetMapping("/{uname}/readers")
    protected void doGetReaders(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable String uname) throws ServletException, IOException {
        com.juick.User user = userService.getUserByName(uname);
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.isBanned()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String head = "<meta name=\"robots\" content=\"noindex\"/>";
            templates.pageHead(out, visitor, "Читатели " + user.getName(), head);
            templates.pageNavigation(out, visitor, null);
            pageUserColumn(out, user, visitor);

            out.println("<section id=\"content\">");
            out.println("<table class=\"users\"><tr>");

            List<com.juick.User> readers = userService.getUserReaders(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>");

            templates.pageFooter(request, out, visitor, false);
            templates.pageEnd(out);
        }
    }

    public void pageUserColumn(PrintWriter out, 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 (userService.isSubscribed(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 (userService.isInBL(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 (!userService.isInBLAny(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(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\">Я читаю: " + userService.getStatsIRead(user.getUid()) + "</a></li>");
        out.println("    <li><a href=\"./readers\">Мои подписчики: " + userService.getStatsMyReaders(user.getUid()) + "</a></li>");
        out.println("    <li>Сообщений: " + userService.getStatsMessages(user.getUid()) + "</li>");
        out.println("    <li>Комментариев: " + userService.getStatsReplies(user.getUid()) + "</li>");
        out.println("  </ul>");

        List<com.juick.User> iread = userService.getUserReadLeastPopular(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 String pageUserTags(com.juick.User user, com.juick.User visitor, int cnt) {
        List<TagStats> tags = tagService.getUserTagStats(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 = StringUtils.EMPTY;
        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;
    }
}