From 8ea2fe0294627ef196282121271cce29f58c0631 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 15 Apr 2020 15:03:57 +1000 Subject: [PATCH] add update token guard and move acknowledgement to signal service --- .../loki/LokiPushNotificationManager.kt | 32 ++++--------------- .../messaging/BackgroundPollWorker.kt | 3 +- .../securesms/util/TextSecurePreferences.java | 19 +++++++++++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt index 8378519412..4858e2c818 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPushNotificationManager.kt @@ -10,7 +10,7 @@ import java.io.IOException object LokiPushNotificationManager { //const val server = "https://live.apns.getsession.org/" const val server = "https://dev.apns.getsession.org/" - const val tokenExpirationInterval = 2 * 24 * 60 * 60 + const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000 private val connection = OkHttpClient() fun disableRemoteNotification(token: String, context: Context?) { @@ -43,6 +43,10 @@ object LokiPushNotificationManager { @JvmStatic fun register(token: String, hexEncodedPublicKey: String, context: Context?) { + if (token == TextSecurePreferences.getTokenForRemoteNotification(context) && System.currentTimeMillis() - TextSecurePreferences.getLastTimeForTokenUploading(context) < tokenExpirationInterval) { + return + } + val parameters = mapOf("token" to token, "pubKey" to hexEncodedPublicKey) val url = "${server}register" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) @@ -57,6 +61,8 @@ object LokiPushNotificationManager { val code = json?.get("code") as? Int if (code != null && code != 0) { TextSecurePreferences.setIsUsingRemoteNotification(context, true) + TextSecurePreferences.setTokenForRemoteNotification(context, token) + TextSecurePreferences.setLastTimeForTokenUploading(context, System.currentTimeMillis()) } else { Log.d("Loki", "Couldn't register device token due to error: ${json?.get("message") as? String}.") } @@ -70,29 +76,5 @@ object LokiPushNotificationManager { }) } - fun acknowledgeDeliveryForMessageWith(hash: String, expiration: Int, hexEncodedPublicKey: String, context: Context?) { - val parameters = mapOf("hash" to hash, "pubKey" to hexEncodedPublicKey, "expiration" to expiration) - val url = "${server}acknowledge_message_delivery" - val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body).build() - connection.newCall(request).enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - when (response.code()) { - 200 -> { - val bodyAsString = response.body()!!.string() - val json = JsonUtil.fromJson(bodyAsString, Map::class.java) - val code = json?.get("code") as? Int - if (code == null || code == 0) { - Log.d("Loki", "Couldn't acknowledge the delivery for message due to error: ${json?.get("message") as? String}.") - } - } - } - } - - override fun onFailure(call: Call, exception: IOException) { - Log.d("Loki", "Couldn't acknowledge the delivery for message with last hash: ${hash}") - } - }) - } } diff --git a/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt b/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt index 53cd42c29e..7205e624de 100644 --- a/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt +++ b/src/org/thoughtcrime/securesms/loki/redesign/messaging/BackgroundPollWorker.kt @@ -6,6 +6,7 @@ import nl.komponents.kovenant.functional.map import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.jobs.PushContentReceiveJob +import org.thoughtcrime.securesms.loki.LokiPushNotificationManager import org.thoughtcrime.securesms.service.PersistentAlarmManagerListener import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope @@ -15,7 +16,7 @@ import java.util.concurrent.TimeUnit class BackgroundPollWorker : PersistentAlarmManagerListener() { companion object { - private val pollInterval = TimeUnit.MINUTES.toMillis(20) + private val pollInterval = TimeUnit.MINUTES.toMillis(2) @JvmStatic fun schedule(context: Context) { diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 62fc6c258e..c1fbd89b25 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -185,7 +185,10 @@ public class TextSecurePreferences { private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode"; + //Session for Push Notification private static final String IS_USING_REMOTE_NOTIFICATION = "pref_is_using_remote_notification"; + private static final String TOKEN_FOR_REMOTE_NOTIFICATION = "pref_token_for_remote_notification"; + private static final String LAST_TIME_FOR_TOKEN_UPLOADING = "pref_last_time_for_token_uploading"; public static boolean isUsingRemoteNotification(Context context) { return getBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, false); @@ -195,6 +198,22 @@ public class TextSecurePreferences { setBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, value); } + public static String getTokenForRemoteNotification(Context context) { + return getStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, ""); + } + + public static void setTokenForRemoteNotification(Context context, String value) { + setStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, value); + } + + public static long getLastTimeForTokenUploading(Context context) { + return getLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, 0); + } + + public static void setLastTimeForTokenUploading(Context context, long value) { + setLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, value); + } + public static boolean isScreenLockEnabled(@NonNull Context context) { return getBooleanPreference(context, SCREEN_LOCK, false); }