aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/UserThread.java
blob: 88217b9d630283bfdccd8b2b80abcecd70889876 (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
/*
 * 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.Tag;
import com.juick.formatters.PlainTextFormatter;
import com.juick.server.util.HttpForbiddenException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.UserUtils;
import com.juick.service.CrosspostService;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
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.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.function.BooleanSupplier;
import java.util.List;
import java.util.stream.Collectors;

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

    @Inject
    private MessagesService messagesService;
    @Inject
    private UserService userService;
    @Inject
    private CrosspostService crosspostService;

    @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) {
            boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid();
            boolean isInBL = userService.isInBL(msg.getUser().getUid(), visitor.getUid());
            msg.VisitorCanComment = isMsgAuthor || !(msg.ReadOnly || isInBL);
        }
        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\"/>";
        String pageUrl = "https://juick.com/" + msg.getUser().getName() + "/" + msg.getMid();
        if (paramView != null) {
            headers += "<link rel=\"canonical\" href=\"" + pageUrl + "\"/>";
        }
        if (msg.Hidden) {
            headers += "<meta name=\"robots\" content=\"noindex\"/>";
        }
        String cardType = StringUtils.isNotEmpty(msg.getAttachmentType()) ? "summary_large_image" : "summary";
        String msgImage = StringUtils.isNotEmpty(msg.getAttachmentType()) ? msg.getAttachment().getMedium().getUrl()
                : "https://i.juick.com/a/" + msg.getUser().getUid() + ".png";
        model.addAttribute("ogtype", "article");
        String cardDescription = StringEscapeUtils.escapeHtml4(PlainTextFormatter.formatTwitterCard(msg));
        headers += "<meta name=\"twitter:card\" content=\"" + cardType + "\" />\n" +
                "<meta name=\"twitter:site\" content=\"@juick\" />\n" +
                "<meta property=\"og:url\" content=\"" + pageUrl + "\" />\n" +
                "<meta property=\"og:title\" content=\"" + msg.getUser().getName() + " at Juick\" />\n" +
                "<meta property=\"og:description\" content=\"" + cardDescription + "\" />\n" +
                "<meta name=\"Description\" content=\"" + cardDescription + "\" />\n" +
                "<meta property=\"og:image\" content=\"" + msgImage + "\" />";
        String twitterName = crosspostService.getTwitterName(msg.getUser().getUid());
        if (StringUtils.isNotEmpty(twitterName)) {
            headers += "<meta name=\"twitter:creator\" content=\"@" + twitterName + "\" />\n";
        }
        if (msg.getTags().size() > 0) {
            headers += "<meta name=\"Keywords\" content=\"" + msg.getTags().stream().map(Tag::getName)
                    .collect(Collectors.joining(", ")) + "\" />\n";
        }
        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);
                }
            }

            reply.VisitorCanComment = visitor.getUid() > 0;
            if (visitor.getUid() > 0) {
                boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid();
                boolean isReplyAuthor = visitor.getUid() == reply.getUser().getUid();
                BooleanSupplier isInBL2 = () -> userService.checkBL(visitor.getUid(), blUIDs).contains(reply.getUser().getUid());
                reply.VisitorCanComment = isMsgAuthor || (!msg.ReadOnly && (isReplyAuthor || !isInBL2.getAsBoolean()));
            }
        }

        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);
    }
}