From 559ae343aa72db04e12b94acfa0bec30ed9a11f1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 9 Dec 2016 16:14:27 +0300 Subject: juick-spring-www: merge some work from develop branch, port some templates to Thymeleaf --- .../com/juick/www/controllers/TagController.java | 86 ++++++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) (limited to 'juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java') diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java index 5c3b6287..0961f683 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java @@ -1,14 +1,28 @@ 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.StringEscapeUtils; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; +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.security.Principal; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/21/16. @@ -19,15 +33,75 @@ public class TagController { private TagService tagService; @Inject private MessagesService messagesService; + @Inject + private AdsService adsService; + @Inject + private UserService userService; @RequestMapping("/tag/{tagName}") - public String showTags( - @PathVariable String tagName, - @RequestParam(required = false, defaultValue = "0") int before, - Model model) { + protected String doGet( + Principal principal, + @PathVariable("tagName") String paramTagStr, + @RequestParam(value = "before", required = false, defaultValue = "0") Integer paramBefore, + @QueryString Optional queryString, + ModelMap model) throws UnsupportedEncodingException { + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + + 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(), "UTF-8"); + if (queryString.isPresent()) { + url += "?" + queryString.get(); + } + return "redirect:" + url; + } else if (!paramTag.getName().equals(paramTagStr)) { + String url = "/tag/" + URLEncoder.encode(paramTag.getName(), "UTF-8"); + if (queryString.isPresent()) { + url += "?" + queryString.get(); + } + return "redirect:" + url; + } + + int visitor_uid = visitor != null ? visitor.getUid() : 0; - return "index"; + 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; + } + } + model.addAttribute("visitor", visitor); + 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()); + return msg; + }).collect(Collectors.toList()) + ); + if (mids.size() >= 20) { + String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), "UTF-8") + "?before=" + mids.get(mids.size() - 1); + model.addAttribute("nextpage", nextpage); + } + return "blog/index"; } @RequestMapping("/tag") -- cgit v1.2.3