mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Prepare for new Forum Sharing Client
Methods for creating, adding and removing forums have been moved to the `ForumManager`. In order to still handle removing forums properly, a `RemoveForumHook` has been introduced. Methods for sharing forums with all current and future contacts have been removed along with the localGroup where this information was saved. The `ShareForumActivity` now has the proper label. The `SessionId` and the `ProtocolEngine` have been moved to the `clients` package. This addresses part of #322 and part of what has been discussed in #320.
This commit is contained in:
@@ -19,7 +19,7 @@ import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.introduction.IntroductionManager;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.properties.TransportProperties;
|
||||
import org.briarproject.api.properties.TransportPropertyManager;
|
||||
|
||||
@@ -140,11 +140,11 @@
|
||||
|
||||
<activity
|
||||
android:name=".android.forum.ShareForumActivity"
|
||||
android:label="@string/app_name"
|
||||
android:parentActivityName=".android.NavDrawerActivity">
|
||||
android:label="@string/forums_share_toolbar_header"
|
||||
android:parentActivityName=".android.forum.ForumActivity">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".android.NavDrawerActivity"
|
||||
android:value=".android.forum.ForumActivity"
|
||||
/>
|
||||
</activity>
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<item
|
||||
android:id="@+id/action_share_forum"
|
||||
android:icon="@drawable/ic_check_white"
|
||||
android:title="@string/forum_share_with_some"
|
||||
android:title="@string/forum_share_action"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
</menu>
|
||||
@@ -85,8 +85,7 @@
|
||||
<string name="choose_forum_name">Choose a name for your forum:</string>
|
||||
<string name="create_forum_button">Create Forum</string>
|
||||
<string name="forum_created_toast">Forum created</string>
|
||||
<string name="forum_share_with_all">Share this forum with all contacts</string>
|
||||
<string name="forum_share_with_some">Share this forum with chosen contacts</string>
|
||||
<string name="forum_share_action">Share this forum with chosen contacts</string>
|
||||
<string name="forum_share_button">Share Forum</string>
|
||||
<string name="forum_compose_post">New Forum Post</string>
|
||||
<string name="from">From:</string>
|
||||
@@ -204,6 +203,7 @@
|
||||
<string name="settings_toolbar_header">Settings</string>
|
||||
<string name="contacts_toolbar_header">Contacts</string>
|
||||
<string name="forums_toolbar_header">Forums</string>
|
||||
<string name="forums_share_toolbar_header">Choose Contacts</string>
|
||||
<!-- Progress titles -->
|
||||
<string name="progress_title_logout">Signing out of Briar..</string>
|
||||
<string name="progress_title_please_wait">Please wait..</string>
|
||||
|
||||
@@ -51,7 +51,7 @@ import org.briarproject.api.introduction.IntroductionManager;
|
||||
import org.briarproject.api.introduction.IntroductionMessage;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.messaging.PrivateMessage;
|
||||
import org.briarproject.api.messaging.PrivateMessageFactory;
|
||||
|
||||
@@ -14,7 +14,7 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ implements EventListener, OnItemClickListener {
|
||||
runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
forumSharingManager.addForum(f);
|
||||
forumManager.addForum(f);
|
||||
forumSharingManager.setSharedWith(f.getId(), shared);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.briarproject.android.AndroidComponent;
|
||||
import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumSharingManager;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
@@ -47,7 +47,7 @@ public class CreateForumActivity extends BriarActivity
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
protected volatile ForumSharingManager forumSharingManager;
|
||||
protected volatile ForumManager forumManager;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
@@ -126,8 +126,8 @@ public class CreateForumActivity extends BriarActivity
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
Forum f = forumSharingManager.createForum(name);
|
||||
forumSharingManager.addForum(f);
|
||||
Forum f = forumManager.createForum(name);
|
||||
forumManager.addForum(f);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing forum took " + duration + " ms");
|
||||
|
||||
@@ -68,7 +68,7 @@ public class ForumActivity extends BriarActivity implements EventListener,
|
||||
Logger.getLogger(ForumActivity.class.getName());
|
||||
|
||||
@Inject protected AndroidNotificationManager notificationManager;
|
||||
private Map<MessageId, byte[]> bodyCache = new HashMap<MessageId, byte[]>();
|
||||
private Map<MessageId, byte[]> bodyCache = new HashMap<>();
|
||||
private TextView empty = null;
|
||||
private ForumAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
@@ -158,7 +158,6 @@ public class ForumActivity extends BriarActivity implements EventListener,
|
||||
Intent i2 = new Intent(this, ShareForumActivity.class);
|
||||
i2.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
|
||||
i2.putExtra(GROUP_ID, groupId.getBytes());
|
||||
i2.putExtra(FORUM_NAME, forum.getName());
|
||||
ActivityOptionsCompat options = ActivityOptionsCompat
|
||||
.makeCustomAnimation(this, android.R.anim.slide_in_left,
|
||||
android.R.anim.slide_out_right);
|
||||
@@ -302,7 +301,7 @@ public class ForumActivity extends BriarActivity implements EventListener,
|
||||
}
|
||||
|
||||
private void markPostsRead() {
|
||||
List<MessageId> unread = new ArrayList<MessageId>();
|
||||
List<MessageId> unread = new ArrayList<>();
|
||||
int count = adapter.getCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
ForumPostHeader h = adapter.getItem(i).getHeader();
|
||||
|
||||
@@ -401,7 +401,7 @@ public class ForumListFragment extends BaseEventFragment implements
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
forumSharingManager.removeForum(f);
|
||||
forumManager.removeForum(f);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Removing forum took " + duration + " ms");
|
||||
|
||||
@@ -33,7 +33,6 @@ import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.android.forum.ForumActivity.FORUM_NAME;
|
||||
|
||||
public class ShareForumActivity extends BriarActivity implements
|
||||
BaseContactListAdapter.OnItemClickListener {
|
||||
@@ -59,9 +58,6 @@ public class ShareForumActivity extends BriarActivity implements
|
||||
byte[] b = i.getByteArrayExtra(GROUP_ID);
|
||||
if (b == null) throw new IllegalStateException();
|
||||
groupId = new GroupId(b);
|
||||
String forumName = i.getStringExtra(FORUM_NAME);
|
||||
if (forumName == null) throw new IllegalStateException();
|
||||
setTitle(forumName);
|
||||
|
||||
adapter = new ContactSelectorAdapter(this, this);
|
||||
BriarRecyclerView list =
|
||||
@@ -112,11 +108,9 @@ public class ShareForumActivity extends BriarActivity implements
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
List<ContactListItem> contacts =
|
||||
new ArrayList<ContactListItem>();
|
||||
Collection<ContactId> selectedContacts =
|
||||
new HashSet<ContactId>(
|
||||
forumSharingManager.getSharedWith(groupId));
|
||||
List<ContactListItem> contacts = new ArrayList<>();
|
||||
Collection<ContactId> selectedContacts = new HashSet<>(
|
||||
forumSharingManager.getSharedWith(groupId));
|
||||
|
||||
for (Contact c : contactManager.getActiveContacts()) {
|
||||
LocalAuthor localAuthor = identityManager
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.briarproject.api;
|
||||
package org.briarproject.api.clients;
|
||||
|
||||
import org.briarproject.api.event.Event;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.briarproject.api.introduction;
|
||||
package org.briarproject.api.clients;
|
||||
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies an
|
||||
* introduction session.
|
||||
* Type-safe wrapper for a byte array
|
||||
* that uniquely identifies a protocol session.
|
||||
*/
|
||||
public class SessionId extends MessageId {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.briarproject.api.event;
|
||||
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
|
||||
public class IntroductionAbortedEvent extends Event {
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.briarproject.api.forum;
|
||||
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -12,6 +13,18 @@ public interface ForumManager {
|
||||
/** Returns the unique ID of the forum client. */
|
||||
ClientId getClientId();
|
||||
|
||||
/** Creates a forum with the given name. */
|
||||
Forum createForum(String name);
|
||||
|
||||
/** Creates a forum with the given name and salt. */
|
||||
Forum createForum(String name, byte[] salt);
|
||||
|
||||
/** Subscribes to a forum. */
|
||||
void addForum(Forum f) throws DbException;
|
||||
|
||||
/** Unsubscribes from a forum. */
|
||||
void removeForum(Forum f) throws DbException;
|
||||
|
||||
/** Stores a local forum post. */
|
||||
void addLocalPost(ForumPost p) throws DbException;
|
||||
|
||||
@@ -29,4 +42,11 @@ public interface ForumManager {
|
||||
|
||||
/** Marks a forum post as read or unread. */
|
||||
void setReadFlag(MessageId m, boolean read) throws DbException;
|
||||
|
||||
/** Registers a hook to be called whenever a forum is removed. */
|
||||
void registerRemoveForumHook(RemoveForumHook hook);
|
||||
|
||||
interface RemoveForumHook {
|
||||
void removingForum(Transaction txn, Forum f) throws DbException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,6 @@ public interface ForumSharingManager {
|
||||
/** Returns the unique ID of the forum sharing client. */
|
||||
ClientId getClientId();
|
||||
|
||||
/** Creates a forum with the given name. */
|
||||
Forum createForum(String name);
|
||||
|
||||
/** Subscribes to a forum. */
|
||||
void addForum(Forum f) throws DbException;
|
||||
|
||||
/** Unsubscribes from a forum. */
|
||||
void removeForum(Forum f) throws DbException;
|
||||
|
||||
/** Returns all forums to which the user could subscribe. */
|
||||
Collection<Forum> getAvailableForums() throws DbException;
|
||||
|
||||
@@ -38,6 +29,4 @@ public interface ForumSharingManager {
|
||||
void setSharedWith(GroupId g, Collection<ContactId> shared)
|
||||
throws DbException;
|
||||
|
||||
/** Shares a forum with all current and future contacts. */
|
||||
void setSharedWithAll(GroupId g) throws DbException;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package org.briarproject.api.introduction;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.api.introduction;
|
||||
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import static org.briarproject.api.introduction.IntroductionConstants.ROLE_INTRODUCEE;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.api.introduction;
|
||||
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.api.introduction;
|
||||
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
|
||||
@@ -17,10 +17,12 @@ import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -29,9 +31,12 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
|
||||
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
|
||||
import static org.briarproject.api.identity.Author.Status.ANONYMOUS;
|
||||
import static org.briarproject.api.identity.Author.Status.UNKNOWN;
|
||||
import static org.briarproject.api.identity.Author.Status.VERIFIED;
|
||||
@@ -44,11 +49,19 @@ class ForumManagerImpl implements ForumManager {
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final ClientHelper clientHelper;
|
||||
private final GroupFactory groupFactory;
|
||||
private final SecureRandom random;
|
||||
private final List<RemoveForumHook> removeHooks;
|
||||
|
||||
@Inject
|
||||
ForumManagerImpl(DatabaseComponent db, ClientHelper clientHelper) {
|
||||
ForumManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
|
||||
GroupFactory groupFactory, SecureRandom random) {
|
||||
|
||||
this.db = db;
|
||||
this.clientHelper = clientHelper;
|
||||
this.groupFactory = groupFactory;
|
||||
this.random = random;
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveForumHook>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,6 +69,53 @@ class ForumManagerImpl implements ForumManager {
|
||||
return CLIENT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Forum createForum(String name) {
|
||||
int length = StringUtils.toUtf8(name).length;
|
||||
if (length == 0) throw new IllegalArgumentException();
|
||||
if (length > MAX_FORUM_NAME_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] salt = new byte[FORUM_SALT_LENGTH];
|
||||
random.nextBytes(salt);
|
||||
return createForum(name, salt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Forum createForum(String name, byte[] salt) {
|
||||
try {
|
||||
BdfList forum = BdfList.of(name, salt);
|
||||
byte[] descriptor = clientHelper.toByteArray(forum);
|
||||
Group g = groupFactory.createGroup(getClientId(), descriptor);
|
||||
return new Forum(g, name, salt);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForum(Forum f) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
db.addGroup(txn, f.getGroup());
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForum(Forum f) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
for (RemoveForumHook hook : removeHooks)
|
||||
hook.removingForum(txn, f);
|
||||
db.removeGroup(txn, f.getGroup());
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalPost(ForumPost p) throws DbException {
|
||||
try {
|
||||
@@ -190,10 +250,16 @@ class ForumManagerImpl implements ForumManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerRemoveForumHook(RemoveForumHook hook) {
|
||||
removeHooks.add(hook);
|
||||
}
|
||||
|
||||
private Forum parseForum(Group g) throws FormatException {
|
||||
byte[] descriptor = g.getDescriptor();
|
||||
// Name, salt
|
||||
BdfList forum = clientHelper.toList(descriptor, 0, descriptor.length);
|
||||
return new Forum(g, forum.getString(0), forum.getRaw(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,9 +10,12 @@ import org.briarproject.api.forum.ForumPostFactory;
|
||||
import org.briarproject.api.forum.ForumSharingManager;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.sync.ValidationManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@@ -34,8 +37,9 @@ public class ForumModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
ForumManager provideForumManager(DatabaseComponent db,
|
||||
ClientHelper clientHelper) {
|
||||
return new ForumManagerImpl(db, clientHelper);
|
||||
ClientHelper clientHelper,
|
||||
GroupFactory groupFactory, SecureRandom random) {
|
||||
return new ForumManagerImpl(db, clientHelper, groupFactory, random);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -75,12 +79,14 @@ public class ForumModule {
|
||||
LifecycleManager lifecycleManager,
|
||||
ContactManager contactManager,
|
||||
ValidationManager validationManager,
|
||||
ForumManager forumManager,
|
||||
ForumSharingManagerImpl forumSharingManager) {
|
||||
lifecycleManager.registerClient(forumSharingManager);
|
||||
contactManager.registerAddContactHook(forumSharingManager);
|
||||
contactManager.registerRemoveContactHook(forumSharingManager);
|
||||
validationManager.registerIncomingMessageHook(
|
||||
ForumSharingManagerImpl.CLIENT_ID, forumSharingManager);
|
||||
forumManager.registerRemoveForumHook(forumSharingManager);
|
||||
return forumSharingManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.api.forum.ForumSharingManager;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -28,7 +27,6 @@ import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -40,12 +38,12 @@ import java.util.Set;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
|
||||
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
|
||||
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.api.forum.ForumManager.RemoveForumHook;
|
||||
|
||||
class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
AddContactHook, RemoveContactHook, IncomingMessageHook {
|
||||
AddContactHook, RemoveContactHook, IncomingMessageHook,
|
||||
RemoveForumHook {
|
||||
|
||||
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
||||
"cd11a5d04dccd9e2931d6fc3df456313"
|
||||
@@ -54,30 +52,23 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
private final DatabaseComponent db;
|
||||
private final ForumManager forumManager;
|
||||
private final ClientHelper clientHelper;
|
||||
private final GroupFactory groupFactory;
|
||||
private final PrivateGroupFactory privateGroupFactory;
|
||||
private final SecureRandom random;
|
||||
private final Clock clock;
|
||||
private final Group localGroup;
|
||||
|
||||
@Inject
|
||||
ForumSharingManagerImpl(DatabaseComponent db, ForumManager forumManager,
|
||||
ClientHelper clientHelper, GroupFactory groupFactory,
|
||||
PrivateGroupFactory privateGroupFactory, SecureRandom random,
|
||||
ClientHelper clientHelper, PrivateGroupFactory privateGroupFactory,
|
||||
Clock clock) {
|
||||
|
||||
this.db = db;
|
||||
this.forumManager = forumManager;
|
||||
this.clientHelper = clientHelper;
|
||||
this.groupFactory = groupFactory;
|
||||
this.privateGroupFactory = privateGroupFactory;
|
||||
this.random = random;
|
||||
this.clock = clock;
|
||||
localGroup = privateGroupFactory.createLocalGroup(CLIENT_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createLocalState(Transaction txn) throws DbException {
|
||||
db.addGroup(txn, localGroup);
|
||||
// Ensure we've set things up for any pre-existing contacts
|
||||
for (Contact c : db.getContacts(txn)) addingContact(txn, c);
|
||||
}
|
||||
@@ -96,9 +87,6 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
meta.put("contactId", c.getId().getInt());
|
||||
clientHelper.mergeGroupMetadata(txn, g.getId(), meta);
|
||||
// Share any forums that are shared with all contacts
|
||||
List<Forum> shared = getForumsSharedWithAllContacts(txn);
|
||||
storeMessage(txn, g.getId(), shared, 0);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
@@ -126,40 +114,10 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Forum createForum(String name) {
|
||||
int length = StringUtils.toUtf8(name).length;
|
||||
if (length == 0) throw new IllegalArgumentException();
|
||||
if (length > MAX_FORUM_NAME_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] salt = new byte[FORUM_SALT_LENGTH];
|
||||
random.nextBytes(salt);
|
||||
return createForum(name, salt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addForum(Forum f) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
public void removingForum(Transaction txn, Forum f) throws DbException {
|
||||
try {
|
||||
db.addGroup(txn, f.getGroup());
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForum(Forum f) throws DbException {
|
||||
try {
|
||||
// Update the list shared with each contact
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
for (Contact c : db.getContacts(txn))
|
||||
removeFromList(txn, getContactGroup(c).getId(), f);
|
||||
db.removeGroup(txn, f.getGroup());
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
for (Contact c : db.getContacts(txn))
|
||||
removeFromList(txn, getContactGroup(c).getId(), f);
|
||||
} catch (IOException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
@@ -247,8 +205,6 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
try {
|
||||
// Retrieve the forum
|
||||
Forum f = parseForum(db.getGroup(txn, g));
|
||||
// Remove the forum from the list shared with all contacts
|
||||
removeFromList(txn, localGroup.getId(), f);
|
||||
// Update the list shared with each contact
|
||||
shared = new HashSet<ContactId>(shared);
|
||||
for (Contact c : db.getContacts(txn)) {
|
||||
@@ -272,46 +228,10 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSharedWithAll(GroupId g) throws DbException {
|
||||
try {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
// Retrieve the forum
|
||||
Forum f = parseForum(db.getGroup(txn, g));
|
||||
// Add the forum to the list shared with all contacts
|
||||
addToList(txn, localGroup.getId(), f);
|
||||
// Add the forum to the list shared with each contact
|
||||
for (Contact c : db.getContacts(txn)) {
|
||||
Group cg = getContactGroup(c);
|
||||
if (addToList(txn, cg.getId(), f)) {
|
||||
if (listContains(txn, cg.getId(), g, false))
|
||||
db.setVisibleToContact(txn, c.getId(), g, true);
|
||||
}
|
||||
}
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Group getContactGroup(Contact c) {
|
||||
return privateGroupFactory.createPrivateGroup(CLIENT_ID, c);
|
||||
}
|
||||
|
||||
private List<Forum> getForumsSharedWithAllContacts(Transaction txn)
|
||||
throws DbException, FormatException {
|
||||
// Find the latest update in the local group
|
||||
LatestUpdate latest = findLatest(txn, localGroup.getId(), true);
|
||||
if (latest == null) return Collections.emptyList();
|
||||
// Retrieve and parse the latest update
|
||||
BdfList message = clientHelper.getMessageAsList(txn, latest.messageId);
|
||||
return parseForumList(message);
|
||||
}
|
||||
|
||||
private LatestUpdate findLatest(Transaction txn, GroupId g, boolean local)
|
||||
throws DbException, FormatException {
|
||||
LatestUpdate latest = null;
|
||||
@@ -334,7 +254,8 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
for (int i = 0; i < forumList.size(); i++) {
|
||||
// Name, salt
|
||||
BdfList forum = forumList.getList(i);
|
||||
forums.add(createForum(forum.getString(0), forum.getRaw(1)));
|
||||
forums.add(forumManager
|
||||
.createForum(forum.getString(0), forum.getRaw(1)));
|
||||
}
|
||||
return forums;
|
||||
}
|
||||
@@ -400,18 +321,6 @@ class ForumSharingManagerImpl implements ForumSharingManager, Client,
|
||||
}
|
||||
}
|
||||
|
||||
private Forum createForum(String name, byte[] salt) {
|
||||
try {
|
||||
BdfList forum = BdfList.of(name, salt);
|
||||
byte[] descriptor = clientHelper.toByteArray(forum);
|
||||
Group g = groupFactory.createGroup(forumManager.getClientId(),
|
||||
descriptor);
|
||||
return new Forum(g, name, salt);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Forum parseForum(Group g) throws FormatException {
|
||||
byte[] descriptor = g.getDescriptor();
|
||||
// Name, salt
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.briarproject.introduction;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.ProtocolEngine;
|
||||
import org.briarproject.api.clients.ProtocolEngine;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.event.Event;
|
||||
@@ -11,7 +11,7 @@ import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.introduction.IntroduceeAction;
|
||||
import org.briarproject.api.introduction.IntroduceeProtocolState;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.briarproject.introduction;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.ProtocolEngine;
|
||||
import org.briarproject.api.clients.ProtocolEngine;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.event.Event;
|
||||
@@ -11,7 +11,7 @@ import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.introduction.IntroducerAction;
|
||||
import org.briarproject.api.introduction.IntroducerProtocolState;
|
||||
import org.briarproject.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.briarproject.api.introduction.IntroductionManager;
|
||||
import org.briarproject.api.introduction.IntroductionMessage;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.briarproject.api.clients.ClientHelper;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.data.MetadataEncoder;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.system.Clock;
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.introduction.IntroduceeProtocolState;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
|
||||
@@ -18,7 +18,7 @@ import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.data.BdfEntry;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.data.MetadataEncoder;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
@@ -14,7 +14,7 @@ import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Metadata;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.introduction.SessionId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
Reference in New Issue
Block a user