aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ugnich Anton2014-04-24 01:29:12 +0700
committerGravatar Ugnich Anton2014-04-24 01:29:12 +0700
commit0b3c15851c0fbad3ee63cf0df298186a26d2a462 (patch)
treeef5a750def2821e8d56eae6563737e8773d12b50
parent69453d196eb1dcc928c628c7ca32ba2f69ea22ea (diff)
Recommendations
-rw-r--r--src/java/com/juick/http/www/Main.java7
-rw-r--r--src/java/com/juick/http/www/NewMessage.java35
-rw-r--r--src/java/com/juick/http/www/PageTemplates.java8
-rw-r--r--src/java/com/juick/http/www/Utils.java25
-rw-r--r--web/scripts3.js14
5 files changed, 85 insertions, 4 deletions
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("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
- out.print("<link rel=\"stylesheet\" href=\"/style.2014041701.css\"/>");
+ out.print("<link rel=\"stylesheet\" href=\"/style.2014042400.css\"/>");
out.print("<script type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\"></script>");
- out.print("<script type=\"text/javascript\" src=\"/scripts.2014041701.js\"></script>");
+ out.print("<script type=\"text/javascript\" src=\"/scripts.2014042200.js\"></script>");
if (headers != null) {
out.print(headers);
}
@@ -411,7 +411,7 @@ public class PageTemplates {
// [link text][http://juick.com/last?page=2]
// <a href="http://juick.com/last?page=2" rel="nofollow">link text</a>
msg = msg.replaceAll("\\[([^\\]]+)\\]\\[((?:ht|f)tps?://[^\\]]+)\\]", "<a href=\"$2\" rel=\"nofollow\">$1</a>");
-
+
// #12345
// <a href="http://juick.com/12345">#12345</a>
msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1<a href=\"http://juick.com/$2\">#$2</a>$3");
@@ -533,7 +533,7 @@ public class PageTemplates {
} else if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) {
out.print("<a href=\"#\" onclick=\"return showCommentFooter(this)\">Комментировать</a> ");
}
- out.print("<a href=\"/post?body=%21+%23" + msg.MID + "\">Рекомендовать</a>");
+ out.print("<a href=\"#\" onclick=\"return likeMessage(this," + msg.MID + ")\">Мне нравится</a>");
if (visitor != null && visitor.UID == 1) {
out.print(" <a href=\"#\" onclick=\"return setPopular(this," + msg.MID + ",2)\">+</a>");
out.print(" <a href=\"#\" onclick=\"return setPopular(this," + msg.MID + ",-1)\">-</a>");
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<Integer> mids) {
String q = "";
for (int i = 0; i < mids.size(); i++) {
diff --git a/web/scripts3.js b/web/scripts3.js
index c5fb472c..7651ab38 100644
--- a/web/scripts3.js
+++ b/web/scripts3.js
@@ -243,6 +243,20 @@ function openDialogLogin() {
/******************************************************************************/
+function likeMessage(e,mid) {
+ $.ajax({
+ url: 'http://juick.com/like?mid='+mid,
+ type: 'POST'
+ }).done(function() {
+ $(e).closest("article").append("<p>OK!</p>");
+ }).fail(function() {
+ $(e).closest("article").append("<p>Ошибка</p>");
+ });
+ return false;
+}
+
+/******************************************************************************/
+
function setPopular(e,mid,popular) {
$.ajax('http://api.juick.com/messages/set_popular?mid='+mid+'&popular='+popular+'&hash='+hash).done(function() {
var a=$(e).closest("article");