Implement AutoDeleteManager.

This commit is contained in:
akwizgran
2020-11-26 13:15:01 +00:00
committed by Torsten Grote
parent 96debcd616
commit 706f4e1c4c
6 changed files with 120 additions and 6 deletions

View File

@@ -4,11 +4,28 @@ import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.ClientId;
@NotNullByDefault
public interface AutoDeleteManager {
/**
* The unique ID of the auto-delete client.
*/
ClientId CLIENT_ID = new ClientId("org.briarproject.briar.autodelete");
/**
* The current major version of the auto-delete client.
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the auto-delete client.
*/
int MINOR_VERSION = 0;
long getAutoDeleteTimer(Transaction txn, ContactId c) throws DbException;
void setAutoDeleteTimer(Transaction txn, ContactId c) throws DbException;
void setAutoDeleteTimer(Transaction txn, ContactId c, long timer)
throws DbException;
}

View File

@@ -1,5 +1,6 @@
package org.briarproject.briar;
import org.briarproject.briar.autodelete.AutoDeleteModule;
import org.briarproject.briar.avatar.AvatarModule;
import org.briarproject.briar.blog.BlogModule;
import org.briarproject.briar.feed.FeedModule;
@@ -13,6 +14,8 @@ import org.briarproject.briar.sharing.SharingModule;
public interface BriarCoreEagerSingletons {
void inject(AutoDeleteModule.EagerSingletons init);
void inject(AvatarModule.EagerSingletons init);
void inject(BlogModule.EagerSingletons init);
@@ -36,6 +39,7 @@ public interface BriarCoreEagerSingletons {
class Helper {
public static void injectEagerSingletons(BriarCoreEagerSingletons c) {
c.inject(new AutoDeleteModule.EagerSingletons());
c.inject(new AvatarModule.EagerSingletons());
c.inject(new BlogModule.EagerSingletons());
c.inject(new FeedModule.EagerSingletons());

View File

@@ -0,0 +1,7 @@
package org.briarproject.briar.autodelete;
interface AutoDeleteConstants {
// Group metadata key for storing the auto-delete timer
String GROUP_KEY_AUTO_DELETE_TIMER = "autoDeleteTimer";
}

View File

@@ -1,33 +1,100 @@
package org.briarproject.briar.autodelete;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.client.ContactGroupFactory;
import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.ContactManager.ContactHook;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupFactory;
import org.briarproject.briar.api.autodelete.AutoDeleteManager;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static org.briarproject.briar.api.autodelete.AutoDeleteConstants.NO_AUTO_DELETE_TIMER;
import static org.briarproject.briar.autodelete.AutoDeleteConstants.GROUP_KEY_AUTO_DELETE_TIMER;
@Immutable
@NotNullByDefault
class AutoDeleteManagerImpl implements AutoDeleteManager {
class AutoDeleteManagerImpl
implements AutoDeleteManager, OpenDatabaseHook, ContactHook {
private final DatabaseComponent db;
private final ClientHelper clientHelper;
private final GroupFactory groupFactory;
private final Group localGroup;
@Inject
AutoDeleteManagerImpl() {
AutoDeleteManagerImpl(
DatabaseComponent db,
ClientHelper clientHelper,
GroupFactory groupFactory,
ContactGroupFactory contactGroupFactory) {
this.db = db;
this.clientHelper = clientHelper;
this.groupFactory = groupFactory;
localGroup = contactGroupFactory.createLocalGroup(CLIENT_ID,
MAJOR_VERSION);
}
@Override
public void onDatabaseOpened(Transaction txn) throws DbException {
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)) addingContact(txn, c);
}
@Override
public void addingContact(Transaction txn, Contact c) throws DbException {
Group g = getGroup(c);
db.addGroup(txn, g);
clientHelper.setContactId(txn, g.getId(), c.getId());
}
@Override
public void removingContact(Transaction txn, Contact c) throws DbException {
db.removeGroup(txn, getGroup(c));
}
@Override
public long getAutoDeleteTimer(Transaction txn, ContactId c)
throws DbException {
return NO_AUTO_DELETE_TIMER;
try {
Group g = getGroup(db.getContact(txn, c));
BdfDictionary meta =
clientHelper.getGroupMetadataAsDictionary(txn, g.getId());
return meta.getLong(GROUP_KEY_AUTO_DELETE_TIMER,
NO_AUTO_DELETE_TIMER);
} catch (FormatException e) {
throw new DbException(e);
}
}
@Override
public void setAutoDeleteTimer(Transaction txn, ContactId c)
public void setAutoDeleteTimer(Transaction txn, ContactId c, long timer)
throws DbException {
// Mmm hmm, yup, I'll bear that in mind
try {
Group g = getGroup(db.getContact(txn, c));
BdfDictionary meta = BdfDictionary.of(
new BdfEntry(GROUP_KEY_AUTO_DELETE_TIMER, timer));
clientHelper.mergeGroupMetadata(txn, g.getId(), meta);
} catch (FormatException e) {
throw new AssertionError(e);
}
}
private Group getGroup(Contact c) {
byte[] descriptor = c.getAuthor().getId().getBytes();
return groupFactory.createGroup(CLIENT_ID, MAJOR_VERSION, descriptor);
}
}

View File

@@ -1,16 +1,32 @@
package org.briarproject.briar.autodelete;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.briar.api.autodelete.AutoDeleteManager;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class AutoDeleteModule {
public static class EagerSingletons {
@Inject
AutoDeleteManager autoDeleteManager;
}
@Provides
@Singleton
AutoDeleteManager provideAutoDeleteManager(
LifecycleManager lifecycleManager, ContactManager contactManager,
AutoDeleteManagerImpl autoDeleteManager) {
lifecycleManager.registerOpenDatabaseHook(autoDeleteManager);
contactManager.registerContactHook(autoDeleteManager);
// Don't need to register with the client versioning manager as this
// client's groups aren't shared with contacts
return autoDeleteManager;
}
}

View File

@@ -65,6 +65,8 @@ public interface BriarIntegrationTestComponent
void inject(BriarIntegrationTest<BriarIntegrationTestComponent> init);
void inject(AutoDeleteModule.EagerSingletons init);
void inject(AvatarModule.EagerSingletons init);
void inject(BlogModule.EagerSingletons init);
@@ -135,6 +137,7 @@ public interface BriarIntegrationTestComponent
BriarIntegrationTestComponent c) {
BrambleCoreIntegrationTestEagerSingletons.Helper
.injectEagerSingletons(c);
c.inject(new AutoDeleteModule.EagerSingletons());
c.inject(new AvatarModule.EagerSingletons());
c.inject(new BlogModule.EagerSingletons());
c.inject(new ForumModule.EagerSingletons());