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 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 mids = messagesService.getTag(paramTag.TID, visitor_uid, paramBefore, (visitor == null) ? 40 : 20); model.addAttribute("title", title); if (tagService.getTagNoIndex(paramTag.TID)) { model.addAttribute("headers", ""); } else if (paramBefore > 0 || mids.size() < 5) { model.addAttribute("headers", ""); } 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 msgs = messagesService.getMessages(mids); List 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:/"; } }