From 48adb63fb719aa4ddd19d9ffdef26ff2fb253ad7 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 7 Dec 2020 09:02:48 +1100 Subject: [PATCH] Refresh snode pool at least once a day Changes from cfb7ff529 --- .../securesms/loki/api/ClosedGroupPoller.kt | 2 +- .../securesms/loki/database/LokiAPIDatabase.kt | 12 ++++++++++++ .../securesms/util/TextSecurePreferences.java | 9 +++++++++ .../session/libsignal/service/loki/api/SwarmAPI.kt | 8 +++++++- .../service/loki/database/LokiAPIDatabaseProtocol.kt | 4 ++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/ClosedGroupPoller.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/ClosedGroupPoller.kt index 16f4a46162..3e2834e612 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/ClosedGroupPoller.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/ClosedGroupPoller.kt @@ -29,7 +29,7 @@ class ClosedGroupPoller private constructor(private val context: Context, privat // region Settings companion object { - private val pollInterval: Long = 2 * 1000 + private val pollInterval: Long = 4 * 1000 public lateinit var shared: ClosedGroupPoller diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt index 61f7b82985..e0e47ec82a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt @@ -9,6 +9,8 @@ import org.thoughtcrime.securesms.loki.utilities.* import org.session.libsignal.service.loki.api.Snode import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink +import org.thoughtcrime.securesms.util.TextSecurePreferences +import java.util.* class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiAPIDatabaseProtocol { @@ -375,6 +377,16 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( return database.delete(openGroupProfilePictureTable, "$publicChatID = ?", arrayOf(index)) > 0 } + override fun getLastSnodePoolRefreshDate(): Date? { + val time = TextSecurePreferences.getLastSnodePoolRefreshDate(context) + if (time <= 0) { return null } + return Date(time) + } + + override fun setLastSnodePoolRefreshDate(date: Date) { + TextSecurePreferences.setLastSnodePoolRefreshDate(context, date) + } + // region Deprecated override fun getDeviceLinks(publicKey: String): Set { return setOf() diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 2c6e639471..b6d87eab98 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.security.SecureRandom; import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -1237,6 +1238,14 @@ public class TextSecurePreferences { public static void setHasSeenLightThemeIntroSheet(Context context) { setBooleanPreference(context, "has_seen_light_theme_intro_sheet", true); } + + public static long getLastSnodePoolRefreshDate(Context context) { + return getLongPreference(context, "last_snode_pool_refresh_date", 0); + } + + public static void setLastSnodePoolRefreshDate(Context context, Date date) { + setLongPreference(context, "last_snode_pool_refresh_date", date.getTime()); + } // endregion // region Backup related diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/SwarmAPI.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/SwarmAPI.kt index 9159fa6aed..43a10108a8 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/SwarmAPI.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/api/SwarmAPI.kt @@ -11,6 +11,7 @@ import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.utilities.getRandomElement import org.session.libsignal.service.loki.utilities.prettifiedDescription import java.security.SecureRandom +import java.util.* class SwarmAPI private constructor(private val database: LokiAPIDatabaseProtocol) { internal var snodeFailureCount: MutableMap = mutableMapOf() @@ -46,7 +47,12 @@ class SwarmAPI private constructor(private val database: LokiAPIDatabaseProtocol // region Swarm API internal fun getRandomSnode(): Promise { val snodePool = this.snodePool - if (snodePool.count() < minimumSnodePoolCount) { + val lastRefreshDate = database.getLastSnodePoolRefreshDate() + val now = Date() + val needsRefresh = (snodePool.count() < minimumSnodePoolCount) || lastRefreshDate == null || (now.time - lastRefreshDate.time) > 24 * 60 * 60 * 1000 + if (needsRefresh) { + database.setLastSnodePoolRefreshDate(now) + val target = seedNodePool.random() val url = "$target/json_rpc" Log.d("Loki", "Populating snode pool using: $target.") diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt index 4fce637932..63ce0da095 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt @@ -2,6 +2,7 @@ package org.session.libsignal.service.loki.database import org.session.libsignal.service.loki.api.Snode import org.session.libsignal.service.loki.protocol.shelved.multidevice.DeviceLink +import java.util.* interface LokiAPIDatabaseProtocol { @@ -31,6 +32,9 @@ interface LokiAPIDatabaseProtocol { fun setOpenGroupPublicKey(server: String, newValue: String) fun setOpenGroupProfilePictureURL(group: Long, server: String, newValue: String) fun getOpenGroupProfilePictureURL(group: Long, server: String): String? + fun getLastSnodePoolRefreshDate(): Date? + fun setLastSnodePoolRefreshDate(newValue: Date) + // region Deprecated fun getDeviceLinks(publicKey: String): Set