/* * 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.Message; import com.juick.server.helpers.TagStats; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.MessageUtils; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import ru.sape.Sape; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * @author Ugnich Anton */ public class PageTemplates { private static final Logger logger = LoggerFactory.getLogger(PageTemplates.class); public Sape sape = null; protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy"); private static String tagsHTML = null; @Inject TagService tagService; @Inject MessagesService messagesService; @Inject UserService userService; public void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) { out.println(""); out.print(""); out.print(""); out.println(""); out.print(""); out.print(""); if (headers != null) { out.print(headers); } out.print("" + title + ""); out.println(""); out.println(""); out.println(""); out.println(""); out.flush(); if (visitor.getUid() > 0) { out.println(""); } else { out.println(""); } } public void pageNavigation(PrintWriter out, com.juick.User visitor, String search) { out.println("
"); out.println(" "); out.print(" "); out.print("
"); out.println("
"); if (visitor.getUid() > 0) { out.print(" "); out.print(" "); } else { out.println("

Чтобы добавлять сообщения и комментарии, представьтесь.

"); } out.println("
"); out.println("
"); } public void pageHomeColumn(PrintWriter out, com.juick.User visitor) { pageHomeColumn(out, visitor, false); } public void pageHomeColumn(PrintWriter out, com.juick.User visitor, boolean showAdv) { if (tagsHTML == null) { tagsHTML = formatPopularTags(80); } out.println(""); } public String formatPopularTags(int cnt) { List popularTags = tagService.getPopularTags().stream() .map(t -> "" + StringEscapeUtils.escapeHtml4(t) + "").collect(Collectors.toList()); return StringUtils.collectionToDelimitedString(popularTags, " "); } public void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { out.println("
"); out.println(" "); out.print("
"); out.print("Twitter"); out.print("ВКонтакте"); out.print("Facebook"); out.println("
"); out.print("
juick.com © 2008-2017"); String queryString = request.getQueryString(); String requestURI = request.getRequestURI(); if (sapeon && sape != null && (visitor.getUid() == 0 || visitor.getUid() == 1) && queryString == null) { String links = sape.getPageLinks(requestURI, request.getCookies()).render(); if (links != null && !links.isEmpty()) { out.print("
Спонсоры: " + links); } } out.println("
"); out.println("
"); out.println(""); } public void pageEnd(PrintWriter out) { out.println(""); } public String formatTags(List tags) { String ret = org.apache.commons.lang3.StringUtils.EMPTY; for (TagStats tag : tags) { String tagName = StringEscapeUtils.escapeHtml4(tag.getTag().getName()); try { ret += ""; } catch (UnsupportedEncodingException e) { } } return ret; } public String formatDate(int minutes, Date fulldate) { if (minutes < 1) { return "сейчас"; } else if (minutes < 60) { String unit; int ld = minutes % 10; if ((minutes < 10 || minutes > 20) && ld == 1) { unit = "минуту"; } else if ((minutes < 10 || minutes > 20) && ld > 1 && ld < 5) { unit = "минуты"; } else { unit = "минут"; } return minutes + " " + unit + " назад"; } else if (minutes < 1440) { int hours = (minutes / 60); String unit; int ld = hours % 10; if ((hours < 10 || hours > 20) && ld == 1) { unit = "час"; } else if ((hours < 10 || hours > 20) && ld > 1 && ld < 5) { unit = "часа"; } else { unit = "часов"; } return hours + " " + unit + " назад"; } else if (minutes < 20160) { int days = (minutes / 1440); String unit; int ld = days % 10; if ((days < 10 || days > 20) && ld == 1) { unit = "день"; } else if ((days < 10 || days > 20) && ld > 1 && ld < 5) { unit = "дня"; } else { unit = "дней"; } return days + " " + unit + " назад"; } else { String ret = sdfFull.format(fulldate); synchronized (sdfSQL) { try { Calendar c = Calendar.getInstance(); int curyear = c.get(Calendar.YEAR); c.setTime(fulldate); if (c.get(Calendar.YEAR) == curyear) { ret = sdfSimple.format(fulldate); } else { ret = sdfFull.format(fulldate); } } catch (Exception e) { logger.error("PARSE EXCEPTION: {}, exception {}", fulldate, e); } } return ret; } } public String formatJSLocalTime(Date ts) { return ""; } public String formatReplies(int replies) { int ld = replies % 10; int lh = replies % 100; if ((lh < 10 || lh > 20) && ld == 1) { return replies + " ответ"; } else if ((lh < 10 || lh > 20) && ld > 1 && ld < 5) { return replies + " ответа"; } else { return replies + " ответов"; } } public void printMessages(PrintWriter out, com.juick.User user, List mids, com.juick.User visitor, int YandexID, int ad_mid) { List msgs = messagesService.getMessages(mids); for (int i = 0; i < msgs.size(); i++) { com.juick.Message msg = msgs.get(i); if (msg.getMid() == ad_mid) { msgs.remove(i); msgs.add(0, msg); break; } } List blUIDs = new ArrayList(20); if (visitor != null) { for (Message msg : msgs) { blUIDs.add(msg.getUser().getUid()); } blUIDs = userService.checkBL(visitor.getUid(), blUIDs); } for (Message msg : msgs) { List tags = tagService.getMessageTags(msg.getMid()); String tagsStr = formatTags(tags); if (msg.ReadOnly) { tagsStr += "readonly"; } if (msg.getPrivacy() < 0) { tagsStr += "friends"; } if (msg.getMid() == ad_mid) { tagsStr += "реклама"; } String txt; if (msg.getTags().stream().anyMatch(t -> t.getName().equals("code"))) { txt = MessageUtils.formatMessageCode(msg.getText()); } else { txt = MessageUtils.formatMessage(msg.getText()); } out.println("
"); out.println("
"); out.println(" @" + msg.getUser().getName() + ":"); out.println("
\""
"); out.println(" "); out.println("
" + tagsStr + "
"); out.println("
"); if (msg.getAttachmentType() != null) { String fname = msg.getMid() + "." + msg.getAttachmentType(); out.println("

\"\"/

"); } out.println("

" + txt + "

"); if (msg.getAttachmentType() != null) { out.println("
"); } out.print(" "); out.print(" "); out.print("
"); } } }