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

import com.juick.Message;
import com.juick.User;
import com.juick.server.util.HttpNotFoundException;
import com.juick.service.AdsService;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.UserUtils;
import com.juick.www.helpers.QueryString;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringEscapeUtils;
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;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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


    @RequestMapping("/tag/{tagName}")
    protected String doGet(
            @PathVariable("tagName") String paramTagStr,
            @RequestParam(value = "before", required = false, defaultValue = "0") Integer paramBefore,
            @QueryString Optional<String> queryString,
            ModelMap model) throws UnsupportedEncodingException {
        User visitor = UserUtils.getCurrentUser();

        com.juick.Tag paramTag = tagService.getTag(paramTagStr, false);
        if (paramTag == null) {
            throw new HttpNotFoundException();
        } else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) {
            com.juick.Tag synTag = tagService.getTag(paramTag.SynonymID);
            String url = "/tag/" + URLEncoder.encode(synTag.getName(), CharEncoding.UTF_8);
            if (queryString.isPresent()) {
                url += "?" + queryString.get();
            }
            return "redirect:" + url;
        } else if (!paramTag.getName().equals(paramTagStr)) {
            String url = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8);
            if (queryString.isPresent()) {
                url += "?" + queryString.get();
            }
            return "redirect:" + url;
        }

        int visitor_uid = visitor != null ? visitor.getUid() : 0;

        String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName());
        List<Integer> mids = messagesService.getTag(paramTag.TID, visitor_uid, paramBefore, (visitor == null) ? 40 : 20);
        model.addAttribute("title", title);
        if (tagService.getTagNoIndex(paramTag.TID)) {
            model.addAttribute("headers", "<meta name=\"robots\" content=\"noindex,nofollow\"/>");
        } else if (paramBefore > 0 || mids.size() < 5) {
            model.addAttribute("headers", "<meta name=\"robots\" content=\"noindex\"/>");
        }
        if (mids.size() > 0) {
            int vuid = visitor != null ? visitor.getUid() : 0;
            int ad_mid = adsService.getAdMid(vuid);
            if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) {
                mids.add(0, ad_mid);
                adsService.logAdMid(vuid, ad_mid);
            } else {
                ad_mid = 0;
            }
        }
        List<Message> msgs = messagesService.getMessages(mids);
        List<Integer> blUIDs = userService.checkBL(visitor_uid,
                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 = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "?before=" + mids.get(mids.size() - 1);
            model.addAttribute("nextpage", nextpage);
        }
        model.addAttribute("tags", tagService.getPopularTags());
        return "views/index";
    }

    @RequestMapping("/tag")
    public String redirectToMain() {
        return "redirect:/";
    }
}