aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/juick/PushComponent.java49
1 files changed, 33 insertions, 16 deletions
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<String> regids;
- if (uid_to > 0) {
+ if (isPM) {
regids = new ArrayList<String>();
- 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<String> urls;
- if (uid_to > 0) {
+ if (isPM) {
urls = new ArrayList<String>();
- 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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<toast>"
+ "<visual>"
@@ -242,14 +259,14 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe
/*** iOS ***/
List<String> tokens;
- if (uid_to > 0) {
+ if (isPM) {
tokens = new ArrayList<String>();
- 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")