aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
blob: 9ab453330f4764007655f024f0a58e27b270329a (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
/*
 * Copyright (C) 2008-2017, Juick
 *
 * 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.util.HttpForbiddenException;
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.server.util.UserUtils;
import com.juick.www.WebApp;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
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;

    @GetMapping("/{uname}/{mid}")
    protected String threadAction(ModelMap model,
                                  @PathVariable String uname,
                                  @PathVariable int mid,
                                  @RequestParam(required = false, value = "view") String paramView) throws IOException {
        com.juick.User visitor = UserUtils.getCurrentUser();

        if (!messagesService.canViewThread(mid, visitor.getUid())) {
            throw new HttpForbiddenException();
        }

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

        if (msg == null || msg.getUser().isBanned()) {
            throw new HttpNotFoundException();
        }

        com.juick.User user = userService.getUserByName(uname);
        if (user.getUid() == 0 || !msg.getUser().equals(user)) {
            return String.format("redirect:/%s/%d", msg.getUser().getName(), mid);
        }
        msg.VisitorCanComment = visitor.getUid() > 0;
        if (visitor.getUid() > 0) {
            msg.VisitorCanComment = visitor.getUid() == msg.getUser().getUid()
                    || !userService.isInBL(msg.getUser().getUid(), visitor.getUid());
        }
        model.addAttribute("msg", msg);

        boolean listview = false;
        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;
        }
        model.addAttribute("listview", listview);
        String title = msg.getUser().getName() + ": " + msg.getTagsString();

        model.addAttribute("title", title);
        model.addAttribute("visitor", visitor);
        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\"/>";
        }
        model.addAttribute("headers", headers);
        model.addAttribute("contentStyle", "margin-left: 0; width: 100%");
        model.addAttribute("isModerator", visitor.getUid() == 3694);
        model.addAttribute("visitorSubscribed", messagesService.isSubscribed(visitor.getUid(), msg.getMid()));
        model.addAttribute("visitorInBL",  userService.isInBL(msg.getUser().getUid(), visitor.getUid()));
        model.addAttribute("recomm", messagesService.getMessageRecommendations(msg.getMid()));
        List<com.juick.Message> replies = messagesService.getReplies(msg.getMid());

        List<Integer> blUIDs = new ArrayList<>();
        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 (visitor.getUid() > 0 && msg.getUser().getUid() == visitor.getUid()) {
                reply.VisitorCanComment = true;
            } else if (visitor.getUid() > 0 && msg.VisitorCanComment) {
                List<Integer> blUIDs2 = userService.checkBL(visitor.getUid(), blUIDs);
                reply.VisitorCanComment = reply.getUser().getUid() == visitor.getUid()
                        || !blUIDs2.contains(reply.getUser().getUid());
            } else {
                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;
                }
            }
        }
        model.addAttribute("replies", replies);
        model.addAttribute("foldable", foldable);
        return "views/thread";
    }

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