diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/PushComponent.java | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index 732fe20d..eb2230cb 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -22,8 +22,8 @@ import com.google.android.gcm.server.MulticastResult; import com.google.android.gcm.server.Result; import com.google.android.gcm.server.Sender; import com.juick.json.MessageSerializer; -import com.juick.server.MessagesQueries; import com.juick.server.PushQueries; +import com.juick.server.SubscriptionsQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message.MessageListener; import com.juick.xmpp.Stream; @@ -46,22 +46,21 @@ import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.json.JSONObject; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.io.IOException; import java.net.Socket; -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -92,8 +91,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe wns_application_sip = conf.getProperty("wns_application_sip", ""); wns_client_secret = conf.getProperty("wns_client_secret", ""); GCMSender = new Sender(conf.getProperty("gcm_key")); - - setupSql(conf.getProperty("datasource_driver"), conf.getProperty("datasource_url")); + sql = (JdbcTemplate) sce.getServletContext().getAttribute("sql"); setupXmppComponent(new JID("", conf.getProperty("push_jid"), ""), conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")), conf.getProperty("push_xmpp_password", "")); } catch (IOException e) { @@ -108,10 +106,6 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe logger.info("component destroyed"); } - public void setupSql(String driverClass, String url) { - - } - public void setupXmppComponent(JID jid, String host, int port, String password) { try { socket = new Socket(host, port); @@ -140,39 +134,38 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe return; } logger.info("Message to push: " + msg.toString()); - + List<User> subscribedUsers = new ArrayList<>(); boolean isPM = jmsg.getMID() == 0; boolean isReply = jmsg.getRID() > 0; - int senderID = 0, recipientID = 0; - // reply pushed to original sender - if (isReply) { - com.juick.Message op = MessagesQueries.getMessage(sql, jmsg.getMID()); - senderID = op.getUser().getUID(); - // do not notify self - if (jmsg.getUser().getUID() == op.getUser().getUID()) { - return; - } - } + int pmTo = 0; if (isPM) { - // PM try { - recipientID = Integer.parseInt(msg.to.Username); + pmTo = Integer.parseInt(msg.to.Username); } catch (NumberFormatException e) { logger.info("Wrong PM recipient: " + msg.to.Username); return; } + } else { + if (isReply) { + subscribedUsers = + SubscriptionsQueries.getUsersSubscribedToComments(sql, jmsg.getMID(), jmsg.getUser().getUID()); + } else { + // new message + subscribedUsers = SubscriptionsQueries.getSubscribedUsers(sql, jmsg.getUser().getUID(), jmsg.getMID()); + } } /*** ANDROID ***/ List<String> regids; if (isPM) { regids = new ArrayList<>(); - String targetId = PushQueries.getAndroidRegID(sql, recipientID); + String targetId = PushQueries.getAndroidRegID(sql, pmTo); if (targetId != null && !targetId.isEmpty()) { regids.add(targetId); } } else { - regids = PushQueries.getAndroidSubscribers(sql, senderID); + List<Integer> uids = subscribedUsers.stream().map(User::getUID).collect(Collectors.toList()); + regids = PushQueries.getAndroidTokens(sql, uids); } if (!regids.isEmpty()) { @@ -199,12 +192,13 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe List<String> urls; if (isPM) { urls = new ArrayList<>(); - String targetURL = PushQueries.getWinPhoneURL(sql, recipientID); + String targetURL = PushQueries.getWinPhoneURL(sql, pmTo); if (!TextUtils.isEmpty(targetURL)) { urls.add(targetURL); } } else { - urls = PushQueries.getWinPhoneSubscribers(sql, senderID); + urls = PushQueries.getWindowsTokens(sql, + subscribedUsers.stream().map(User::getUID).collect(Collectors.toList())); } @@ -245,12 +239,13 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe List<String> tokens; if (isPM) { tokens = new ArrayList<>(); - String targetToken = PushQueries.getAPNSToken(sql, recipientID); + String targetToken = PushQueries.getAPNSToken(sql, pmTo); if (targetToken != null && !targetToken.isEmpty()) { tokens.add(targetToken); } } else { - tokens = PushQueries.getAPNSSubscribers(sql, senderID); + tokens = PushQueries.getAPNSTokens(sql, + subscribedUsers.stream().map(User::getUID).collect(Collectors.toList())); } if (!tokens.isEmpty()) { ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") |