From d1c10837517c6ea4787c602f2cb3f84bec4a8a6e Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 9 Sep 2019 14:30:02 +1000 Subject: [PATCH] Refactor --- .../securesms/jobs/PushDecryptJob.java | 106 +++++++++--------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index e8b9bba557..b6105c75ed 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -123,7 +123,6 @@ import org.whispersystems.signalservice.api.messages.multidevice.StickerPackOper import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage; import org.whispersystems.signalservice.api.messages.shared.SharedContact; import org.whispersystems.signalservice.api.push.SignalServiceAddress; -import org.whispersystems.signalservice.internal.util.concurrent.SettableFuture; import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher; import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus; import org.whispersystems.signalservice.loki.messaging.LokiServiceMessage; @@ -139,7 +138,6 @@ import java.util.List; import javax.inject.Inject; -import kotlin.Unit; import network.loki.messenger.R; public class PushDecryptJob extends BaseJob implements InjectableType { @@ -742,58 +740,60 @@ public class PushDecryptJob extends BaseJob implements InjectableType { { notifyTypingStoppedFromIncomingMessage(getMessageDestination(content, message), content.getSender(), content.getSenderDevice()); - Optional insertResult; + Optional quote = getValidatedQuote(message.getQuote()); + Optional> sharedContacts = getContacts(message.getSharedContacts()); + Optional> linkPreviews = getLinkPreviews(message.getPreviews(), message.getBody().or("")); + Optional sticker = getStickerAttachment(message.getSticker()); + IncomingMediaMessage mediaMessage = new IncomingMediaMessage(Address.fromExternal(context, content.getSender()), + message.getTimestamp(), -1, + message.getExpiresInSeconds() * 1000L, false, + content.isNeedsReceipt(), + message.getBody(), + message.getGroupInfo(), + message.getAttachments(), + quote, + sharedContacts, + linkPreviews, + sticker); + if (linkPreviews.isPresent()) { + int linkPreviewCount = linkPreviews.get().size(); + if (linkPreviewCount != 0) { + LinkPreviewRepository lpr = new LinkPreviewRepository(context); + final int[] count = { 0 }; + for (LinkPreview linkPreview : linkPreviews.get()) { + lpr.getLinkPreview(context, linkPreview.getUrl(), lp -> Util.runOnMain(() -> { + int c = count[0]; + c = c + 1; + count[0] = c; + if (lp.isPresent() && lp.get().getThumbnail().isPresent()) { + Attachment thumbnail = lp.get().getThumbnail().get(); + linkPreview.thumbnail = Optional.of(thumbnail); + } + if (c == linkPreviewCount) { + try { + handleMediaMessage(content, mediaMessage, smsMessageId); + } catch (Exception e) { + // Do nothing + } + } + })); + } + } else { + handleMediaMessage(content, mediaMessage, smsMessageId); + } + } else { + handleMediaMessage(content, mediaMessage, smsMessageId); + } + } + + private void handleMediaMessage(@NonNull SignalServiceContent content, @NonNull IncomingMediaMessage mediaMessage, @NonNull Optional smsMessageID) throws StorageFailedException { MmsDatabase database = DatabaseFactory.getMmsDatabase(context); database.beginTransaction(); + Optional insertResult; + try { - Optional quote = getValidatedQuote(message.getQuote()); - Optional> sharedContacts = getContacts(message.getSharedContacts()); - Optional> linkPreviews = getLinkPreviews(message.getPreviews(), message.getBody().or("")); - Optional sticker = getStickerAttachment(message.getSticker()); - IncomingMediaMessage mediaMessage = new IncomingMediaMessage(Address.fromExternal(context, content.getSender()), - message.getTimestamp(), -1, - message.getExpiresInSeconds() * 1000L, false, - content.isNeedsReceipt(), - message.getBody(), - message.getGroupInfo(), - message.getAttachments(), - quote, - sharedContacts, - linkPreviews, - sticker); - - if (linkPreviews.isPresent()) { - int linkPreviewCount = linkPreviews.get().size(); - if (linkPreviewCount != 0) { - final SettableFuture[] future = { new SettableFuture() }; - LinkPreviewRepository lpr = new LinkPreviewRepository(context); - final int[] count = { 0 }; - for (LinkPreview linkPreview : linkPreviews.get()) { - lpr.getLinkPreview(context, linkPreview.getUrl(), lp -> Util.runOnMain(() -> { - int c = count[0]; - c = c + 1; - count[0] = c; - if (lp.isPresent() && lp.get().getThumbnail().isPresent()) { - Attachment thumbnail = lp.get().getThumbnail().get(); - linkPreview.thumbnail = Optional.of(thumbnail); - } - if (c == linkPreviewCount) { - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; - f.set(Unit.INSTANCE); - } - })); - } - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; - try { - f.get(); - } catch (Exception e) { - // Do nothing - } - } - } - insertResult = database.insertSecureDecryptedMessageInbox(mediaMessage, -1); if (insertResult.isPresent()) { @@ -804,13 +804,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType { forceStickerDownloadIfNecessary(stickerAttachments); for (DatabaseAttachment attachment : attachments) { - ApplicationContext.getInstance(context) - .getJobManager() - .add(new AttachmentDownloadJob(insertResult.get().getMessageId(), attachment.getAttachmentId(), false)); + ApplicationContext.getInstance(context).getJobManager().add(new AttachmentDownloadJob(insertResult.get().getMessageId(), attachment.getAttachmentId(), false)); } - if (smsMessageId.isPresent()) { - DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get()); + if (smsMessageID.isPresent()) { + DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageID.get()); } database.setTransactionSuccessful();