From 0b3c15851c0fbad3ee63cf0df298186a26d2a462 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 24 Apr 2014 01:29:12 +0700 Subject: Recommendations --- src/java/com/juick/http/www/Main.java | 7 ++++++ src/java/com/juick/http/www/NewMessage.java | 35 ++++++++++++++++++++++++++ src/java/com/juick/http/www/PageTemplates.java | 8 +++--- src/java/com/juick/http/www/Utils.java | 25 ++++++++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) (limited to 'src/java/com/juick') diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 13506db7..ec7c4e1e 100644 --- a/src/java/com/juick/http/www/Main.java +++ b/src/java/com/juick/http/www/Main.java @@ -285,6 +285,13 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else { response.sendError(403); } + } else if (uri.equals("/like")) { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor != null) { + pagesNewMessage.doPostRecomm(sql, request, response, xmpp, visitor); + } else { + response.sendError(403); + } } else if (uri.equals("/pm/send")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { diff --git a/src/java/com/juick/http/www/NewMessage.java b/src/java/com/juick/http/www/NewMessage.java index 79b087eb..fb5e6784 100644 --- a/src/java/com/juick/http/www/NewMessage.java +++ b/src/java/com/juick/http/www/NewMessage.java @@ -26,6 +26,7 @@ import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; +import com.juick.xmpp.extensions.JuickUser; import com.juick.xmpp.extensions.Nickname; import com.juick.xmpp.extensions.XOOB; import java.io.IOException; @@ -336,4 +337,38 @@ public class NewMessage { Utils.sendTemporaryRedirect(response, "/" + msg.User.UName + "/" + mid + "#" + ridnew); } + + public void doPostRecomm(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + int mid = Utils.parseInt(request.getParameter("mid"), 0); + if (mid == 0) { + response.sendError(400); + return; + } + com.juick.Message msg = MessagesQueries.getMessage(sql, mid); + if (msg == null) { + response.sendError(404); + return; + } + if (msg.User.UID == visitor.UID) { + response.sendError(403); + return; + } + + boolean res = MessagesQueries.recommendMessage(sql, mid, visitor.UID); + + if (res) { + Message xmsg = new Message(); + xmsg.from = new JID("juick", "juick.com", null); + xmsg.to = new JID("recomm", "s2s.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.MID = mid; + jmsg.User = new JuickUser(visitor); + xmsg.addChild(jmsg); + xmpp.send(xmsg); + + Utils.replyJSON(request, response, "{\"status\":\"ok\"}"); + } else { + response.sendError(500); + } + } } diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java index d88639a0..b2586472 100644 --- a/src/java/com/juick/http/www/PageTemplates.java +++ b/src/java/com/juick/http/www/PageTemplates.java @@ -52,9 +52,9 @@ public class PageTemplates { out.println(""); out.print(""); out.print("
"); - out.print(""); + out.print(""); out.print(""); - out.print(""); + out.print(""); if (headers != null) { out.print(headers); } @@ -411,7 +411,7 @@ public class PageTemplates { // [link text][http://juick.com/last?page=2] // link text msg = msg.replaceAll("\\[([^\\]]+)\\]\\[((?:ht|f)tps?://[^\\]]+)\\]", "$1"); - + // #12345 // #12345 msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); @@ -533,7 +533,7 @@ public class PageTemplates { } else if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) { out.print("Комментировать "); } - out.print("Рекомендовать"); + out.print("Мне нравится"); if (visitor != null && visitor.UID == 1) { out.print(" +"); out.print(" -"); diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java index 7a8abecb..2ba12f5e 100644 --- a/src/java/com/juick/http/www/Utils.java +++ b/src/java/com/juick/http/www/Utils.java @@ -19,8 +19,10 @@ package com.juick.http.www; import java.io.BufferedReader; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.sql.Connection; @@ -126,6 +128,29 @@ public class Utils { } } + public static void replyJSON(HttpServletRequest request, HttpServletResponse response, String json) throws IOException { + response.setContentType("application/json; charset=UTF-8"); + response.setHeader("Access-Control-Allow-Origin", "*"); + + String callback = request.getParameter("callback"); + if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-\\_]+"))) { + callback = null; + } + + PrintWriter out = response.getWriter(); + try { + if (callback != null) { + out.print(callback + "("); + out.print(json); + out.print(")"); + } else { + out.print(json); + } + } finally { + out.close(); + } + } + public static String convertArray2String(ArrayList