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

import com.juick.Message;
import com.juick.User;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.UserUtils;
import com.juick.www.util.EncodeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.inject.Inject;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by aalexeev on 11/21/16.
 */
@Controller
public class IndexController {
    @Inject
    private MessageSource messageSource;
    @Inject
    private UserService userService;
    @Inject
    private MessagesService messagesService;
    @Inject
    private TagService tagService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String indexPage(
            @RequestParam("show") Optional<String> paramShow,
            @RequestParam("tag") Optional<String> paramTagStr,
            @RequestParam(value = "before") Optional<Integer> paramBefore,
            @RequestParam(value = "search", required = false) String paramSearch,
            ModelMap model) throws IOException {

        if (paramTagStr.isPresent())
            return "redirect:/tag/" + URLEncoder.encode(paramTagStr.get(), "UTF-8");

        if (StringUtils.isNotEmpty(paramSearch) && paramSearch.length() > 64)
            paramSearch = "";

        User visitor = UserUtils.getCurrentUser();

        String title = "";

        int before = paramBefore.orElse(0);

        List<Integer> mids = new ArrayList<>();

        if (StringUtils.isNotEmpty(paramSearch)) {
            title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch);
            mids = messagesService.getSearch(EncodeUtils.encodeSphinx(paramSearch),
                    before);
        } else if (!paramShow.isPresent()) {
            mids = messagesService.getPopular(visitor.getUid(), before);
        } else if (paramShow.get().equals("top")) {
            return "redirect:/";
        } else if (paramShow.get().equals("my") && visitor.getUid() > 0) {
            title = "Моя лента";
            mids = messagesService.getMyFeed(visitor.getUid(), before);
        } else if (paramShow.get().equals("private") && visitor.getUid() > 0) {
            title = "Приватные";
            mids = messagesService.getPrivate(visitor.getUid(), before);
        } else if (paramShow.get().equals("discuss") && visitor.getUid() > 0) {
            title = "Обсуждения";
            mids = messagesService.getDiscussions(visitor.getUid(), before);
        } else if (paramShow.get().equals("recommended") && visitor.getUid() > 0) {
            title = "Рекомендации";
            mids = messagesService.getRecommended(visitor.getUid(), before);
        } else if (paramShow.get().equals("photos")) {
            title = "Фотографии";
            mids = messagesService.getPhotos(visitor.getUid(), before);
        } else if (paramShow.get().equals("all")) {
            title = "Все сообщения";
            mids = messagesService.getAll(visitor.getUid(), before);
        }
        model.addAttribute("title", title);
        model.addAttribute("tags", tagService.getPopularTags());
        List<Message> msgs = messagesService.getMessages(mids);
        List<Integer> blUIDs = userService.checkBL(visitor.getUid(),
                msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList()));
        model.addAttribute("msgs",
                msgs.stream().map(msg -> {
                    msg.ReadOnly |= blUIDs.contains(msg.getUser().getUid());
                    if (msg.ReadOnly) {
                        msg.ReadOnly = visitor.getUid() != msg.getUser().getUid();
                    }
                    return msg;
                }).collect(Collectors.toList())
        );
        if (mids.size() >= 20) {
            String nextpage = "?before=" + mids.get(mids.size() - 1);
            if (paramShow.isPresent()) {
                nextpage += "&show=" + paramShow.get();
            }
            if (StringUtils.isNotEmpty(paramSearch)) {
                nextpage += "&search=" + paramSearch;
            }
            model.addAttribute("nextpage", nextpage);
        }
        return "views/index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLoginForm() {
        return "views/login";
    }

    @RequestMapping(value = "/login-error", method = RequestMethod.GET)
    public String getLoginErrorForm(Model model) {
        model.addAttribute("loginError", true);
        return "views/login";
    }
}