diff options
Diffstat (limited to 'src/java/com')
-rw-r--r-- | src/java/com/juick/api/Main.java | 10 | ||||
-rw-r--r-- | src/java/com/juick/api/Messages.java | 39 | ||||
-rw-r--r-- | src/java/com/juick/api/PM.java | 6 | ||||
-rw-r--r-- | src/java/com/juick/api/Utils.java | 11 |
4 files changed, 62 insertions, 4 deletions
diff --git a/src/java/com/juick/api/Main.java b/src/java/com/juick/api/Main.java index 718ca2497..38716282b 100644 --- a/src/java/com/juick/api/Main.java +++ b/src/java/com/juick/api/Main.java @@ -44,6 +44,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Connection sql; Connection sqlSearch; Stream xmpp; + Messages messages; Users users; PM pm; Others others; @@ -59,6 +60,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password="); + messages = new Messages(sql); users = new Users(sql); pm = new PM(sql); others = new Others(sql); @@ -138,7 +140,13 @@ public class Main extends HttpServlet implements Stream.StreamListener { } String uri = request.getRequestURI(); - if (uri.equals("/users")) { + if (uri.equals("/home")) { + if (vuid > 0) { + messages.doGetHome(request, response, vuid); + } else { + response.sendError(401); + } + } else if (uri.equals("/users")) { users.doGetUsers(request, response, vuid); } else if (uri.equals("/users/read")) { users.doGetUserRead(request, response, vuid); diff --git a/src/java/com/juick/api/Messages.java b/src/java/com/juick/api/Messages.java new file mode 100644 index 000000000..5ecf503f2 --- /dev/null +++ b/src/java/com/juick/api/Messages.java @@ -0,0 +1,39 @@ +package com.juick.api; + +import com.juick.server.MessagesQueries; +import java.io.IOException; +import java.sql.Connection; +import java.util.ArrayList; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author ugnich + */ +public class Messages { + + Connection sql; + + public Messages(Connection sql) { + this.sql = sql; + } + + public void doGetHome(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + + ArrayList<Integer> mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); + if (mids != null && !mids.isEmpty()) { + ArrayList<com.juick.Message> msgs = MessagesQueries.getMessages(sql, mids); + if (msgs != null && !msgs.isEmpty()) { + String json = com.juick.json.Messages.arrayToString(msgs); + Main.replyJSON(request, response, json); + } else { + response.sendError(404); + } + } else { + response.sendError(404); + } + } +} diff --git a/src/java/com/juick/api/PM.java b/src/java/com/juick/api/PM.java index 285f9ff6f..700f16655 100644 --- a/src/java/com/juick/api/PM.java +++ b/src/java/com/juick/api/PM.java @@ -59,14 +59,12 @@ public class PM { return; } - if (UserQueries.isInBL(sql, uid, vuid)) { + if (UserQueries.isInBL(sql, uid, vuid) || UserQueries.isInBL(sql, vuid, uid)) { response.sendError(403); return; } if (PMQueries.createPM(sql, vuid, uid, body)) { - Main.replyJSON(request, response, ""); - Message msg = new Message(); msg.from = new JID("juick", "juick.com", null); msg.to = new JID(Integer.toString(uid), "push.juick.com", null); @@ -80,6 +78,8 @@ public class PM { msg.to.Host = "ws.juick.com"; xmpp.send(msg); + Main.replyJSON(request, response, com.juick.json.Message.toJSON(jmsg).toString()); + String jid = UserQueries.getJIDbyUID(sql, uid); if (jid != null) { Message mm = new Message(); diff --git a/src/java/com/juick/api/Utils.java b/src/java/com/juick/api/Utils.java index b406c3620..10e0cccb0 100644 --- a/src/java/com/juick/api/Utils.java +++ b/src/java/com/juick/api/Utils.java @@ -135,4 +135,15 @@ public class Utils { ret = ret.replaceAll("@", "\\\\@"); return ret; } + + public static int parseInt(String str, int def) { + int ret = def; + if (str != null) { + try { + ret = Integer.parseInt(str); + } catch (Exception e) { + } + } + return ret; + } } |