Skip to content

Commit

Permalink
Fix race condition that could show an empty link preview after send.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Aug 5, 2021
1 parent 80e1b2c commit 2bac1a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,6 @@ protected void sendComplete(long threadId) {
attachmentManager.cleanup();

updateLinkPreviewState();
linkPreviewViewModel.onSend();
}

private void sendMessage() {
Expand Down Expand Up @@ -2939,13 +2938,14 @@ private void sendMediaMessage(final boolean forceSms, final long expiresIn, fina
throws InvalidMessageException
{
Log.i(TAG, "Sending media message...");
List<LinkPreview> linkPreviews = linkPreviewViewModel.onSend();
sendMediaMessage(recipient.getId(),
forceSms,
getMessage(),
attachmentManager.buildSlideDeck(),
inputPanel.getQuote().orNull(),
Collections.emptyList(),
linkPreviewViewModel.getActiveLinkPreviews(),
linkPreviews,
composeText.getMentions(),
expiresIn,
viewOnce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,27 @@ public boolean hasLinkPreviewUi() {
return linkPreviewSafeState.getValue() != null && linkPreviewSafeState.getValue().hasContent();
}

public @NonNull List<LinkPreview> getActiveLinkPreviews() {
final LinkPreviewState state = linkPreviewSafeState.getValue();
/**
* Gets the current state for use in the UI, then resets local state to prepare for the next message send.
*/
public @NonNull List<LinkPreview> onSend() {
final LinkPreviewState currentState = linkPreviewSafeState.getValue();

if (state == null || !state.getLinkPreview().isPresent()) {
if (activeRequest != null) {
activeRequest.cancel();
activeRequest = null;
}

userCanceled = false;
activeUrl = null;

debouncer.clear();
linkPreviewState.setValue(LinkPreviewState.forNoLinks());

if (currentState == null || !currentState.getLinkPreview().isPresent()) {
return Collections.emptyList();
} else {
return Collections.singletonList(state.getLinkPreview().get());
return Collections.singletonList(currentState.getLinkPreview().get());
}
}

Expand Down Expand Up @@ -115,7 +129,11 @@ public void onSuccess(@NonNull LinkPreview linkPreview) {
public void onError(@NonNull LinkPreviewRepository.Error error) {
ThreadUtil.runOnMain(() -> {
if (!userCanceled) {
linkPreviewState.setValue(LinkPreviewState.forLinksWithNoPreview(error));
if (activeUrl != null) {
linkPreviewState.setValue(LinkPreviewState.forLinksWithNoPreview(error));
} else {
linkPreviewState.setValue(LinkPreviewState.forNoLinks());
}
}
activeRequest = null;
});
Expand Down Expand Up @@ -145,19 +163,6 @@ public void onTransportChanged(boolean isSms) {
}
}

public void onSend() {
if (activeRequest != null) {
activeRequest.cancel();
activeRequest = null;
}

userCanceled = false;
activeUrl = null;

debouncer.clear();
linkPreviewState.setValue(LinkPreviewState.forNoLinks());
}

public void onEnabled() {
userCanceled = false;
enabled = SignalStore.settings().isLinkPreviewsEnabled();
Expand Down

1 comment on commit 2bac1a7

@Tursko
Copy link

@Tursko Tursko commented on 2bac1a7 Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this fix!

Please sign in to comment.