From 1e892568afa29ff9c477ef1e39905338b4d84479 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 17 Jul 2020 09:05:37 +1000 Subject: [PATCH] Handle session request expiration --- .../securesms/ApplicationContext.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 51950ccff7..e1a079f16a 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -108,6 +108,7 @@ import org.whispersystems.signalservice.loki.api.shelved.p2p.LokiP2PAPIDelegate; import org.whispersystems.signalservice.loki.database.LokiAPIDatabaseProtocol; import org.whispersystems.signalservice.loki.protocol.mentions.MentionsManager; import org.whispersystems.signalservice.loki.protocol.meta.SessionMetaProtocol; +import org.whispersystems.signalservice.loki.protocol.meta.TTLUtilities; import org.whispersystems.signalservice.loki.protocol.multidevice.DeviceLink; import org.whispersystems.signalservice.loki.protocol.multidevice.MultiDeviceProtocol; import org.whispersystems.signalservice.loki.protocol.sessionmanagement.SessionManagementProtocol; @@ -585,6 +586,17 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc Runtime.getRuntime().exit(0); } + public boolean hasSentSessionRequestExpired(@NotNull String publicKey) { + LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); + Long timestamp = apiDB.getSessionRequestSentTimestamp(publicKey); + if (timestamp != null) { + long expiration = timestamp + TTLUtilities.getTTL(TTLUtilities.MessageType.SessionRequest); + return new Date().getTime() > expiration; + } else { + return false; + } + } + @Override public void sendSessionRequestIfNeeded(@NotNull String publicKey) { // It's never necessary to establish a session with self @@ -597,10 +609,14 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc // Check that we didn't already send a session request LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); boolean hasSentSessionRequest = (apiDB.getSessionRequestSentTimestamp(publicKey) != null); - if (hasSentSessionRequest) { return; } + boolean hasSentSessionRequestExpired = hasSentSessionRequestExpired(publicKey); + if (hasSentSessionRequestExpired) { + apiDB.setSessionRequestSentTimestamp(publicKey, 0); + } + if (hasSentSessionRequest && !hasSentSessionRequestExpired) { return; } // Send the session request long timestamp = new Date().getTime(); - DatabaseFactory.getLokiAPIDatabase(this).setSessionRequestSentTimestamp(publicKey, timestamp); + apiDB.setSessionRequestSentTimestamp(publicKey, timestamp); PushSessionRequestMessageSendJob job = new PushSessionRequestMessageSendJob(publicKey, timestamp); jobManager.add(job); }