Load missing attachments when they arrive.

parent 593a0c46
Pipeline #3602 passed with stage
in 8 minutes and 52 seconds
...@@ -82,6 +82,7 @@ import org.briarproject.briar.api.messaging.Attachment; ...@@ -82,6 +82,7 @@ import org.briarproject.briar.api.messaging.Attachment;
import org.briarproject.briar.api.messaging.AttachmentHeader; import org.briarproject.briar.api.messaging.AttachmentHeader;
import org.briarproject.briar.api.messaging.MessagingManager; import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessageHeader; import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager; import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -113,6 +114,7 @@ import static java.util.Collections.sort; ...@@ -113,6 +114,7 @@ import static java.util.Collections.sort;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logDuration; import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now; import static org.briarproject.bramble.util.LogUtils.now;
...@@ -140,7 +142,7 @@ public class ConversationActivity extends BriarActivity ...@@ -140,7 +142,7 @@ public class ConversationActivity extends BriarActivity
public static final String CONTACT_ID = "briar.CONTACT_ID"; public static final String CONTACT_ID = "briar.CONTACT_ID";
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(ConversationActivity.class.getName()); getLogger(ConversationActivity.class.getName());
private static final int TRANSITION_DURATION_MS = 500; private static final int TRANSITION_DURATION_MS = 500;
private static final int ONBOARDING_DELAY_MS = 250; private static final int ONBOARDING_DELAY_MS = 250;
...@@ -173,6 +175,8 @@ public class ConversationActivity extends BriarActivity ...@@ -173,6 +175,8 @@ public class ConversationActivity extends BriarActivity
volatile GroupInvitationManager groupInvitationManager; volatile GroupInvitationManager groupInvitationManager;
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>(); private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
private final Map<MessageId, PrivateMessageHeader> missingAttachments =
new ConcurrentHashMap<>();
private final Observer<String> contactNameObserver = name -> { private final Observer<String> contactNameObserver = name -> {
requireNonNull(name); requireNonNull(name);
loadMessages(); loadMessages();
...@@ -455,15 +459,19 @@ public class ConversationActivity extends BriarActivity ...@@ -455,15 +459,19 @@ public class ConversationActivity extends BriarActivity
List<AttachmentItem> items = attachmentRetriever.cacheGet(id); List<AttachmentItem> items = attachmentRetriever.cacheGet(id);
if (items == null) { if (items == null) {
LOG.info("Eagerly loading image size for latest message"); LOG.info("Eagerly loading image size for latest message");
Attachment a = attachmentRetriever AttachmentHeader header = headers.get(0);
.getMessageAttachment(headers.get(0)); try {
AttachmentItem item = Attachment a = attachmentRetriever
attachmentRetriever.getAttachmentItem(a, true); .getMessageAttachment(header);
attachmentRetriever.cachePut(id, singletonList(item)); AttachmentItem item =
attachmentRetriever.getAttachmentItem(a, true);
attachmentRetriever.cachePut(id, singletonList(item));
} catch (NoSuchMessageException e) {
LOG.info("Attachment not received yet");
missingAttachments.put(header.getMessageId(), h);
}
} }
} }
} catch (NoSuchMessageException e) {
LOG.info("Attachment not received yet");
} catch (DbException e) { } catch (DbException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
...@@ -543,25 +551,30 @@ public class ConversationActivity extends BriarActivity ...@@ -543,25 +551,30 @@ public class ConversationActivity extends BriarActivity
&& adapter.isScrolledToBottom(layoutManager); && adapter.isScrolledToBottom(layoutManager);
} }
private void loadMessageAttachments(MessageId messageId, private void loadMessageAttachments(PrivateMessageHeader h) {
List<AttachmentHeader> headers) {
// TODO: Use placeholders for missing/invalid attachments // TODO: Use placeholders for missing/invalid attachments
runOnDbThread(() -> { runOnDbThread(() -> {
try { try {
// TODO move getting the items off to IoExecutor, if size == 1 // TODO move getting the items off to IoExecutor, if size == 1
List<AttachmentHeader> headers = h.getAttachmentHeaders();
boolean needsSize = headers.size() == 1; boolean needsSize = headers.size() == 1;
List<AttachmentItem> items = new ArrayList<>(headers.size()); List<AttachmentItem> items = new ArrayList<>(headers.size());
for (AttachmentHeader h : headers) { for (AttachmentHeader header : headers) {
Attachment a = attachmentRetriever.getMessageAttachment(h); try {
AttachmentItem item = Attachment a = attachmentRetriever
attachmentRetriever.getAttachmentItem(a, needsSize); .getMessageAttachment(header);
items.add(item); AttachmentItem item = attachmentRetriever
.getAttachmentItem(a, needsSize);
items.add(item);
} catch (NoSuchMessageException e) {
LOG.info("Attachment not received yet");
missingAttachments.put(header.getMessageId(), h);
return;
}
} }
// TODO: Don't cache items unless all are present and valid // Don't cache items unless all are present and valid
attachmentRetriever.cachePut(messageId, items); attachmentRetriever.cachePut(h.getId(), items);
displayMessageAttachments(messageId, items); displayMessageAttachments(h.getId(), items);
} catch (NoSuchMessageException e) {
LOG.info("Attachment not received yet");
} catch (DbException e) { } catch (DbException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
...@@ -584,7 +597,13 @@ public class ConversationActivity extends BriarActivity ...@@ -584,7 +597,13 @@ public class ConversationActivity extends BriarActivity
@Override @Override
public void eventOccurred(Event e) { public void eventOccurred(Event e) {
// TODO: Load missing attachments when they arrive if (e instanceof AttachmentReceivedEvent) {
AttachmentReceivedEvent a = (AttachmentReceivedEvent) e;
if (a.getContactId().equals(contactId)) {
LOG.info("Attachment received");
onAttachmentReceived(a.getMessageId());
}
}
if (e instanceof ContactRemovedEvent) { if (e instanceof ContactRemovedEvent) {
ContactRemovedEvent c = (ContactRemovedEvent) e; ContactRemovedEvent c = (ContactRemovedEvent) e;
if (c.getContactId().equals(contactId)) { if (c.getContactId().equals(contactId)) {
...@@ -635,6 +654,15 @@ public class ConversationActivity extends BriarActivity ...@@ -635,6 +654,15 @@ public class ConversationActivity extends BriarActivity
scrollToBottom(); scrollToBottom();
} }
@UiThread
private void onAttachmentReceived(MessageId attachmentId) {
PrivateMessageHeader h = missingAttachments.remove(attachmentId);
if (h != null) {
LOG.info("Missing attachment received");
loadMessageAttachments(h);
}
}
@UiThread @UiThread
private void onNewConversationMessage(ConversationMessageHeader h) { private void onNewConversationMessage(ConversationMessageHeader h) {
if (h instanceof ConversationRequest || if (h instanceof ConversationRequest ||
...@@ -921,11 +949,11 @@ public class ConversationActivity extends BriarActivity ...@@ -921,11 +949,11 @@ public class ConversationActivity extends BriarActivity
} }
@Override @Override
public List<AttachmentItem> getAttachmentItems(MessageId m, public List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h) {
List<AttachmentHeader> headers) { List<AttachmentItem> attachments =
List<AttachmentItem> attachments = attachmentRetriever.cacheGet(m); attachmentRetriever.cacheGet(h.getId());
if (attachments == null) { if (attachments == null) {
loadMessageAttachments(m, headers); loadMessageAttachments(h);
return emptyList(); return emptyList();
} }
return attachments; return attachments;
......
...@@ -15,7 +15,6 @@ import org.briarproject.briar.api.forum.ForumInvitationRequest; ...@@ -15,7 +15,6 @@ import org.briarproject.briar.api.forum.ForumInvitationRequest;
import org.briarproject.briar.api.forum.ForumInvitationResponse; import org.briarproject.briar.api.forum.ForumInvitationResponse;
import org.briarproject.briar.api.introduction.IntroductionRequest; import org.briarproject.briar.api.introduction.IntroductionRequest;
import org.briarproject.briar.api.introduction.IntroductionResponse; import org.briarproject.briar.api.introduction.IntroductionResponse;
import org.briarproject.briar.api.messaging.AttachmentHeader;
import org.briarproject.briar.api.messaging.PrivateMessageHeader; import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest; import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest;
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse; import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse;
...@@ -56,8 +55,7 @@ class ConversationVisitor implements ...@@ -56,8 +55,7 @@ class ConversationVisitor implements
if (h.getAttachmentHeaders().isEmpty()) { if (h.getAttachmentHeaders().isEmpty()) {
attachments = emptyList(); attachments = emptyList();
} else { } else {
attachments = attachmentCache attachments = attachmentCache.getAttachmentItems(h);
.getAttachmentItems(h.getId(), h.getAttachmentHeaders());
} }
if (h.isLocal()) { if (h.isLocal()) {
item = new ConversationMessageItem( item = new ConversationMessageItem(
...@@ -295,7 +293,6 @@ class ConversationVisitor implements ...@@ -295,7 +293,6 @@ class ConversationVisitor implements
} }
interface AttachmentCache { interface AttachmentCache {
List<AttachmentItem> getAttachmentItems(MessageId m, List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h);
List<AttachmentHeader> headers);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment