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.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. */ @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( Principal principal, @CookieValue("ref") Optional ref, @RequestHeader("Referer") Optional referer, @RequestParam("show") Optional paramShow, @RequestParam("tag") Optional paramTagStr, @RequestParam(value = "before") Optional 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); String title; if (visitor.getUid() > 0) { title = "Популярные"; } else { title = "Микроблоги Juick: популярные записи"; } int before = paramBefore.orElse(0); 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("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 "index"; } }