aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
blob: 6c2e0031f4c1a13c5d508692544eeb42cd39e693 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * 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.Message;
import com.juick.server.helpers.TagStats;
import com.juick.server.util.HttpNotFoundException;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.MessageUtils;
import com.juick.www.WebApp;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ugnich Anton
 */
@Controller
public class UserThread {

    @Inject
    WebApp webApp;
    @Inject
    MessagesService messagesService;
    @Inject
    UserService userService;
    @Inject
    TagService tagService;
    @Inject
    PageTemplates templates;

    @RequestMapping(value = "/{uname}/{mid}", method = RequestMethod.GET)
    protected void doGetThread(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable String uname,
                               @PathVariable int mid) throws ServletException, IOException {
        com.juick.User visitor = webApp.getVisitorUser(request, response);

        if (!messagesService.canViewThread(mid, visitor.getUid())) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        com.juick.Message msg = messagesService.getMessage(mid);

        if (msg == null) {
            throw new HttpNotFoundException();
        }

        com.juick.User user = userService.getUserByName(uname);
        if (user.getUid() == 0 || !msg.getUser().equals(user)) {
            response.sendRedirect(String.format("/%s/%d", msg.getUser().getName(), mid));
            return;
        }

        boolean listview = false;
        String paramView = request.getParameter("view");
        if (paramView != null) {
            if (paramView.equals("list")) {
                listview = true;
                if (visitor.getUid() > 0) {
                    userService.setUserOptionInt(visitor.getUid(), "repliesview", 1);
                }
            } else if (paramView.equals("tree") && visitor.getUid() > 0) {
                userService.setUserOptionInt(visitor.getUid(), "repliesview", 0);
            }
        } else if (visitor.getUid() > 0 && userService.getUserOptionInt(visitor.getUid(), "repliesview", 0) == 1) {
            listview = true;
        }

        String title = msg.getUser().getName() + ": " + msg.getTagsString();

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String headers = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + msg.getUser().getName() + "\" href=\"//rss.juick.com/" + msg.getUser().getName() + "/blog\"/>";
            if (paramView != null) {
                headers += "<link rel=\"canonical\" href=\"http://juick.com/" + msg.getUser().getName() + "/" + msg.getMid() + "\"/>";
            }
            if (msg.Hidden) {
                headers += "<meta name=\"robots\" content=\"noindex\"/>";
            }
            templates.pageHead(out, visitor, title, headers);
            templates.pageNavigation(out, visitor, null);

            out.println("<section id=\"content\" data-mid=\"" + msg.getMid() + "\" style=\"margin-left: 0; width: 100%\">");
            printMessage(out, msg, visitor);
            printReplies(out, msg, visitor, listview);
            out.println("</section>");

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

            templates.pageEnd(out);
        }
    }

    // when message id is not fit to int
    @ExceptionHandler(NumberFormatException.class)
    public ResponseEntity<String> notfound() {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    public com.juick.Message printMessage(PrintWriter out, com.juick.Message msg, com.juick.User visitor) {
        msg.VisitorCanComment = visitor.getUid() > 0;

        List<TagStats> tags = tagService.getMessageTags(msg.getMid());
        String tagsStr = templates.formatTags(tags);
        if (msg.ReadOnly) {
            tagsStr += "<a>readonly</a>";
            msg.VisitorCanComment = false;
        }
        if (msg.getPrivacy() < 0) {
            tagsStr += "<a>friends</a>";
        }

        String txt;
        if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) {
            txt = MessageUtils.formatMessageCode(msg.getText());
        } else {
            txt = MessageUtils.formatMessage(msg.getText());
        }

        if (!tags.isEmpty()) {
            tagsStr = "<div class=\"msg-tags\">" + tagsStr + "</div>";
        }

        out.println("<ul>");
        out.println("  <li id=\"msg-" + msg.getMid() + "\" data-mid=\"" + msg.getMid() + "\" class=\"msg msgthread\">");
        out.println("    <div class=\"msg-cont\">");
        out.println("      <div class=\"msg-menu\"><a href=\"#\"></a></div>");
        out.println("      <div class=\"msg-ts\">" + templates.formatJSLocalTime(msg.getDate()) + "</div>");
        out.println("      <div class=\"msg-avatar\"><a href=\"/" + msg.getUser().getName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUid() + ".png\" alt=\"" + msg.getUser().getName() + "\"/></a></div>");
        out.println("      <div class=\"msg-header\">@<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:" + tagsStr + "</div>");
        out.println("      <div class=\"msg-txt\">" + txt + "</div>");

        if (msg.getAttachmentType() != null) {
            out.println("    <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
        }

        boolean visitorInBL = false;
        if (visitor.getUid() > 0) {
            if (visitor.getUid() == msg.getUser().getUid()) {
                msg.VisitorCanComment = true;
            } else {
                visitorInBL = userService.isInBL(msg.getUser().getUid(), visitor.getUid());
                if (visitorInBL) {
                    msg.VisitorCanComment = false;
                }
            }
        }

        if (msg.VisitorCanComment) {
            out.println("    <form action=\"/comment\" method=\"POST\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"mid\" value=\"" + msg.getMid() + "\"/>");
            out.println("      <div class=\"msg-comment\"><div class=\"ta-wrapper\"><textarea name=\"body\" rows=\"1\" class=\"reply\" placeholder=\"Написать комментарий\"></textarea></div></div>");
            out.println("    </form>");
        }

        List<String> recomm = messagesService.getMessageRecommendations(msg.getMid());
        if (!recomm.isEmpty()) {
            out.print("    <div class=\"msg-recomms\">Рекомендовали (" + 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("    </div>");
        out.println("  </li>");

        out.println("  <li id=\"mtoolbar\"><ul>");
        out.println("    <li><a href=\"/" + msg.getMid() + "\"><div style=\"background-position: -64px 0\"></div>" + msg.getMid() + "</a></li>");
        if (visitor.getUid() > 0) {
            if (visitor.getUid() != msg.getUser().getUid()) {
                if (messagesService.isSubscribed(visitor.getUid(), msg.getMid())) {
                    out.println("    <li><a href=\"/post?body=U+%23" + msg.getMid() + "\"><div style=\"background-position: -48px 0\"></div>Подписан</a></li>");
                } else {
                    out.println("    <li><a href=\"/post?body=S+%23" + msg.getMid() + "\"><div style=\"background-position: -16px 0\"></div>Подписаться</a></li>");
                }
                if (!visitorInBL) {
                    out.println("    <li><a href=\"/post?body=%21+%23" + msg.getMid() + "\"><div style=\"background-position: -32px 0\"></div>Рекомендовать</a></li>");
                }
            } else {
                out.println("    <li><a href=\"/post?body=D+%23" + msg.getMid() + "\"><div style=\"background-position: 0\"></div>Удалить</a></li>");
            }
        }
        out.println("  </ul></li>");
        out.println("</ul>");

        return msg;
    }

    public void printReplies(PrintWriter out, com.juick.Message msg, com.juick.User visitor, boolean listview) {
        List<com.juick.Message> replies = messagesService.getReplies(msg.getMid());

        List<Integer> blUIDs = new ArrayList<Integer>();
        for (int i = 0; i < replies.size(); i++) {
            com.juick.Message reply = replies.get(i);
            if (reply.getUser().getUid() != msg.getUser().getUid() && !blUIDs.contains(reply.getUser().getUid())) {
                blUIDs.add(reply.getUser().getUid());
            }
            if (reply.getReplyto() > 0) {
                boolean added = false;
                for (int n = 0; n < replies.size(); n++) {
                    if (replies.get(n).getRid() == reply.getReplyto()) {
                        replies.get(n).childs.add(reply);
                        added = true;
                        break;
                    }
                }
                if (!added) {
                    reply.setReplyto(0);
                }
            }
        }

        if (!replies.isEmpty()) {
            if (visitor.getUid() > 0 && msg.getUser().getUid() == visitor.getUid()) {
                for (Message reply : replies) {
                    reply.VisitorCanComment = true;
                }
            } else if (visitor.getUid() > 0 && msg.VisitorCanComment) {
                blUIDs = userService.checkBL(visitor.getUid(), blUIDs);
                for (Message reply : replies) {
                    reply.VisitorCanComment = reply.getUser().getUid() == visitor.getUid() || !blUIDs.contains(reply.getUser().getUid());
                }
            } else {
                for (Message reply : replies) {
                    reply.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\" rel=\"nofollow\">Показать деревом</a>");
            } else {
                if (foldable) {
                    out.print("<span id=\"unfoldall\"><a href=\"#\">Раскрыть все</a> &#183; </span>");
                }
                out.print("<a href=\"?view=list\" rel=\"nofollow\">Показать списком</a>");
            }
            out.print("</div>");
            out.println("  <h2>Ответы (" + replies.size() + ")</h2>");
            out.println("</div>");

            out.println("<ul id=\"replies\">");
            if (listview) {
                printList(out, replies, visitor);
            } else {
                printTree(out, replies, visitor, 0, 0, false);
            }
            out.println("</ul>");

            for (Message reply : replies) {
                reply.cleanupChilds();
            }
            replies.clear();
        }
    }

    public void printTree(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor, int ReplyTo, int margin, boolean hidden) {
        if (margin > 240) {
            margin = 240;
        }

        for (int i = 0; i < replies.size(); i++) {
            com.juick.Message msg = replies.get(i);
            if (msg.getReplyto() == ReplyTo) {

                out.print("  <li id=\"" + msg.getRid() + "\" class=\"msg\" style=\"");
                if (margin > 0) {
                    out.print("margin-left: " + margin + "px;");
                }
                if (hidden) {
                    out.print("display:none;");
                }
                out.println("\">");
                out.println("    <div class=\"msg-cont\">");
                out.println("      <div class=\"msg-header\">");
				if (!msg.getUser().isBanned()) {
					out.println("        @<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:");
					out.println("        <div class=\"msg-avatar\"><a href=\"/" + msg.getUser().getName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUid() + ".png\" alt=\"" + msg.getUser().getName() + "\"/></a></div>");
                } else {
					out.println("        [удалено]:");
					out.println("        <div class=\"msg-avatar\"><img src=\"//i.juick.com/av-96.png\"/></div>");
                }
                out.println("        <div class=\"msg-menu\"><a href=\"#\" class=\"a-thread-links\"></a></div>");
                out.println("        <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + templates.sdfSQL.format(msg.getDate()) + " GMT\">" + templates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>");
                out.println("      </div>");
                out.println("      <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>");
                if (msg.getAttachmentType() != null) {
                    out.println("    <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
                }
                out.print("    <div class=\"msg-links\">/" + msg.getRid());
                if (msg.getReplyto() > 0) {
                    out.print(" в ответ на <a href=\"#" + msg.getReplyto() + "\">/" + msg.getReplyto() + "</a>");
                }
                if (msg.VisitorCanComment) {
                    out.println(" &#183; <a href=\"/post?body=%23" + msg.getMid() + "/" + msg.getRid() + "%20\" class=\"a-thread-comment\">Ответить</a></div>");
                    out.println("    <div class=\"msg-comment\" style=\"display: none\"></div>");
                } else if (visitor == null) {
                    out.println(" &#183; <a href=\"#\" class=\"a-login\">Ответить</a></div>");
                }

                int childs = msg.getChildsCount();
                if (ReplyTo == 0 && childs > 1 && replies.size() > 10) {
                    out.println("    <div class=\"msg-comments\"><a href=\"#\">" + templates.formatReplies(childs) + "</a></div>");
                }
                out.println("    </div>");
                out.println("  </li>");

                if (ReplyTo == 0 && childs > 1 && replies.size() > 10) {
                    printTree(out, msg.childs, visitor, msg.getRid(), margin + 20, true);
                } else if (childs > 0) {
                    printTree(out, msg.childs, visitor, msg.getRid(), margin + 20, hidden);
                }
            }
        }
    }

    public void printList(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor) {
        for (Message msg : replies) {
            out.print("  <li id=\"" + msg.getRid() + "\" class=\"msg\">");
            out.println("    <div class=\"msg-cont\">");
            out.println("      <div class=\"msg-header\">");
            if (!msg.getUser().isBanned()) {
            	out.println("      @<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:");
            	out.println("      <div class=\"msg-avatar\"><a href=\"/" + msg.getUser().getName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUid() + ".png\" alt=\"" + msg.getUser().getName() + "\"/></a></div>");
            } else {
            	out.println("      [удалено]:");
            	out.println("      <div class=\"msg-avatar\"><img src=\"//i.juick.com/av-96.png\"/></div>");
            }
            out.println("        <div class=\"msg-menu\"><a href=\"#\" class=\"a-thread-links\"></a></div>");
            out.println("        <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + templates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>");
            out.println("      </div>");
            out.println("      <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>");
            if (msg.getAttachmentType() != null) {
                out.println("    <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
            }
            out.print("    <div class=\"msg-links\">/" + msg.getRid());
            if (msg.getReplyto() > 0) {
                out.print(" в ответ на <a href=\"#" + msg.getReplyto() + "\">/" + msg.getReplyto() + "</a>");
            }
            if (msg.VisitorCanComment) {
                out.println(" &#183; <a href=\"#\" class=\"a-thread-comment\">Ответить</a></div>");
                out.println("    <div class=\"msg-comment\" style=\"display: none\"></div>");
            } else if (visitor.getUid() == 0) {
                out.println("    <div class=\"msg-links\"><a href=\"/post?body=%23" + msg.getMid() + "/" + msg.getRid() + "%20\" class=\"a-thread-comment\">Ответить</a></div>");
            }
            out.println("    </div>");
            out.println("  </li>");
        }
    }
}