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/IndexController.java | 117 +++++++++++++++++++-- 1 file changed, 107 insertions(+), 10 deletions(-) (limited to 'juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java') diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java index 2973bf30..91efd8aa 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java @@ -1,9 +1,26 @@ 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.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.*; + +import javax.inject.Inject; +import java.io.IOException; +import java.net.URLEncoder; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/21/16. @@ -11,14 +28,94 @@ import org.springframework.web.bind.annotation.RequestParam; @Controller public class IndexController { - @RequestMapping("/") - public String index( - java.security.Principal userPrincipal, - @RequestParam(required = false, defaultValue = "0") int before, - Model model) { + @Inject + UserService userService; + @Inject + MessagesService messagesService; + @Inject + TagService tagService; + + @RequestMapping(value = "/", method = RequestMethod.GET) + protected String doGet( + Principal principal, + @CookieValue("ref") Optional ref, + @RequestHeader("Referer") Optional referer, + @RequestParam("show") Optional paramShow, + @RequestParam("tag") Optional paramTagStr, + @RequestParam(value = "before", required = false) 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 = ""; + } + + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); - model.addAttribute("currentUser", userPrincipal); + String title; + if (visitor.getUid() > 0) { + title = "Популярные"; + } else { + title = "Микроблоги Juick: популярные записи"; + } + List mids = new ArrayList<>(); - return "index"; + if (StringUtils.isNotEmpty(paramSearch)) { + title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch); + mids = messagesService.getSearch(EncodeUtils.encodeSphinx(paramSearch), + paramBefore); + } else if (!paramShow.isPresent()) { + mids = messagesService.getPopular(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("top")) { + return "redirect:/"; + } else if (paramShow.get().equals("my") && visitor.getUid() > 0) { + title = "Моя лента"; + mids = messagesService.getMyFeed(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("private") && visitor.getUid() > 0) { + title = "Приватные"; + mids = messagesService.getPrivate(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("discuss") && visitor.getUid() > 0) { + title = "Обсуждения"; + mids = messagesService.getDiscussions(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("recommended") && visitor.getUid() > 0) { + title = "Рекомендации"; + mids = messagesService.getRecommended(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("photos")) { + title = "Фотографии"; + mids = messagesService.getPhotos(visitor.getUid(), paramBefore); + } else if (paramShow.get().equals("all")) { + title = "Все сообщения"; + mids = messagesService.getAll(visitor.getUid(), paramBefore); + } + model.addAttribute("title", title); + model.addAttribute("visitor", visitor); + model.addAttribute("tags", tagService.getPopularTags()); + model.addAttribute("showAdv", true); + List msgs = messagesService.getMessages(mids); + List 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()); + 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); + } + //model.addAttribute("isModerator", userService.getModerators().contains(visitor.getUid())); + return "blog/index"; } } -- cgit v1.2.3