From d9379351e992704c7b93ed0b74d2e1c60e166538 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Sep 2016 01:51:30 +0300 Subject: juick-www: signup/rss -> pebble --- juick-www/src/main/java/com/juick/www/Main.java | 14 +++- juick-www/src/main/java/com/juick/www/RSS.java | 63 ++++------------- juick-www/src/main/java/com/juick/www/SignUp.java | 86 +++++++---------------- juick-www/src/main/resources/layouts/rss.xml | 38 ++++++++++ juick-www/src/main/resources/views/signup.html | 41 +++++++++++ 5 files changed, 131 insertions(+), 111 deletions(-) create mode 100644 juick-www/src/main/resources/layouts/rss.xml create mode 100644 juick-www/src/main/resources/views/signup.html (limited to 'juick-www') diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index 57704f36..af8711ed 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -210,7 +210,12 @@ public class Main extends HttpServlet implements Stream.StreamListener { String uname = uri.substring(5); int uid = UserQueries.getUIDbyName(sql, uname); if (uid > 0) { - rss.doGet(sql, request, response, uid, uname); + try { + rss.doGet(sql, response, uid); + } catch (PebbleException e) { + log("pebble exception", e); + response.sendError(500); + } } else { response.sendError(404); } @@ -230,7 +235,12 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.startsWith("/_twitter")) { twitterAuth.doGet(sql, request, response); } else if (uri.equals("/signup")) { - signup.doGet(sql, request, response); + try { + signup.doGet(sql, request, response); + } catch (PebbleException e) { + log("pebble exception", e); + response.sendError(500); + } } else if (uri.equals("/help") || uri.equals("/help/")) { help.doRedirectToHelpIndex(response); } else if (uri.startsWith("/help/")) { diff --git a/juick-www/src/main/java/com/juick/www/RSS.java b/juick-www/src/main/java/com/juick/www/RSS.java index 23120140..f27ea539 100644 --- a/juick-www/src/main/java/com/juick/www/RSS.java +++ b/juick-www/src/main/java/com/juick/www/RSS.java @@ -19,16 +19,18 @@ package com.juick.www; import com.juick.Message; import com.juick.server.MessagesQueries; +import com.mitchellbosecke.pebble.error.PebbleException; +import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -38,7 +40,7 @@ public class RSS { private static final SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletResponse response, int uid) throws ServletException, IOException, PebbleException { List mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); if (mids.isEmpty()) { response.sendError(404); @@ -47,55 +49,16 @@ public class RSS { List msgs = MessagesQueries.getMessages(sql, mids); + msgs.forEach(m -> Utils.formatMessage(m.getText())); + response.setContentType("application/rss+xml; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println(""); - out.println("" + uname + " - Juick"); - out.println("http://juick.com/" + uname + "/"); - out.println("The latest messages by @" + uname + " at Juick"); - out.println("http://i.juick.com/a/" + uid + ".png" + uname + " - Juickhttp://juick.com/" + uname + "/"); - - for (Message msg : msgs) { - out.println(""); - out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); - out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); - - out.print("<![CDATA[@" + msg.getUser().getUName() + ":"); - if (!msg.Tags.isEmpty()) { - for (int n = 0; n < msg.Tags.size(); n++) { - out.print(" *" + msg.Tags.get(n)); - } - } - out.println("]]>"); - out.println(""); - - Date date = msg.getDate(); - out.println("" + sdfRSS.format(date) + ""); - - - out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); - if (!msg.Tags.isEmpty()) { - for (int n = 0; n < msg.Tags.size(); n++) { - out.println("" + msg.Tags.get(n) + ""); - } - } - if (msg.AttachmentType != null) { - if (msg.AttachmentType.equals("jpg")) { - out.println(""); - out.println(""); - } else if (msg.AttachmentType.equals("png")) { - out.println(""); - out.println(""); - } - } - out.println(""); - out.println(""); - } - - out.println(""); + PebbleTemplate template = Utils.getEngine().getTemplate("layouts/rss.xml"); + Map context = new HashMap<>(); + context.put("user", msgs.stream().findFirst().get().getUser()); + context.put("msgs", msgs); + context.put("sdfRSS", sdfRSS); + template.evaluate(out, context); } } } diff --git a/juick-www/src/main/java/com/juick/www/SignUp.java b/juick-www/src/main/java/com/juick/www/SignUp.java index a67d049a..43c82a9a 100644 --- a/juick-www/src/main/java/com/juick/www/SignUp.java +++ b/juick-www/src/main/java/com/juick/www/SignUp.java @@ -18,6 +18,8 @@ package com.juick.www; import com.juick.server.UserQueries; +import com.mitchellbosecke.pebble.error.PebbleException; +import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.apache.commons.lang3.tuple.Pair; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -28,7 +30,9 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -36,7 +40,7 @@ import java.util.List; */ public class SignUp { - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String type = request.getParameter("type"); @@ -47,14 +51,19 @@ public class SignUp { } String account = null; - if (type.equals("fb")) { - account = getFacebookNameByHash(sql, hash); - } else if (type.equals("vk")) { - account = getVKNameByHash(sql, hash); - } else if (type.equals("xmpp")) { - account = getJIDByHash(sql, hash); - } else if (type.equals("durov")) { - account = getTelegramNameByHash(sql, hash); + switch (type) { + case "fb": + account = getFacebookNameByHash(sql, hash); + break; + case "vk": + account = getVKNameByHash(sql, hash); + break; + case "xmpp": + account = getJIDByHash(sql, hash); + break; + case "durov": + account = getTelegramNameByHash(sql, hash); + break; } if (account == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); @@ -63,53 +72,14 @@ public class SignUp { response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - PageTemplates.pageHead(out, visitor, "Новый пользователь", null); - PageTemplates.pageNavigation(out, visitor, null); - - out.println("
"); - - out.print("

"); - if (type.charAt(0) == 'f') { - out.print("\"Facebook\"/"); - } else if (type.charAt(0) == 'v') { - out.print("\"VKontakte\"/"); - } else if (type.charAt(0) == 'x') { - out.print("\"XMPP\"/"); - } else if (type.charAt(0) == 'd') { - out.print("\"Telegram\"/"); - } - out.println(account + "

"); - - out.println("

Связать с существующим аккаунтом Juick

"); - out.println("
"); - out.println(""); - out.println(""); - out.println(""); - if (visitor.getUID() > 0) { - out.println(""); - } else { - out.println("

Имя пользователя:

"); - out.println("

Пароль:

"); - out.println("

"); - } - out.println("
"); - - out.println("
"); - - out.println("

Создать новый аккаунт Juick

"); - out.println("
"); - out.println(""); - out.println(""); - out.println(""); - out.println("

Имя пользователя:
(От 2-х до 16-и латинских символов и/или цифр, дефис)

"); - out.println("

Пароль:
(от 6-и до 32-х символов)

"); - out.println("

"); - out.println("
"); - - out.println("
"); - - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + PebbleTemplate template = Utils.getEngine().getTemplate("views/signup.html"); + Map context = new HashMap<>(); + context.put("title", "Новый пользователь"); + context.put("visitor", visitor); + context.put("account", account); + context.put("type", type); + context.put("hash", hash); + template.evaluate(out, context); } } @@ -233,9 +203,7 @@ public class SignUp { private String getVKNameByHash(JdbcTemplate sql, String hash) { List> logins = sql.query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?", - (rs, num) -> { - return Pair.of(rs.getString(1), rs.getString(2)); - }, hash); + (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash); if (logins.size() > 0) { return "" + logins.get(0).getLeft() + ""; } diff --git a/juick-www/src/main/resources/layouts/rss.xml b/juick-www/src/main/resources/layouts/rss.xml new file mode 100644 index 00000000..61dd3be8 --- /dev/null +++ b/juick-www/src/main/resources/layouts/rss.xml @@ -0,0 +1,38 @@ + + + + + {{ user.getUName() }} - Juick + http://juick.com/{{ user.getUName() }}/ + The latest messages by @{{ user.getUName() }} at Juick + + http://i.juick.com/a/{{ user.getUID() }}.png + {{ user.getUName() }} - Juick + http://juick.com/{{ user.getUName() }}/ + + {% for msg in msgs %} + + http://juick.com/{{ msg.getUser().getUName() }}/{{ msg.getMID() }} + http://juick.com/{{ msg.getUser().getUName() }}/{{ msg.getMID() }} + <![CDATA[@{{ msg.getUser().getUName() }}: {% for tag in msg.Tags %} *{{ tag }}{% endfor %}]]> + + {{ sdfRSS.format(msg.getDate()) }} + http://juick.com/{{ msg.getUser().getUName() }}/{{ msg.getMID() }} + {% for tag in msg.Tags %} + {{ tag }} + {% endfor %} + {% if msg.AttachmentType is not empty %} + {% if msg.AttachmentType == "jpg" %} + + + {% elseif msg.AttachmentType == "png" %} + + + {% endif %} + {% endif %} + + + {% endfor %} + + \ No newline at end of file diff --git a/juick-www/src/main/resources/views/signup.html b/juick-www/src/main/resources/views/signup.html new file mode 100644 index 00000000..724ad961 --- /dev/null +++ b/juick-www/src/main/resources/views/signup.html @@ -0,0 +1,41 @@ +{% extends "layouts/content.html" %} +{% block content %} +

+ {% if type | slice(0, 1) == 'f' %} + Facebook + {% elseif type | slice(0, 1) == 'v' %} + VKontakte + {% elseif type | slice(0, 1) == 'x' %} + XMPP + {% elseif type | slice(0, 1) == 'd' %} + Telegram + {% endif %} + {{ account | raw }}

+ + +
+ + + + {% if visitor.getUID() > 0 %} + + {% else %} +

Имя пользователя:

+

Пароль:

+

+ {% endif %} +
+ + + + +
+ + + +

Имя пользователя:
(От 2-х до 16-и латинских символов + и/или цифр, дефис)

+

Пароль:
(от 6-и до 32-х символов)

+

+
+{% endblock %} \ No newline at end of file -- cgit v1.2.3