Skip to content

Commit

Permalink
Debounce thread updates for incoming messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Apr 12, 2023
1 parent 6d4906d commit 71e2b82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.groups.GroupMigrationMembershipChange
import org.thoughtcrime.securesms.insights.InsightsConstants
import org.thoughtcrime.securesms.jobs.OptimizeMessageSearchIndexJob
import org.thoughtcrime.securesms.jobs.ThreadUpdateJob
import org.thoughtcrime.securesms.jobs.TrimThreadJob
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.linkpreview.LinkPreview
Expand Down Expand Up @@ -1058,7 +1059,7 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
}

if (!silent) {
threads.update(threadId, true)
ThreadUpdateJob.enqueue(threadId)
TrimThreadJob.enqueueAsync(threadId)
}

Expand Down Expand Up @@ -2467,7 +2468,7 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
if (!MessageTypes.isPaymentsActivated(mailbox) && !MessageTypes.isPaymentsRequestToActivate(mailbox) && !MessageTypes.isExpirationTimerUpdate(mailbox) && !retrieved.storyType.isStory && isNotStoryGroupReply) {
val incrementUnreadMentions = retrieved.mentions.isNotEmpty() && retrieved.mentions.any { it.recipientId == Recipient.self().id }
threads.incrementUnread(threadId, 1, if (incrementUnreadMentions) 1 else 0)
threads.update(threadId, true)
ThreadUpdateJob.enqueue(threadId)
}

notifyConversationListeners(threadId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
import org.thoughtcrime.securesms.jobmanager.JsonJobData;
import org.thoughtcrime.securesms.jobmanager.Job;

/**
* A job that effectively debounces thread updates through a combination of having a max instance count
* and sleeping at the end of the job to make sure it takes a minimum amount of time.
*/
public final class ThreadUpdateJob extends BaseJob {

public static final String KEY = "ThreadUpdateJob";

private static final String KEY_THREAD_ID = "thread_id";

private static final long DEBOUNCE_INTERVAL = 3000;

private final long threadId;

private ThreadUpdateJob(long threadId) {
Expand Down Expand Up @@ -47,7 +53,7 @@ public static void enqueue(long threadId) {
@Override
protected void onRun() throws Exception {
SignalDatabase.threads().update(threadId, true);
ThreadUtil.sleep(1000);
ThreadUtil.sleep(DEBOUNCE_INTERVAL);
}

@Override
Expand Down

0 comments on commit 71e2b82

Please sign in to comment.