Skip to content

Commit

Permalink
Chunk read sync messages.
Browse files Browse the repository at this point in the history
Same thing we do with read receipts we send to other people. Just missed
this part.
  • Loading branch information
greyson-signal committed Nov 30, 2020
1 parent cce8cdc commit 105862b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
import org.thoughtcrime.securesms.logging.Log;

Expand All @@ -18,6 +19,7 @@
import org.thoughtcrime.securesms.recipients.RecipientUtil;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
Expand All @@ -40,13 +42,13 @@ public class MultiDeviceReadUpdateJob extends BaseJob {

private List<SerializableSyncMessageId> messageIds;

public MultiDeviceReadUpdateJob(List<SyncMessageId> messageIds) {
private MultiDeviceReadUpdateJob(List<SyncMessageId> messageIds) {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setLifespan(TimeUnit.DAYS.toMillis(1))
.setMaxAttempts(Parameters.UNLIMITED)
.build(),
messageIds);
SendReadReceiptJob.ensureSize(messageIds, SendReadReceiptJob.MAX_TIMESTAMPS));
}

private MultiDeviceReadUpdateJob(@NonNull Job.Parameters parameters, @NonNull List<SyncMessageId> messageIds) {
Expand All @@ -59,6 +61,23 @@ private MultiDeviceReadUpdateJob(@NonNull Job.Parameters parameters, @NonNull Li
}
}

/**
* Enqueues all the necessary jobs for read receipts, ensuring that they're all within the
* maximum size.
*/
public static void enqueue(@NonNull List<SyncMessageId> messageIds) {
JobManager jobManager = ApplicationDependencies.getJobManager();
List<List<SyncMessageId>> messageIdChunks = Util.chunk(messageIds, SendReadReceiptJob.MAX_TIMESTAMPS);

if (messageIdChunks.size() > 1) {
Log.w(TAG, "Large receipt count! Had to break into multiple chunks. Total count: " + messageIds.size());
}

for (List<SyncMessageId> chunk : messageIdChunks) {
jobManager.add(new MultiDeviceReadUpdateJob(chunk));
}
}

@Override
public @NonNull Data serialize() {
String[] ids = new String[messageIds.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SendReadReceiptJob extends BaseJob {

private static final String TAG = SendReadReceiptJob.class.getSimpleName();

private static final int MAX_TIMESTAMPS = 500;
static final int MAX_TIMESTAMPS = 500;

private static final String KEY_THREAD = "thread";
private static final String KEY_ADDRESS = "address";
Expand Down Expand Up @@ -150,7 +150,7 @@ public void onFailure() {
Log.w(TAG, "Failed to send read receipts to: " + recipientId);
}

private static <E> List<E> ensureSize(@NonNull List<E> list, int maxSize) {
static <E> List<E> ensureSize(@NonNull List<E> list, int maxSize) {
if (list.size() > maxSize) {
throw new IllegalArgumentException("Too large! Size: " + list.size() + ", maxSize: " + maxSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void process(@NonNull Context context, @NonNull List<MarkedMessage

scheduleDeletion(context, smsExpirationInfo, mmsExpirationInfo);

ApplicationDependencies.getJobManager().add(new MultiDeviceReadUpdateJob(syncMessageIds));
MultiDeviceReadUpdateJob.enqueue(syncMessageIds);

Map<Long, List<MarkedMessageInfo>> threadToInfo = Stream.of(markedReadMessages)
.collect(Collectors.groupingBy(MarkedMessageInfo::getThreadId));
Expand Down

0 comments on commit 105862b

Please sign in to comment.