From 3977558af5ec48fd84ecb79d3acad3f13763e154 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 30 Dec 2015 13:58:27 +0300 Subject: fix push flow, WIP --- src/main/java/com/juick/PushComponent.java | 49 ++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index 0f806827..2102f77d 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -23,6 +23,7 @@ import com.google.android.gcm.server.Result; import com.google.android.gcm.server.Sender; import com.juick.json.MessageSerializer; import com.juick.server.PushQueries; +import com.juick.server.MessagesQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message.MessageListener; import com.juick.xmpp.utils.XmlUtils; @@ -163,19 +164,40 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe if (jmsg == null) { return; } + logger.info("Message to push: " + msg.toString()); - int uid_to = jmsg.User.UID; + boolean isPM = jmsg.MID == 0; + boolean isReply = jmsg.RID > 0; + int senderID = 0, recipientID = 0; + // reply pushed to original sender + if (isReply) { + com.juick.Message op = MessagesQueries.getMessage(sql, jmsg.MID); + senderID = op.User.UID; + // do not notify self + if (jmsg.User.UID == op.User.UID) { + return; + } + } + if (isPM) { + // PM + try { + recipientID = Integer.parseInt(msg.to.Username); + } catch (NumberFormatException e) { + logger.info("Wrong PM recipient: " + msg.to.Username); + return; + } + } /*** ANDROID ***/ ArrayList regids; - if (uid_to > 0) { + if (isPM) { regids = new ArrayList(); - String targetId = PushQueries.getAndroidRegID(sql, uid_to); + String targetId = PushQueries.getAndroidRegID(sql, recipientID); if (targetId != null && !targetId.isEmpty()) { regids.add(targetId); } } else { - regids = PushQueries.getAndroidSubscribers(sql, jmsg.User.UID); + regids = PushQueries.getAndroidSubscribers(sql, senderID); } if (!regids.isEmpty()) { @@ -198,14 +220,14 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe /*** WinPhone ***/ ArrayList urls; - if (uid_to > 0) { + if (isPM) { urls = new ArrayList(); - String targetURL = PushQueries.getWinPhoneURL(sql, uid_to); + String targetURL = PushQueries.getWinPhoneURL(sql, recipientID); if (targetURL != null && !targetURL.isEmpty()) { urls.add(targetURL); } } else { - urls = PushQueries.getWinPhoneSubscribers(sql, jmsg.User.UID); + urls = PushQueries.getWinPhoneSubscribers(sql, senderID); } if (!urls.isEmpty()) { @@ -213,12 +235,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe if (!jmsg.Tags.isEmpty()) { text1 += ":" + XmlUtils.escape(jmsg.getTagsString()); } - String text2; - if (jmsg.Text.length() > 250) { - text2 = XmlUtils.escape(jmsg.Text.substring(0, 255)) + "..."; - } else { - text2 = XmlUtils.escape(jmsg.Text); - } + String text2 = XmlUtils.escape(jmsg.Text); String xml = "" + "" + "" @@ -242,14 +259,14 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe /*** iOS ***/ List tokens; - if (uid_to > 0) { + if (isPM) { tokens = new ArrayList(); - String targetToken = PushQueries.getAPNSToken(sql, uid_to); + String targetToken = PushQueries.getAPNSToken(sql, recipientID); if (targetToken != null && !targetToken.isEmpty()) { tokens.add(targetToken); } } else { - tokens = PushQueries.getAPNSSubscribers(sql, jmsg.User.UID); + tokens = PushQueries.getAPNSSubscribers(sql, senderID); } if (!tokens.isEmpty()) { ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") -- cgit v1.2.3