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.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 paramShow, @RequestParam("tag") Optional paramTagStr, @RequestParam(value = "before") Optional paramBefore, @RequestParam(value = "search", required = false) String paramSearch, ModelMap model) throws IOException { int before = paramBefore.orElse(0); String tag = paramTagStr.orElse(StringUtils.EMPTY); if (StringUtils.isNotEmpty(tag)) { StringBuilder builder = new StringBuilder(); builder.append("redirect:").append("/tag/").append(tag); if (before > 0) builder.append("?before=").append(before); return builder.toString(); } if (StringUtils.isNotEmpty(paramSearch) && paramSearch.length() > 64) paramSearch = StringUtils.EMPTY; User visitor = UserUtils.getCurrentUser(); String title = StringUtils.EMPTY; List 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 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()); 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"; } }