From 1d5a31aae48a16bc00fffcaf745259c42345ed8c Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 9 Sep 2019 13:53:23 +1000 Subject: [PATCH] Implement link previews for regular conversations --- .../securesms/jobs/PushDecryptJob.java | 35 ++++++++++++++++++- .../securesms/jobs/PushMediaSendJob.java | 2 +- .../securesms/linkpreview/LinkPreview.java | 2 +- .../linkpreview/LinkPreviewRepository.java | 4 +-- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 55fa598cd1..e8b9bba557 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -58,14 +58,15 @@ import org.thoughtcrime.securesms.database.StickerDatabase; import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.MmsMessageRecord; -import org.thoughtcrime.securesms.dependencies.InjectableType; import org.thoughtcrime.securesms.database.model.StickerRecord; +import org.thoughtcrime.securesms.dependencies.InjectableType; import org.thoughtcrime.securesms.groups.GroupMessageProcessor; import org.thoughtcrime.securesms.jobmanager.Data; import org.thoughtcrime.securesms.jobmanager.Job; import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.linkpreview.Link; import org.thoughtcrime.securesms.linkpreview.LinkPreview; +import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository; import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.loki.LokiMessageDatabase; @@ -96,6 +97,7 @@ import org.thoughtcrime.securesms.util.Hex; import org.thoughtcrime.securesms.util.IdentityUtil; import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.Util; import org.whispersystems.libsignal.state.PreKeyBundle; import org.whispersystems.libsignal.state.SignalProtocolStore; import org.whispersystems.libsignal.util.guava.Optional; @@ -121,6 +123,7 @@ 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; @@ -136,6 +139,7 @@ import java.util.List; import javax.inject.Inject; +import kotlin.Unit; import network.loki.messenger.R; public class PushDecryptJob extends BaseJob implements InjectableType { @@ -760,6 +764,35 @@ public class PushDecryptJob extends BaseJob implements InjectableType { 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); diff --git a/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java index 9558af26a7..ab0a497b72 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java @@ -77,7 +77,7 @@ public class PushMediaSendJob extends PushSendJob implements InjectableType { List attachments = new LinkedList<>(); 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.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 attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId())).toList(); diff --git a/src/org/thoughtcrime/securesms/linkpreview/LinkPreview.java b/src/org/thoughtcrime/securesms/linkpreview/LinkPreview.java index 00368652d4..35b3634c78 100644 --- a/src/org/thoughtcrime/securesms/linkpreview/LinkPreview.java +++ b/src/org/thoughtcrime/securesms/linkpreview/LinkPreview.java @@ -26,7 +26,7 @@ public class LinkPreview { private final AttachmentId attachmentId; @JsonIgnore - private final Optional thumbnail; + public Optional thumbnail; public LinkPreview(@NonNull String url, @NonNull String title, @NonNull DatabaseAttachment thumbnail) { this.url = url; diff --git a/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewRepository.java b/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewRepository.java index 1e9fff7f65..324ec905c0 100644 --- a/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewRepository.java +++ b/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewRepository.java @@ -71,7 +71,7 @@ public class LinkPreviewRepository implements InjectableType { ApplicationContext.getInstance(context).injectDependencies(this); } - RequestController getLinkPreview(@NonNull Context context, @NonNull String url, @NonNull Callback> callback) { + public RequestController getLinkPreview(@NonNull Context context, @NonNull String url, @NonNull Callback> callback) { CompositeRequestController compositeController = new CompositeRequestController(); if (!LinkPreviewUtil.isWhitelistedLinkUrl(url)) { @@ -293,7 +293,7 @@ public class LinkPreviewRepository implements InjectableType { } } - interface Callback { + public interface Callback { void onComplete(@NonNull T result); } }