aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick/www/controllers/ThreadController.java
blob: f8693e1369ff7f54715b1809710d923ce7fa7555 (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
package com.juick.www.controllers;

import com.juick.server.util.HttpForbiddenException;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.inject.Inject;

/**
 * Created by vitalyster on 20.12.2016.
 */
@Controller
public class ThreadController {
    @Inject
    MessagesService messagesService;
    @Inject
    UserService userService;

    @RequestMapping(value = "/{userName}/{mid}")
    public String doGetThread(
            @PathVariable int mid,
            @RequestParam(required = false, value = "view") String paramView,
            ModelMap modelMap) {
        com.juick.User visitor = UserUtils.getCurrentUser();

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

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

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

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

        modelMap.put("title", title);
        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\"/>";
        }
        modelMap.put("headers", headers);
        modelMap.put("msg", msg);
        modelMap.put("listview", listview);
        return "views/thread";
    }
}