From f13b5ab9d719244e10ee102072a1948f4cf0c170 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 19 May 2021 17:01:53 +1000 Subject: [PATCH 1/3] fix open group spam --- .../securesms/ApplicationContext.java | 6 ++-- .../securesms/loki/api/OpenGroupManager.kt | 16 +++++++++ .../OptimizedMessageNotifier.java | 33 +++---------------- .../pollers/OpenGroupPollerV2.kt | 2 ++ 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index afcfff4a21..9cc136153a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -206,10 +206,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc poller.setCaughtUp(false); } startPollingIfNeeded(); - // FIXME: Open group handling - /* - publicChatManager.markAllAsNotCaughtUp(); - */ + + OpenGroupManager.INSTANCE.markAllAsNotCaughtUp(); OpenGroupManager.INSTANCE.startPolling(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt index 62aca11265..c682a89557 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt @@ -19,6 +19,22 @@ object OpenGroupManager { private var pollers = mutableMapOf() // One for each server private var isPolling = false + val isAllCaughtUp: Boolean + get() { + pollers.values.forEach { poller -> + if (!poller.isCaughtUp) { + return false + } + } + return true + } + + fun markAllAsNotCaughtUp() { + pollers.values.forEach { poller -> + poller.isCaughtUp = false + } + } + fun startPolling() { if (isPolling) { return } isPolling = true diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java index 46b4d836d7..448d977da9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java @@ -12,6 +12,7 @@ import org.session.libsession.utilities.recipients.Recipient; import org.session.libsession.utilities.Debouncer; import org.session.libsignal.utilities.ThreadUtils; import org.thoughtcrime.securesms.ApplicationContext; +import org.thoughtcrime.securesms.loki.api.OpenGroupManager; import java.util.concurrent.TimeUnit; @@ -42,18 +43,12 @@ public class OptimizedMessageNotifier implements MessageNotifier { @Override public void updateNotification(@NonNull Context context) { Poller poller = ApplicationContext.getInstance(context).poller; - // FIXME: Open group handling boolean isCaughtUp = true; if (poller != null) { isCaughtUp = isCaughtUp && poller.isCaughtUp(); } - // FIXME: Open group handling - /* - if (publicChatManager != null) { - isCaughtUp = isCaughtUp && publicChatManager.areAllCaughtUp(); - } - */ + isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp(); if (isCaughtUp) { performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context)); @@ -65,18 +60,12 @@ public class OptimizedMessageNotifier implements MessageNotifier { @Override public void updateNotification(@NonNull Context context, long threadId) { Poller lokiPoller = ApplicationContext.getInstance(context).poller; - // FIXME: Open group handling boolean isCaughtUp = true; if (lokiPoller != null) { isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp(); } - // FIXME: Open group handling - /* - if (publicChatManager != null) { - isCaughtUp = isCaughtUp && publicChatManager.areAllCaughtUp(); - } - */ + isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp(); if (isCaughtUp) { performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId)); @@ -88,18 +77,12 @@ public class OptimizedMessageNotifier implements MessageNotifier { @Override public void updateNotification(@NonNull Context context, long threadId, boolean signal) { Poller lokiPoller = ApplicationContext.getInstance(context).poller; - // FIXME: Open group handling boolean isCaughtUp = true; if (lokiPoller != null) { isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp(); } - // FIXME: Open group handling - /* - if (publicChatManager != null) { - isCaughtUp = isCaughtUp && publicChatManager.areAllCaughtUp(); - } - */ + isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp(); if (isCaughtUp) { performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal)); @@ -111,18 +94,12 @@ public class OptimizedMessageNotifier implements MessageNotifier { @Override public void updateNotification(@androidx.annotation.NonNull Context context, boolean signal, int reminderCount) { Poller lokiPoller = ApplicationContext.getInstance(context).poller; - // FIXME: Open group handling boolean isCaughtUp = true; if (lokiPoller != null) { isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp(); } - // FIXME: Open group handling - /* - if (publicChatManager != null) { - isCaughtUp = isCaughtUp && publicChatManager.areAllCaughtUp(); - } - */ + isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp(); if (isCaughtUp) { performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount)); diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt index 320c3afd97..70e1ea903e 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt @@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit class OpenGroupPollerV2(private val server: String, private val executorService: ScheduledExecutorService?) { var hasStarted = false + var isCaughtUp = false private var future: ScheduledFuture<*>? = null companion object { @@ -44,6 +45,7 @@ class OpenGroupPollerV2(private val server: String, private val executorService: handleNewMessages(openGroupID, response.messages, isBackgroundPoll) handleDeletedMessages(openGroupID, response.deletions) } + isCaughtUp = true }.always { executorService?.schedule(this@OpenGroupPollerV2::poll, OpenGroupPollerV2.pollInterval, TimeUnit.MILLISECONDS) }.map { } From b2827184a8a6a348901a79434248170db8310173 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 20 May 2021 14:43:22 +1000 Subject: [PATCH 2/3] extend the debouncer timeout --- .../securesms/notifications/OptimizedMessageNotifier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java index 448d977da9..72bddb4bbb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java @@ -23,7 +23,7 @@ public class OptimizedMessageNotifier implements MessageNotifier { @MainThread public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) { this.wrapped = wrapped; - this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(1)); + this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(2)); } @Override From 6822bc461c53377f8e772ec3c376b6398935ce1b Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 20 May 2021 15:41:17 +1000 Subject: [PATCH 3/3] new approach for batch notification --- .../securesms/ApplicationContext.java | 2 +- .../securesms/loki/api/OpenGroupManager.kt | 16 +--------------- .../notifications/DefaultMessageNotifier.java | 4 ++++ .../notifications/OptimizedMessageNotifier.java | 2 +- .../pollers/OpenGroupPollerV2.kt | 2 -- 5 files changed, 7 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 9cc136153a..b2b18f0d5d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -207,7 +207,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc } startPollingIfNeeded(); - OpenGroupManager.INSTANCE.markAllAsNotCaughtUp(); + OpenGroupManager.INSTANCE.setAllCaughtUp(false); OpenGroupManager.INSTANCE.startPolling(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt index c682a89557..20bc37e553 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/api/OpenGroupManager.kt @@ -19,21 +19,7 @@ object OpenGroupManager { private var pollers = mutableMapOf() // One for each server private var isPolling = false - val isAllCaughtUp: Boolean - get() { - pollers.values.forEach { poller -> - if (!poller.isCaughtUp) { - return false - } - } - return true - } - - fun markAllAsNotCaughtUp() { - pollers.values.forEach { poller -> - poller.isCaughtUp = false - } - } + var isAllCaughtUp = false fun startPolling() { if (isPolling) { return } diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/DefaultMessageNotifier.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/DefaultMessageNotifier.java index d5ab94c1a0..55dd3f17a2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/DefaultMessageNotifier.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/DefaultMessageNotifier.java @@ -56,6 +56,7 @@ import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord; import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.MmsMessageRecord; +import org.thoughtcrime.securesms.loki.api.OpenGroupManager; import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol; import org.thoughtcrime.securesms.loki.utilities.MentionUtilities; import org.thoughtcrime.securesms.mms.SlideDeck; @@ -286,6 +287,9 @@ public class DefaultMessageNotifier implements MessageNotifier { } finally { if (telcoCursor != null) telcoCursor.close(); if (pushCursor != null) pushCursor.close(); + if (!OpenGroupManager.INSTANCE.isAllCaughtUp()) { + OpenGroupManager.INSTANCE.setAllCaughtUp(true); + } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java index 72bddb4bbb..448d977da9 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java @@ -23,7 +23,7 @@ public class OptimizedMessageNotifier implements MessageNotifier { @MainThread public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) { this.wrapped = wrapped; - this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(2)); + this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(1)); } @Override diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt index 70e1ea903e..320c3afd97 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/OpenGroupPollerV2.kt @@ -18,7 +18,6 @@ import java.util.concurrent.TimeUnit class OpenGroupPollerV2(private val server: String, private val executorService: ScheduledExecutorService?) { var hasStarted = false - var isCaughtUp = false private var future: ScheduledFuture<*>? = null companion object { @@ -45,7 +44,6 @@ class OpenGroupPollerV2(private val server: String, private val executorService: handleNewMessages(openGroupID, response.messages, isBackgroundPoll) handleDeletedMessages(openGroupID, response.deletions) } - isCaughtUp = true }.always { executorService?.schedule(this@OpenGroupPollerV2::poll, OpenGroupPollerV2.pollInterval, TimeUnit.MILLISECONDS) }.map { }