From aac9725adc63b295d20be38032f1e206490d291a Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Mon, 12 Aug 2019 15:04:26 -0400 Subject: [PATCH] Move shared attachment job generation code to parent class. --- .../securesms/jobs/PushGroupSendJob.java | 26 ++++------------ .../securesms/jobs/PushMediaSendJob.java | 26 ++++------------ .../securesms/jobs/PushSendJob.java | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java index 7dab39546..989a39b59 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java @@ -10,7 +10,6 @@ import com.annimon.stream.Stream; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.attachments.Attachment; -import org.thoughtcrime.securesms.attachments.DatabaseAttachment; import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; @@ -48,7 +47,6 @@ import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupC import java.io.IOException; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -86,26 +84,12 @@ public class PushGroupSendJob extends PushSendJob { @WorkerThread public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId, @NonNull Address destination, @Nullable Address filterAddress) { try { - MmsDatabase database = DatabaseFactory.getMmsDatabase(context); - OutgoingMediaMessage message = database.getOutgoingMessage(messageId); - List attachments = new LinkedList<>(); + MmsDatabase database = DatabaseFactory.getMmsDatabase(context); + OutgoingMediaMessage message = database.getOutgoingMessage(messageId); + JobManager.Chain compressAndUploadAttachment = createCompressingAndUploadAttachmentsChain(jobManager, message); - attachments.addAll(message.getAttachments()); - attachments.addAll(Stream.of(message.getLinkPreviews()).filter(p -> p.getThumbnail().isPresent()).map(p -> p.getThumbnail().get()).toList()); - attachments.addAll(Stream.of(message.getSharedContacts()).filter(c -> c.getAvatar() != null).map(c -> c.getAvatar().getAttachment()).withoutNulls().toList()); - - List compressionJobs = Stream.of(attachments).map(a -> AttachmentCompressionJob.fromAttachment((DatabaseAttachment) a, false, -1)).toList(); - - List attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId())).toList(); - - if (attachmentJobs.isEmpty()) { - jobManager.add(new PushGroupSendJob(messageId, destination, filterAddress)); - } else { - jobManager.startChain(compressionJobs) - .then(attachmentJobs) - .then(new PushGroupSendJob(messageId, destination, filterAddress)) - .enqueue(); - } + compressAndUploadAttachment.then(new PushGroupSendJob(messageId, destination, filterAddress)) + .enqueue(); } catch (NoSuchMessageException | MmsException e) { Log.w(TAG, "Failed to enqueue message.", e); diff --git a/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java index 0c1a9ec0a..bbaeae013 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java @@ -9,7 +9,6 @@ import com.annimon.stream.Stream; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.attachments.Attachment; -import org.thoughtcrime.securesms.attachments.DatabaseAttachment; import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; @@ -44,7 +43,6 @@ import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserExce import java.io.FileNotFoundException; import java.io.IOException; -import java.util.LinkedList; import java.util.List; public class PushMediaSendJob extends PushSendJob { @@ -73,26 +71,12 @@ public class PushMediaSendJob extends PushSendJob { throw new AssertionError(); } - MmsDatabase database = DatabaseFactory.getMmsDatabase(context); - OutgoingMediaMessage message = database.getOutgoingMessage(messageId); - List attachments = new LinkedList<>(); + MmsDatabase database = DatabaseFactory.getMmsDatabase(context); + OutgoingMediaMessage message = database.getOutgoingMessage(messageId); + JobManager.Chain compressAndUploadAttachment = createCompressingAndUploadAttachmentsChain(jobManager, message); - attachments.addAll(message.getAttachments()); - attachments.addAll(Stream.of(message.getLinkPreviews()).filter(p -> p.getThumbnail().isPresent()).map(p -> p.getThumbnail().get()).toList()); - attachments.addAll(Stream.of(message.getSharedContacts()).filter(c -> c.getAvatar() != null).map(c -> c.getAvatar().getAttachment()).withoutNulls().toList()); - - List compressionJobs = Stream.of(attachments).map(a -> AttachmentCompressionJob.fromAttachment((DatabaseAttachment) a, false, -1)).toList(); - - List attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId())).toList(); - - if (attachmentJobs.isEmpty()) { - jobManager.add(new PushMediaSendJob(messageId, destination)); - } else { - jobManager.startChain(compressionJobs) - .then(attachmentJobs) - .then(new PushMediaSendJob(messageId, destination)) - .enqueue(); - } + compressAndUploadAttachment.then(new PushMediaSendJob(messageId, destination)) + .enqueue(); } catch (NoSuchMessageException | MmsException e) { Log.w(TAG, "Failed to enqueue message.", e); diff --git a/src/org/thoughtcrime/securesms/jobs/PushSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushSendJob.java index 28d9ac10a..3cb4a9597 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushSendJob.java @@ -14,16 +14,22 @@ import org.signal.libsignal.metadata.certificate.SenderCertificate; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.TextSecureExpiredException; import org.thoughtcrime.securesms.attachments.Attachment; +import org.thoughtcrime.securesms.attachments.DatabaseAttachment; import org.thoughtcrime.securesms.contactshare.Contact; import org.thoughtcrime.securesms.contactshare.ContactModelMapper; import org.thoughtcrime.securesms.crypto.ProfileKeyUtil; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; +import org.thoughtcrime.securesms.database.MmsDatabase; +import org.thoughtcrime.securesms.database.NoSuchMessageException; import org.thoughtcrime.securesms.events.PartProgressEvent; import org.thoughtcrime.securesms.jobmanager.Job; +import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint; +import org.thoughtcrime.securesms.linkpreview.LinkPreview; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader; +import org.thoughtcrime.securesms.mms.MmsException; import org.thoughtcrime.securesms.mms.OutgoingMediaMessage; import org.thoughtcrime.securesms.mms.PartAuthority; import org.thoughtcrime.securesms.notifications.MessageNotifier; @@ -143,6 +149,30 @@ public abstract class PushSendJob extends SendJob { return null; } + protected static JobManager.Chain createCompressingAndUploadAttachmentsChain(@NonNull JobManager jobManager, OutgoingMediaMessage message) { + List attachments = new LinkedList<>(); + + attachments.addAll(message.getAttachments()); + + attachments.addAll(Stream.of(message.getLinkPreviews()) + .map(LinkPreview::getThumbnail) + .filter(Optional::isPresent) + .map(Optional::get) + .toList()); + + attachments.addAll(Stream.of(message.getSharedContacts()) + .map(Contact::getAvatar).withoutNulls() + .map(Contact.Avatar::getAttachment).withoutNulls() + .toList()); + + List compressionJobs = Stream.of(attachments).map(a -> AttachmentCompressionJob.fromAttachment((DatabaseAttachment) a, false, -1)).toList(); + + List attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId())).toList(); + + return jobManager.startChain(compressionJobs) + .then(attachmentJobs); + } + protected @NonNull List getAttachmentPointersFor(List attachments) { return Stream.of(attachments).map(this::getAttachmentPointerFor).filter(a -> a != null).toList(); }