/* * Juick * Copyright (C) 2008-2011, Ugnich Anton * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.juick.www.controllers; import com.juick.server.helpers.TagStats; import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.UserUtils; import com.juick.www.Utils; import com.juick.www.WebApp; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * * @author Ugnich Anton */ @Controller public class User { @Inject WebApp webApp; @Inject UserService userService; @Inject TagService tagService; @Inject MessagesService messagesService; @Inject PageTemplates templates; @GetMapping("/{uname}/") protected void doGetBlog(HttpServletRequest request, HttpServletResponse response, @PathVariable String uname, @RequestParam(required = false, defaultValue = "0") Integer before) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (user.isBanned()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } List mids; String paramShow = request.getParameter("show"); com.juick.Tag paramTag = null; String paramTagStr = request.getParameter("tag"); if (paramTagStr != null) { if (paramTagStr.length() < 64) { paramTag = tagService.getTag(paramTagStr, false); } if (paramTag == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } else if (!paramTag.getName().equals(paramTagStr)) { String url = "/" + user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); Utils.sendPermanentRedirect(response, url); return; } } String paramSearch = request.getParameter("search"); if (paramSearch != null && paramSearch.length() > 64) { paramSearch = null; } int privacy = 0; if (visitor.getUid() > 0) { if (user.getUid() == visitor.getUid() || visitor.getUid() == 1) { privacy = -3; } else if (userService.isInWL(user.getUid(), visitor.getUid())) { privacy = -2; } } String title; if (paramShow == null) { if (paramTag != null) { title = "Блог " + user.getName() + ": *" + StringEscapeUtils.escapeHtml4(paramTag.getName()); mids = messagesService.getUserTag(user.getUid(), paramTag.TID, privacy, before); } else if (paramSearch != null) { title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch); mids = messagesService.getUserSearch(user.getUid(), Utils.encodeSphinx(paramSearch), privacy, before); } else { title = "Блог " + user.getName(); mids = messagesService.getUserBlog(user.getUid(), privacy, before); } } else if (paramShow.equals("recomm")) { title = "Рекомендации " + user.getName(); mids = messagesService.getUserRecommendations(user.getUid(), before); } else if (paramShow.equals("photos")) { title = "Фотографии " + user.getName(); mids = messagesService.getUserPhotos(user.getUid(), privacy, before); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = ""; if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { head += ""; } else if (before > 0 || paramShow != null) { head += ""; } templates.pageHead(out, visitor, title, head); templates.pageNavigation(out, visitor, null); pageUserColumn(out, user, visitor); if (mids.size() > 0) { out.println("
"); if (paramTag != null) { out.println("

← Все записи с тегом " + StringEscapeUtils.escapeHtml4(paramTag.getName()) + "

"); } templates.printMessages(out, user, mids, visitor); if (mids.size() >= 20) { String nextpage = "?before=" + mids.get(mids.size() - 1); if (paramShow != null) { nextpage += "&show=" + paramShow; } if (paramTag != null) { nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); } if (paramSearch != null) { nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); } out.println("

Читать дальше →

"); } out.println("
"); } templates.pageFooter(request, out, visitor, true); templates.pageEnd(out); } } @GetMapping("/{uname}/tags") protected void doGetTags(HttpServletRequest request, HttpServletResponse response, @PathVariable String uname) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = ""; templates.pageHead(out, visitor, "Теги " + user.getName(), head); templates.pageNavigation(out, visitor, null); pageUserColumn(out, user, visitor); out.println("
"); out.println("

" + pageUserTags(user, visitor, 0) + "

"); out.println("
"); templates.pageFooter(request, out, visitor, false); templates.pageEnd(out); } } @GetMapping("/{uname}/friends") protected void doGetFriends(HttpServletRequest request, HttpServletResponse response, @PathVariable String uname) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { throw new HttpForbiddenException(); } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = ""; templates.pageHead(out, visitor, "Подписки " + user.getName(), head); templates.pageNavigation(out, visitor, null); pageUserColumn(out, user, visitor); out.println("
"); out.println(""); List friends = userService.getUserFriends(user.getUid()); for (int i = 0; i < friends.size(); i++) { if (i % 3 == 0 && i > 0) { out.print(""); } out.print(""); } out.println("
" + friends.get(i).getName() + "
"); out.println("
"); templates.pageFooter(request, out, visitor, false); templates.pageEnd(out); } } @GetMapping("/{uname}/readers") protected void doGetReaders(HttpServletRequest request, HttpServletResponse response, @PathVariable String uname) throws IOException { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { throw new HttpForbiddenException(); } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = ""; templates.pageHead(out, visitor, "Читатели " + user.getName(), head); templates.pageNavigation(out, visitor, null); pageUserColumn(out, user, visitor); out.println("
"); out.println(""); List readers = userService.getUserReaders(user.getUid()); for (int i = 0; i < readers.size(); i++) { if (i % 3 == 0 && i > 0) { out.print(""); } out.print(""); } out.println("
" + readers.get(i).getName() + "
"); out.println("
"); templates.pageFooter(request, out, visitor, false); templates.pageEnd(out); } } public void pageUserColumn(PrintWriter out, com.juick.User user, com.juick.User visitor) { out.println(""); } public String pageUserTags(com.juick.User user, com.juick.User visitor, int cnt) { List tags = tagService.getUserTagStats(user.getUid()).stream() .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).collect(Collectors.toList()); int maxUsageCnt = tags.stream().map(TagStats::getUsageCount).max(Comparator.naturalOrder()).orElse(0); String ret = StringUtils.EMPTY; int count = cnt > 0 ? Math.min(tags.size(), cnt) : tags.size(); for (int i = 0; i < count; i++) { String tag = StringEscapeUtils.escapeHtml4(tags.get(i).getTag().getName()); try { tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } if (tags.get(i).getUsageCount() > maxUsageCnt / 3 * 2) { ret += "" + tag + " "; } else if (tags.get(i).getUsageCount() > maxUsageCnt / 3) { ret += "" + tag + " "; } else { ret += tag + " "; } } return ret; } }