mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Update RemoteWipeManager
This commit is contained in:
@@ -20,12 +20,13 @@ import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.MessageStatus;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.versioning.ClientVersioningManager;
|
||||
import org.briarproject.briar.api.attachment.AttachmentHeader;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.DeletionResult;
|
||||
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
|
||||
import org.briarproject.briar.api.socialbackup.ShardMessageHeader;
|
||||
import org.briarproject.briar.api.remotewipe.RemoteWipeMessageHeader;
|
||||
import org.briarproject.briar.client.ConversationClientImpl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -47,6 +48,7 @@ import static org.briarproject.briar.socialbackup.SocialBackupConstants.MSG_KEY_
|
||||
public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
implements RemoteWipeManager, LifecycleManager.OpenDatabaseHook {
|
||||
|
||||
private final ClientVersioningManager clientVersioningManager;
|
||||
private final Group localGroup;
|
||||
private final Clock clock;
|
||||
private final ContactGroupFactory contactGroupFactory;
|
||||
@@ -60,19 +62,51 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
MessageTracker messageTracker,
|
||||
Clock clock,
|
||||
ContactManager contactManager,
|
||||
ClientVersioningManager clientVersioningManager,
|
||||
ContactGroupFactory contactGroupFactory) {
|
||||
super(db, clientHelper, metadataParser, messageTracker);
|
||||
this.clock = clock;
|
||||
this.contactGroupFactory = contactGroupFactory;
|
||||
this.contactManager = contactManager;
|
||||
this.clientVersioningManager = clientVersioningManager;
|
||||
localGroup =
|
||||
contactGroupFactory.createLocalGroup(CLIENT_ID, MAJOR_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDatabaseOpened(Transaction txn) throws DbException {
|
||||
System.out.println("DATAbase opened");
|
||||
if (db.containsGroup(txn, localGroup.getId())) return;
|
||||
db.addGroup(txn, localGroup);
|
||||
// Set things up for any pre-existing contacts
|
||||
for (Contact c : db.getContacts(txn)) {
|
||||
// Create a group to share with the contact
|
||||
Group g = getContactGroup(c);
|
||||
db.addGroup(txn, g);
|
||||
// Apply the client's visibility to the contact group
|
||||
Group.Visibility client = clientVersioningManager.getClientVisibility(txn,
|
||||
c.getId(), CLIENT_ID, MAJOR_VERSION);
|
||||
db.setGroupVisibility(txn, c.getId(), g.getId(), client);
|
||||
// Attach the contact ID to the group
|
||||
setContactId(txn, g.getId(), c.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private void setContactId(Transaction txn, GroupId g, ContactId c)
|
||||
throws DbException {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
d.put(GROUP_KEY_CONTACT_ID, c.getInt());
|
||||
try {
|
||||
clientHelper.mergeGroupMetadata(txn, g, d);
|
||||
} catch (FormatException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean incomingMessage(Transaction txn, Message m, BdfList body,
|
||||
BdfDictionary meta) throws DbException, FormatException {
|
||||
|
||||
System.out.println("Incoming message called");
|
||||
MessageType type = MessageType.fromValue(body.getLong(0).intValue());
|
||||
if (type == SETUP) {
|
||||
|
||||
@@ -95,9 +129,13 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
// TODO if (we already have a set of wipers?) do something
|
||||
if (wipers.size() < 2) throw new FormatException();
|
||||
for (ContactId c : wipers) {
|
||||
sendSetupMessage(txn, contactManager.getContact(c));
|
||||
System.out.println("Sending a setup message...");
|
||||
sendSetupMessage(txn, contactManager.getContact(txn, c));
|
||||
}
|
||||
|
||||
System.out.println("All setup messages sent");
|
||||
if (!db.containsGroup(txn, localGroup.getId()))
|
||||
db.addGroup(txn, localGroup);
|
||||
// TODO Make some sort of record of this
|
||||
// clientHelper.mergeGroupMetadata(txn, localGroup.getId(), meta)
|
||||
}
|
||||
@@ -144,10 +182,6 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
messageTracker.trackOutgoingMessage(txn, m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDatabaseOpened(Transaction txn) throws DbException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Group getContactGroup(Contact c) {
|
||||
@@ -184,8 +218,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
}
|
||||
|
||||
// TODO create a SetupMessageHeader
|
||||
private ShardMessageHeader createSetupMessageHeader(
|
||||
private RemoteWipeMessageHeader createSetupMessageHeader(
|
||||
Message message, BdfDictionary meta, MessageStatus status
|
||||
)
|
||||
throws FormatException {
|
||||
@@ -200,7 +233,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
List<AttachmentHeader> attachmentHeaders =
|
||||
new ArrayList<>();
|
||||
return new ShardMessageHeader(
|
||||
return new RemoteWipeMessageHeader(
|
||||
message.getId(), message.getGroupId(), timestamp,
|
||||
isLocal, read, status.isSent(), status.isSeen(),
|
||||
attachmentHeaders);
|
||||
|
||||
@@ -1,14 +1,29 @@
|
||||
package org.briarproject.briar.remotewipe;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
|
||||
import org.briarproject.briar.api.remotewipe.RemoteWipeMessageHeader;
|
||||
import org.briarproject.briar.test.BriarIntegrationTest;
|
||||
import org.briarproject.briar.test.BriarIntegrationTestComponent;
|
||||
import org.briarproject.briar.test.DaggerBriarIntegrationTestComponent;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegrationTestComponent> {
|
||||
|
||||
private RemoteWipeManager remoteWipeManager0;
|
||||
@@ -59,6 +74,59 @@ public class RemoteWipeIntegrationTest extends BriarIntegrationTest<BriarIntegra
|
||||
|
||||
@Test
|
||||
public void testRemoteWipe() throws Exception {
|
||||
db0.transaction(false, txn -> {
|
||||
// TODO assert that we do not already have a wipe setup
|
||||
// assertNull(socialBackupManager0.getBackupMetadata(txn));
|
||||
remoteWipeManager0.setup(txn,
|
||||
asList(contactId1From0, contactId2From0));
|
||||
// TODO now check that we do have a wipe setup
|
||||
});
|
||||
// Sync the setup messages to the contacts
|
||||
sync0To1(1, true);
|
||||
sync0To2(1, true);
|
||||
// TODO check for the headers
|
||||
// The setup message from 0 should have arrived at 1
|
||||
Collection<ConversationMessageHeader> messages0At1 =
|
||||
getMessages0At1();
|
||||
assertEquals(1, messages0At1.size());
|
||||
for (ConversationMessageHeader h : messages0At1) {
|
||||
assertTrue(h instanceof RemoteWipeMessageHeader);
|
||||
RemoteWipeMessageHeader r = (RemoteWipeMessageHeader) h;
|
||||
assertFalse(r.isLocal());
|
||||
}
|
||||
// db1.transaction(false, txn -> {
|
||||
// assertTrue(socialBackupManager1.amCustodian(txn, contactId0From1));
|
||||
// });
|
||||
}
|
||||
|
||||
private Collection<ConversationMessageHeader> getMessages1At0()
|
||||
throws DbException {
|
||||
return db0.transactionWithResult(true, txn -> remoteWipeManager0
|
||||
.getMessageHeaders(txn, contactId1From0));
|
||||
}
|
||||
|
||||
private Collection<ConversationMessageHeader> getMessages2At0()
|
||||
throws DbException {
|
||||
return db0.transactionWithResult(true, txn -> remoteWipeManager0
|
||||
.getMessageHeaders(txn, contactId2From0));
|
||||
}
|
||||
|
||||
private Collection<ConversationMessageHeader> getMessages0At1()
|
||||
throws DbException {
|
||||
return db1.transactionWithResult(true, txn -> remoteWipeManager1
|
||||
.getMessageHeaders(txn, contactId0From1));
|
||||
}
|
||||
|
||||
private Collection<ConversationMessageHeader> getMessages0At2()
|
||||
throws DbException {
|
||||
return db1.transactionWithResult(true, txn -> remoteWipeManager2
|
||||
.getMessageHeaders(txn, contactId0From2));
|
||||
}
|
||||
|
||||
public static void assertGroupCount(MessageTracker tracker, GroupId g,
|
||||
long msgCount, long unreadCount) throws DbException {
|
||||
MessageTracker.GroupCount c1 = tracker.getGroupCount(g);
|
||||
assertEquals(msgCount, c1.getMsgCount());
|
||||
assertEquals(unreadCount, c1.getUnreadCount());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user