Commit ffeca881 authored by Torsten Grote's avatar Torsten Grote

Prepare private message retrieval through ConversationManager

parent 59fae2fa
......@@ -341,15 +341,15 @@ public class ConversationActivity extends BriarActivity
try {
long start = now();
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(contactId);
messagingManager.getMessages(contactId);
Collection<PrivateMessageHeader> introductions =
introductionManager.getIntroductionMessages(contactId);
introductionManager.getMessages(contactId);
Collection<PrivateMessageHeader> forumInvitations =
forumSharingManager.getInvitationMessages(contactId);
forumSharingManager.getMessages(contactId);
Collection<PrivateMessageHeader> blogInvitations =
blogSharingManager.getInvitationMessages(contactId);
blogSharingManager.getMessages(contactId);
Collection<PrivateMessageHeader> groupInvitations =
groupInvitationManager.getInvitationMessages(contactId);
groupInvitationManager.getMessages(contactId);
List<PrivateMessageHeader> invitations = new ArrayList<>(
forumInvitations.size() + blogInvitations.size() +
groupInvitations.size());
......
......@@ -51,7 +51,8 @@ public interface IntroductionManager extends ConversationClient {
/**
* Returns all introduction messages for the given contact.
*/
Collection<PrivateMessageHeader> getIntroductionMessages(ContactId contactId)
@Deprecated
Collection<PrivateMessageHeader> getMessages(ContactId contactId)
throws DbException;
}
......@@ -10,6 +10,8 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import java.util.Collection;
@NotNullByDefault
public interface ConversationManager {
......@@ -19,6 +21,15 @@ public interface ConversationManager {
*/
void registerConversationClient(ConversationClient client);
/**
* Returns (the headers) of all messages in the given private conversation.
*
* Only {@link MessagingManager} returns only headers.
* The others also return the message body.
*/
Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException;
/**
* Returns the unified group count for all private conversation messages.
*/
......@@ -29,6 +40,9 @@ public interface ConversationManager {
Group getContactGroup(Contact c);
Collection<PrivateMessageHeader> getMessages(Transaction txn,
ContactId contactId) throws DbException;
GroupCount getGroupCount(Transaction txn, ContactId c)
throws DbException;
......
......@@ -46,7 +46,8 @@ public interface MessagingManager extends ConversationClient {
/**
* Returns the headers of all messages in the given private conversation.
*/
Collection<PrivateMessageHeader> getMessageHeaders(ContactId c)
@Deprecated
Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException;
/**
......
......@@ -77,7 +77,8 @@ public interface GroupInvitationManager extends ConversationClient {
* Returns all private group invitation messages related to the given
* contact.
*/
Collection<PrivateMessageHeader> getInvitationMessages(ContactId c)
@Deprecated
Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException;
/**
......
......@@ -40,8 +40,9 @@ public interface SharingManager<S extends Shareable>
* Returns all group sharing messages sent by the Contact
* identified by contactId.
*/
Collection<PrivateMessageHeader> getInvitationMessages(
ContactId contactId) throws DbException;
@Deprecated
Collection<PrivateMessageHeader> getMessages(ContactId contactId)
throws DbException;
/**
* Returns all invitations to groups.
......
......@@ -399,17 +399,30 @@ class IntroductionManagerImpl extends ConversationClientImpl
}
@Override
public Collection<PrivateMessageHeader> getIntroductionMessages(ContactId c)
public Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException {
List<PrivateMessageHeader> messages;
Collection<PrivateMessageHeader> messages;
Transaction txn = db.startTransaction(true);
try {
messages = getMessages(txn, c);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return messages;
}
@Override
public Collection<PrivateMessageHeader> getMessages(Transaction txn,
ContactId c) throws DbException {
try {
Contact contact = db.getContact(txn, c);
GroupId contactGroupId = getContactGroup(contact).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
Map<MessageId, BdfDictionary> results = clientHelper
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
messages = new ArrayList<>(results.size());
List<PrivateMessageHeader> messages =
new ArrayList<>(results.size());
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
MessageId m = e.getKey();
MessageMetadata meta =
......@@ -432,13 +445,10 @@ class IntroductionManagerImpl extends ConversationClientImpl
status, ss.bdfSession, false));
}
}
db.commitTransaction(txn);
return messages;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return messages;
}
private IntroductionRequest parseInvitationRequest(Transaction txn,
......
......@@ -7,7 +7,11 @@ import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import org.briarproject.briar.api.messaging.ConversationManager;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
......@@ -33,6 +37,22 @@ class ConversationManagerImpl implements ConversationManager {
throw new IllegalStateException("Client is already registered");
}
@Override
public Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException {
List<PrivateMessageHeader> messages = new ArrayList<>();
Transaction txn = db.startTransaction(true);
try {
for (ConversationClient client : clients) {
messages.addAll(client.getMessages(txn, c));
}
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return messages;
}
@Override
public GroupCount getGroupCount(ContactId contactId) throws DbException {
int msgCount = 0, unreadCount = 0;
......
......@@ -178,21 +178,31 @@ class MessagingManagerImpl extends ConversationClientImpl
}
@Override
public Collection<PrivateMessageHeader> getMessageHeaders(ContactId c)
public Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException {
Collection<PrivateMessageHeader> headers;
Transaction txn = db.startTransaction(true);
try {
headers = getMessages(txn, c);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return headers;
}
@Override
public Collection<PrivateMessageHeader> getMessages(Transaction txn,
ContactId c) throws DbException {
Map<MessageId, BdfDictionary> metadata;
Collection<MessageStatus> statuses;
GroupId g;
Transaction txn = db.startTransaction(true);
try {
g = getContactGroup(db.getContact(txn, c)).getId();
metadata = clientHelper.getMessageMetadataAsDictionary(txn, g);
statuses = db.getMessageStatus(txn, c, g);
db.commitTransaction(txn);
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
Collection<PrivateMessageHeader> headers = new ArrayList<>();
for (MessageStatus s : statuses) {
......@@ -203,9 +213,8 @@ class MessagingManagerImpl extends ConversationClientImpl
long timestamp = meta.getLong("timestamp");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean("read");
headers.add(
new PrivateMessageHeader(id, g, timestamp, local, read,
s.isSent(), s.isSeen()));
headers.add(new PrivateMessageHeader(id, g, timestamp, local,
read, s.isSent(), s.isSeen()));
} catch (FormatException e) {
throw new DbException(e);
}
......
......@@ -368,17 +368,30 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
}
@Override
public Collection<PrivateMessageHeader> getInvitationMessages(ContactId c)
public Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException {
List<PrivateMessageHeader> messages;
Collection<PrivateMessageHeader> messages;
Transaction txn = db.startTransaction(true);
try {
messages = getMessages(txn, c);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return messages;
}
@Override
public Collection<PrivateMessageHeader> getMessages(Transaction txn,
ContactId c) throws DbException {
try {
Contact contact = db.getContact(txn, c);
GroupId contactGroupId = getContactGroup(contact).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
Map<MessageId, BdfDictionary> results = clientHelper
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
messages = new ArrayList<>(results.size());
List<PrivateMessageHeader> messages =
new ArrayList<>(results.size());
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
MessageId m = e.getKey();
MessageMetadata meta =
......@@ -398,13 +411,10 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
status, false));
}
}
db.commitTransaction(txn);
return messages;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return messages;
}
private GroupInvitationRequest parseInvitationRequest(Transaction txn,
......
......@@ -321,17 +321,30 @@ abstract class SharingManagerImpl<S extends Shareable>
}
@Override
public Collection<PrivateMessageHeader> getInvitationMessages(ContactId c)
public Collection<PrivateMessageHeader> getMessages(ContactId c)
throws DbException {
List<PrivateMessageHeader> messages;
Collection<PrivateMessageHeader> messages;
Transaction txn = db.startTransaction(true);
try {
messages = getMessages(txn, c);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
return messages;
}
@Override
public Collection<PrivateMessageHeader> getMessages(Transaction txn,
ContactId c) throws DbException {
try {
Contact contact = db.getContact(txn, c);
GroupId contactGroupId = getContactGroup(contact).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
Map<MessageId, BdfDictionary> results = clientHelper
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
messages = new ArrayList<>(results.size());
List<PrivateMessageHeader> messages =
new ArrayList<>(results.size());
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
MessageId m = e.getKey();
MessageMetadata meta =
......@@ -351,13 +364,10 @@ abstract class SharingManagerImpl<S extends Shareable>
status, false));
}
}
db.commitTransaction(txn);
return messages;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return messages;
}
private PrivateRequest parseInvitationRequest(Transaction txn,
......
......@@ -298,21 +298,17 @@ public class IntroductionIntegrationTest
Group g1 = introductionManager0.getContactGroup(introducee1);
Group g2 = introductionManager0.getContactGroup(introducee2);
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId1From0)
.size());
introductionManager0.getMessages(contactId1From0).size());
assertGroupCount(messageTracker0, g1.getId(), 2, 1);
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId2From0)
.size());
introductionManager0.getMessages(contactId2From0).size());
assertGroupCount(messageTracker0, g2.getId(), 2, 1);
assertEquals(2,
introductionManager1.getIntroductionMessages(contactId0From1)
.size());
introductionManager1.getMessages(contactId0From1).size());
assertGroupCount(messageTracker1, g1.getId(), 2, 1);
// introducee2 should also have the decline response of introducee1
assertEquals(3,
introductionManager2.getIntroductionMessages(contactId0From2)
.size());
introductionManager2.getMessages(contactId0From2).size());
assertGroupCount(messageTracker2, g2.getId(), 3, 2);
assertFalse(listener0.aborted);
......@@ -363,16 +359,13 @@ public class IntroductionIntegrationTest
.contactExists(author1.getId(), author2.getId()));
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId1From0)
.size());
introductionManager0.getMessages(contactId1From0).size());
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId2From0)
.size());
introductionManager0.getMessages(contactId2From0).size());
assertEquals(3,
introductionManager1.getIntroductionMessages(contactId0From1)
.size());
introductionManager1.getMessages(contactId0From1).size());
assertEquals(3,
introductionManager2.getIntroductionMessages(contactId0From2)
introductionManager2.getMessages(contactId0From2)
.size());
assertFalse(listener0.aborted);
assertFalse(listener1.aborted);
......@@ -520,20 +513,16 @@ public class IntroductionIntegrationTest
Group g1 = introductionManager0.getContactGroup(introducee1);
Group g2 = introductionManager0.getContactGroup(introducee2);
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId1From0)
.size());
introductionManager0.getMessages(contactId1From0).size());
assertGroupCount(messageTracker0, g1.getId(), 2, 1);
assertEquals(2,
introductionManager0.getIntroductionMessages(contactId2From0)
.size());
introductionManager0.getMessages(contactId2From0).size());
assertGroupCount(messageTracker0, g2.getId(), 2, 1);
assertEquals(3,
introductionManager1.getIntroductionMessages(contactId0From1)
.size());
introductionManager1.getMessages(contactId0From1).size());
assertGroupCount(messageTracker1, g1.getId(), 3, 2);
assertEquals(3,
introductionManager2.getIntroductionMessages(contactId0From2)
.size());
introductionManager2.getMessages(contactId0From2).size());
assertGroupCount(messageTracker2, g2.getId(), 3, 2);
assertFalse(listener0.aborted);
......@@ -557,8 +546,7 @@ public class IntroductionIntegrationTest
assertFalse(listener1.requestReceived);
// make really sure we don't have that request
assertTrue(introductionManager1.getIntroductionMessages(contactId0From1)
.isEmpty());
assertTrue(introductionManager1.getMessages(contactId0From1).isEmpty());
// The message was invalid, so no abort message was sent
assertFalse(listener0.aborted);
......@@ -1101,22 +1089,19 @@ public class IntroductionIntegrationTest
private void assertDefaultUiMessages() throws DbException {
Collection<PrivateMessageHeader> messages =
introductionManager0.getIntroductionMessages(contactId1From0);
introductionManager0.getMessages(contactId1From0);
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = introductionManager0.getIntroductionMessages(
contactId2From0);
messages = introductionManager0.getMessages(contactId2From0);
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = introductionManager1.getIntroductionMessages(
contactId0From1);
messages = introductionManager1.getMessages(contactId0From1);
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
messages = introductionManager2.getIntroductionMessages(
contactId0From2);
messages = introductionManager2.getMessages(contactId0From2);
assertEquals(2, messages.size());
assertMessagesAreAcked(messages);
}
......@@ -1299,8 +1284,7 @@ public class IntroductionIntegrationTest
private IntroductionRequest getIntroductionRequest(
IntroductionManager manager, ContactId contactId)
throws DbException {
for (PrivateMessageHeader im : manager
.getIntroductionMessages(contactId)) {
for (PrivateMessageHeader im : manager.getMessages(contactId)) {
if (im instanceof IntroductionRequest) {
return (IntroductionRequest) im;
}
......
......@@ -93,7 +93,7 @@ public class GroupInvitationIntegrationTest
assertFalse(item.isSubscribed());
Collection<PrivateMessageHeader> messages =
groupInvitationManager1.getInvitationMessages(contactId0From1);
groupInvitationManager1.getMessages(contactId0From1);
assertEquals(1, messages.size());
GroupInvitationRequest request =
(GroupInvitationRequest) messages.iterator().next();
......@@ -118,7 +118,7 @@ public class GroupInvitationIntegrationTest
.respondToInvitation(contactId0From1, privateGroup0, false);
Collection<PrivateMessageHeader> messages =
groupInvitationManager1.getInvitationMessages(contactId0From1);
groupInvitationManager1.getMessages(contactId0From1);
assertEquals(2, messages.size());
boolean foundResponse = false;
for (PrivateMessageHeader m : messages) {
......@@ -134,7 +134,7 @@ public class GroupInvitationIntegrationTest
sync1To0(1, true);
messages =
groupInvitationManager0.getInvitationMessages(contactId1From0);
groupInvitationManager0.getMessages(contactId1From0);
assertEquals(2, messages.size());
foundResponse = false;
for (PrivateMessageHeader m : messages) {
......@@ -165,7 +165,7 @@ public class GroupInvitationIntegrationTest
.respondToInvitation(contactId0From1, privateGroup0, true);
Collection<PrivateMessageHeader> messages =
groupInvitationManager1.getInvitationMessages(contactId0From1);
groupInvitationManager1.getMessages(contactId0From1);
assertEquals(2, messages.size());
boolean foundResponse = false;
for (PrivateMessageHeader m : messages) {
......@@ -182,7 +182,7 @@ public class GroupInvitationIntegrationTest
sync1To0(1, true);
messages =
groupInvitationManager0.getInvitationMessages(contactId1From0);
groupInvitationManager0.getMessages(contactId1From0);
assertEquals(2, messages.size());
foundResponse = false;
for (PrivateMessageHeader m : messages) {
......@@ -217,7 +217,7 @@ public class GroupInvitationIntegrationTest
Group g0 = groupInvitationManager1.getContactGroup(contact0From1);
assertGroupCount(messageTracker1, g0.getId(), 1, 1, timestamp);
PrivateMessageHeader m =
groupInvitationManager1.getInvitationMessages(contactId0From1)
groupInvitationManager1.getMessages(contactId0From1)
.iterator().next();
groupInvitationManager1
......
......@@ -702,7 +702,7 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
}});
Collection<PrivateMessageHeader> messages =
groupInvitationManager.getInvitationMessages(contactId);
groupInvitationManager.getMessages(contactId);
assertEquals(2, messages.size());
for (PrivateMessageHeader m : messages) {
assertEquals(contactGroup.getId(), m.getGroupId());
......
......@@ -148,7 +148,7 @@ public class BlogSharingIntegrationTest
// invitee has one invitation message from sharer
List<PrivateMessageHeader> list = new ArrayList<>(
blogSharingManager1.getInvitationMessages(contactId0From1));
blogSharingManager1.getMessages(contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the message
for (PrivateMessageHeader m : list) {
......@@ -167,7 +167,7 @@ public class BlogSharingIntegrationTest
}
// sharer has own invitation message and response
assertEquals(2,
blogSharingManager0.getInvitationMessages(contactId1From0)
blogSharingManager0.getMessages(contactId1From0)
.size());
// blog can not be shared again
assertFalse(blogSharingManager0.canBeShared(blog2.getId(),
......@@ -219,7 +219,7 @@ public class BlogSharingIntegrationTest
// invitee has one invitation message from sharer
List<PrivateMessageHeader> list = new ArrayList<>(
blogSharingManager1.getInvitationMessages(contactId0From1));
blogSharingManager1.getMessages(contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the message
for (PrivateMessageHeader m : list) {
......@@ -237,7 +237,7 @@ public class BlogSharingIntegrationTest
}
}
// sharer has own invitation message and response
assertEquals(2, blogSharingManager0.getInvitationMessages(
assertEquals(2, blogSharingManager0.getMessages(
contactId1From0).size());
// blog can not be shared again
assertFalse(blogSharingManager0.canBeShared(rssBlog.getId(),
......@@ -278,7 +278,7 @@ public class BlogSharingIntegrationTest
// invitee has one invitation message from sharer and one response
List<PrivateMessageHeader> list = new ArrayList<>(
blogSharingManager1.getInvitationMessages(contactId0From1));
blogSharingManager1.getMessages(contactId0From1));
assertEquals(2, list.size());
// check things are alright with the message
for (PrivateMessageHeader m : list) {
......@@ -296,7 +296,7 @@ public class BlogSharingIntegrationTest
}
// sharer has own invitation message and response
assertEquals(2,
blogSharingManager0.getInvitationMessages(contactId1From0)
blogSharingManager0.getMessages(contactId1From0)
.size());
// blog can be shared again
assertTrue(
......@@ -383,7 +383,7 @@ public class BlogSharingIntegrationTest
// make sure 1 knows that they have blog2 already
Collection<PrivateMessageHeader> messages =
blogSharingManager1.getInvitationMessages(contactId0From1);
blogSharingManager1.getMessages(contactId0From1);
assertEquals(2, messages.size());
assertEquals(blog2, blogManager1.getBlog(blog2.getId()));
......
......@@ -132,7 +132,7 @@ public class ForumSharingIntegrationTest
// invitee has one invitation message from sharer
List<PrivateMessageHeader> list = new ArrayList<>(
forumSharingManager1.getInvitationMessages(contactId0From1));
forumSharingManager1.getMessages(contactId0From1));
assertEquals(2, list.size());
// check other things are alright with the forum message
for (PrivateMessageHeader m : list) {
......@@ -152,7 +152,7 @@ public class ForumSharingIntegrationTest
}
// sharer has own invitation message and response
assertEquals(2,
forumSharingManager0.getInvitationMessages(contactId1From0)
forumSharingManager0.getMessages(contactId1From0)
.size());
// forum can not be shared again
Contact c1 = contactManager0.getContact(contactId1From0);
......@@ -189,7 +189,7 @@ public class ForumSharingIntegrationTest
// invitee has one invitation message from sharer and one response
List<PrivateMessageHeader> list = new ArrayList<>(
forumSharingManager1.getInvitationMessages(contactId0From1));
forumSharingManager1.getMessages(contactId0From1));
assertEquals(2, list.size());
// check things are alright with the forum message
for (PrivateMessageHeader m : list) {
......@@ -207,7 +207,7 @@ public class ForumSharingIntegrationTest
}
// sharer has own invitation message and response
assertEquals(2,
forumSharingManager0.getInvitationMessages(contactId1From0)
forumSharingManager0.getMessages(contactId1From0)
.size());
// forum can be shared again
Contact c1 = contactManager0.getContact(contactId1From0);
......@@ -485,9 +485,9 @@ public class ForumSharingIntegrationTest
// and both have each other's invitations (and no response)
assertEquals(2, forumSharingManager0
.getInvitationMessages(contactId1From0).size());
.getMessages(contactId1From0).size());
assertEquals(2, forumSharingManager1
.getInvitationMessages(contactId0From1).size());
.getMessages(contactId0From1).size());
// there are no more open invitations
assertTrue(forumSharingManager0.getInvitations().isEmpty());
......@@ -743,7 +743,7 @@ public class ForumSharingIntegrationTest
// get invitation MessageId for later
MessageId invitationId = null;
for (PrivateMessageHeader m : forumSharingManager1
.getInvitationMessages(contactId0From1)) {
.getMessages(contactId0From1)) {
if (m instanceof ForumInvitationRequest) {
invitationId = m.getId();
}
......
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