mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Addressing second round of review issues
This commit is contained in:
@@ -34,7 +34,7 @@ import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
|
||||
|
||||
public class ForumActivity extends
|
||||
ThreadListActivity<Forum, ForumEntry, ForumPostHeader, NestedForumAdapter> {
|
||||
ThreadListActivity<Forum, ForumItem, ForumPostHeader, NestedForumAdapter> {
|
||||
|
||||
private static final int REQUEST_FORUM_SHARED = 3;
|
||||
|
||||
@@ -47,7 +47,7 @@ public class ForumActivity extends
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ThreadListController<Forum, ForumEntry, ForumPostHeader> getController() {
|
||||
protected ThreadListController<Forum, ForumItem, ForumPostHeader> getController() {
|
||||
return forumController;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package org.briarproject.android.forum;
|
||||
|
||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.android.threaded.ThreadListController;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumPostHeader;
|
||||
|
||||
public interface ForumController
|
||||
extends ThreadListController<Forum, ForumEntry, ForumPostHeader> {
|
||||
extends ThreadListController<Forum, ForumItem, ForumPostHeader> {
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.android.api.AndroidNotificationManager;
|
||||
import org.briarproject.android.threaded.ThreadListControllerImpl;
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
@@ -14,20 +15,20 @@ import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.api.forum.ForumPost;
|
||||
import org.briarproject.api.forum.ForumPostHeader;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class ForumControllerImpl
|
||||
extends ThreadListControllerImpl<Forum, ForumEntry, ForumPostHeader, ForumPost>
|
||||
extends ThreadListControllerImpl<Forum, ForumItem, ForumPostHeader, ForumPost>
|
||||
implements ForumController {
|
||||
|
||||
private static final Logger LOG =
|
||||
@@ -37,12 +38,12 @@ public class ForumControllerImpl
|
||||
|
||||
@Inject
|
||||
ForumControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
LifecycleManager lifecycleManager, IdentityManager identityManager,
|
||||
@CryptoExecutor Executor cryptoExecutor,
|
||||
ForumManager forumManager, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager) {
|
||||
super(dbExecutor, lifecycleManager, cryptoExecutor, eventBus,
|
||||
notificationManager);
|
||||
super(dbExecutor, lifecycleManager, identityManager, cryptoExecutor,
|
||||
eventBus, notificationManager);
|
||||
this.forumManager = forumManager;
|
||||
}
|
||||
|
||||
@@ -72,7 +73,7 @@ public class ForumControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Forum loadGroupItem() throws DbException {
|
||||
protected Forum loadNamedGroup() throws DbException {
|
||||
return forumManager.getForum(getGroupId());
|
||||
}
|
||||
|
||||
@@ -82,18 +83,8 @@ public class ForumControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<MessageId, String> loadBodies(
|
||||
Collection<ForumPostHeader> headers)
|
||||
throws DbException {
|
||||
Map<MessageId, String> bodies = new HashMap<>();
|
||||
for (ForumPostHeader header : headers) {
|
||||
if (!bodyCache.containsKey(header.getId())) {
|
||||
String body = StringUtils
|
||||
.fromUtf8(forumManager.getPostBody(header.getId()));
|
||||
bodies.put(header.getId(), body);
|
||||
}
|
||||
}
|
||||
return bodies;
|
||||
protected String loadMessageBody(MessageId id) throws DbException {
|
||||
return StringUtils.fromUtf8(forumManager.getPostBody(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,9 +93,17 @@ public class ForumControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ForumPost createLocalMessage(String body,
|
||||
@Nullable MessageId parentId) throws DbException {
|
||||
return forumManager.createLocalPost(getGroupId(), body, parentId);
|
||||
protected long getLatestTimestamp() throws DbException {
|
||||
GroupCount count = forumManager.getGroupCount(getGroupId());
|
||||
return count.getLatestMsgTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ForumPost createLocalMessage(String body, long timestamp,
|
||||
@Nullable MessageId parentId, LocalAuthor author) {
|
||||
return forumManager
|
||||
.createLocalPost(getGroupId(), body, timestamp, parentId,
|
||||
author);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,13 +113,13 @@ public class ForumControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteGroupItem(Forum forum) throws DbException {
|
||||
protected void deleteNamedGroup(Forum forum) throws DbException {
|
||||
forumManager.removeForum(forum);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ForumEntry buildItem(ForumPostHeader header, String body) {
|
||||
return new ForumEntry(header, body);
|
||||
protected ForumItem buildItem(ForumPostHeader header, String body) {
|
||||
return new ForumItem(header, body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,15 +6,17 @@ import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.Author.Status;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
/* This class is not thread safe */
|
||||
public class ForumEntry extends ThreadItem {
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
ForumEntry(ForumPostHeader h, String text) {
|
||||
super(h.getId(), h.getParentId(), text, h.getTimestamp(), h.getAuthor(),
|
||||
@NotThreadSafe
|
||||
public class ForumItem extends ThreadItem {
|
||||
|
||||
ForumItem(ForumPostHeader h, String body) {
|
||||
super(h.getId(), h.getParentId(), body, h.getTimestamp(), h.getAuthor(),
|
||||
h.getAuthorStatus(), h.isRead());
|
||||
}
|
||||
|
||||
public ForumEntry(MessageId messageId, MessageId parentId, String text,
|
||||
public ForumItem(MessageId messageId, MessageId parentId, String text,
|
||||
long timestamp, Author author, Status status) {
|
||||
super(messageId, parentId, text, timestamp, author, status, true);
|
||||
}
|
||||
@@ -10,9 +10,9 @@ import org.briarproject.R;
|
||||
import org.briarproject.android.threaded.ThreadItemAdapter;
|
||||
|
||||
@UiThread
|
||||
public class NestedForumAdapter extends ThreadItemAdapter<ForumEntry> {
|
||||
public class NestedForumAdapter extends ThreadItemAdapter<ForumItem> {
|
||||
|
||||
public NestedForumAdapter(ThreadItemListener<ForumEntry> listener,
|
||||
public NestedForumAdapter(ThreadItemListener<ForumItem> listener,
|
||||
LinearLayoutManager layoutManager) {
|
||||
super(listener, layoutManager);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import android.view.View;
|
||||
|
||||
import org.briarproject.android.threaded.ThreadItemViewHolder;
|
||||
|
||||
public class NestedForumHolder extends ThreadItemViewHolder<ForumEntry> {
|
||||
public class NestedForumHolder extends ThreadItemViewHolder<ForumItem> {
|
||||
|
||||
public NestedForumHolder(View v) {
|
||||
super(v);
|
||||
|
||||
@@ -4,12 +4,15 @@ import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.android.api.AndroidNotificationManager;
|
||||
import org.briarproject.android.threaded.ThreadListControllerImpl;
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.GroupMessageAddedEvent;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.privategroup.GroupMessage;
|
||||
import org.briarproject.api.privategroup.GroupMessageHeader;
|
||||
@@ -18,8 +21,6 @@ import org.briarproject.api.privategroup.PrivateGroupManager;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -36,19 +37,19 @@ public class GroupControllerImpl
|
||||
|
||||
@Inject
|
||||
GroupControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
LifecycleManager lifecycleManager, IdentityManager identityManager,
|
||||
@CryptoExecutor Executor cryptoExecutor,
|
||||
PrivateGroupManager privateGroupManager, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager) {
|
||||
super(dbExecutor, lifecycleManager, cryptoExecutor, eventBus,
|
||||
notificationManager);
|
||||
super(dbExecutor, lifecycleManager, identityManager, cryptoExecutor,
|
||||
eventBus, notificationManager);
|
||||
this.privateGroupManager = privateGroupManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResume() {
|
||||
super.onActivityResume();
|
||||
notificationManager.clearForumPostNotification(getGroupId());
|
||||
// TODO: Add new notification manager methods for private groups
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,7 +57,7 @@ public class GroupControllerImpl
|
||||
super.eventOccurred(e);
|
||||
|
||||
if (e instanceof GroupMessageAddedEvent) {
|
||||
final GroupMessageAddedEvent gmae = (GroupMessageAddedEvent) e;
|
||||
GroupMessageAddedEvent gmae = (GroupMessageAddedEvent) e;
|
||||
if (!gmae.isLocal() && gmae.getGroupId().equals(getGroupId())) {
|
||||
LOG.info("Group message received, adding...");
|
||||
final GroupMessageHeader h = gmae.getHeader();
|
||||
@@ -71,7 +72,7 @@ public class GroupControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PrivateGroup loadGroupItem() throws DbException {
|
||||
protected PrivateGroup loadNamedGroup() throws DbException {
|
||||
return privateGroupManager.getPrivateGroup(getGroupId());
|
||||
}
|
||||
|
||||
@@ -81,18 +82,8 @@ public class GroupControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<MessageId, String> loadBodies(
|
||||
Collection<GroupMessageHeader> headers)
|
||||
throws DbException {
|
||||
Map<MessageId, String> bodies = new HashMap<>();
|
||||
for (GroupMessageHeader header : headers) {
|
||||
if (!bodyCache.containsKey(header.getId())) {
|
||||
String body =
|
||||
privateGroupManager.getMessageBody(header.getId());
|
||||
bodies.put(header.getId(), body);
|
||||
}
|
||||
}
|
||||
return bodies;
|
||||
protected String loadMessageBody(MessageId id) throws DbException {
|
||||
return privateGroupManager.getMessageBody(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,10 +92,17 @@ public class GroupControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GroupMessage createLocalMessage(String body,
|
||||
@Nullable MessageId parentId) throws DbException {
|
||||
protected long getLatestTimestamp() throws DbException {
|
||||
GroupCount count = privateGroupManager.getGroupCount(getGroupId());
|
||||
return count.getLatestMsgTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GroupMessage createLocalMessage(String body, long timestamp,
|
||||
@Nullable MessageId parentId, LocalAuthor author) {
|
||||
return privateGroupManager
|
||||
.createLocalMessage(getGroupId(), body, parentId);
|
||||
.createLocalMessage(getGroupId(), body, timestamp, parentId,
|
||||
author);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,7 +112,7 @@ public class GroupControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteGroupItem(PrivateGroup group) throws DbException {
|
||||
protected void deleteNamedGroup(PrivateGroup group) throws DbException {
|
||||
privateGroupManager.removePrivateGroup(group.getId());
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ class GroupListAdapter extends BriarAdapter<GroupItem, GroupViewHolder> {
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(GroupViewHolder ui, int position) {
|
||||
ui.bindView(ctx, getItemAt(position), listener);
|
||||
ui.bindView(ctx, items.get(position), listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -68,6 +68,7 @@ public class GroupListControllerImpl extends DbControllerImpl
|
||||
throw new IllegalStateException(
|
||||
"GroupListListener needs to be attached");
|
||||
eventBus.addListener(this);
|
||||
// TODO: Add new notification manager methods for private groups
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.android.privategroup.list;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ActivityOptionsCompat;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
@@ -51,7 +50,7 @@ class GroupViewHolder extends RecyclerView.ViewHolder {
|
||||
remove = (Button) v.findViewById(R.id.removeButton);
|
||||
}
|
||||
|
||||
void bindView(final Context ctx, @Nullable final GroupItem group,
|
||||
void bindView(final Context ctx, final GroupItem group,
|
||||
@NotNull final OnGroupRemoveClickListener listener) {
|
||||
if (group == null) return;
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.support.v7.widget.RecyclerView;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -56,7 +57,7 @@ public abstract class ThreadItemAdapter<I extends ThreadItem>
|
||||
return replyItem;
|
||||
}
|
||||
|
||||
public void setItems(List<I> items) {
|
||||
public void setItems(Collection<I> items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(items);
|
||||
notifyDataSetChanged();
|
||||
|
||||
@@ -57,7 +57,7 @@ public abstract class ThreadItemViewHolder<I extends ThreadItem>
|
||||
|
||||
// TODO improve encapsulation, so we don't need to pass the adapter here
|
||||
public void bind(final ThreadItemAdapter<I> adapter,
|
||||
final ThreadItemListener listener, final I item, int pos) {
|
||||
final ThreadItemListener<I> listener, final I item, int pos) {
|
||||
|
||||
textView.setText(StringUtils.trim(item.getText()));
|
||||
|
||||
|
||||
@@ -26,10 +26,9 @@ import org.briarproject.api.clients.PostHeader;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static android.support.design.widget.Snackbar.make;
|
||||
import static android.view.View.GONE;
|
||||
@@ -75,7 +74,8 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
|
||||
list.setAdapter(adapter);
|
||||
|
||||
if (state != null) {
|
||||
replyId = new MessageId(state.getByteArray(KEY_REPLY_ID));
|
||||
byte[] replyIdBytes = state.getByteArray(KEY_REPLY_ID);
|
||||
if(replyIdBytes != null) replyId = new MessageId(replyIdBytes);
|
||||
}
|
||||
|
||||
loadItems();
|
||||
@@ -110,9 +110,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
|
||||
new UiResultExceptionHandler<Collection<I>, DbException>(
|
||||
this) {
|
||||
@Override
|
||||
public void onResultUi(Collection<I> result) {
|
||||
// FIXME What's the benefit of copying the collection?
|
||||
List<I> items = new ArrayList<>(result);
|
||||
public void onResultUi(Collection<I> items) {
|
||||
if (items.isEmpty()) {
|
||||
list.showData();
|
||||
} else {
|
||||
@@ -225,7 +223,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
|
||||
public void onSendClick(String text) {
|
||||
if (text.trim().length() == 0)
|
||||
return;
|
||||
if (text.length() > getMaxBodyLength()) {
|
||||
if (StringUtils.isTooLong(text, getMaxBodyLength())) {
|
||||
displaySnackbarShort(R.string.text_too_long);
|
||||
return;
|
||||
}
|
||||
@@ -243,12 +241,8 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
|
||||
finish();
|
||||
}
|
||||
};
|
||||
if (replyItem == null) {
|
||||
// root post
|
||||
getController().send(text, handler);
|
||||
} else {
|
||||
getController().send(text, replyItem.getId(), handler);
|
||||
}
|
||||
getController().createAndStoreMessage(text,
|
||||
replyItem != null ? replyItem.getId() : null, handler);
|
||||
textInput.hideSoftKeyboard();
|
||||
textInput.setVisibility(GONE);
|
||||
adapter.setReplyItem(null);
|
||||
@@ -268,6 +262,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
|
||||
@Override
|
||||
public void onExceptionUi(DbException exception) {
|
||||
// TODO add proper exception handling
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,9 +29,7 @@ public interface ThreadListController<G extends NamedGroup, I extends ThreadItem
|
||||
|
||||
void markItemsRead(Collection<I> items);
|
||||
|
||||
void send(String body, ResultExceptionHandler<I, DbException> handler);
|
||||
|
||||
void send(String body, @Nullable MessageId parentId,
|
||||
void createAndStoreMessage(String body, @Nullable MessageId parentId,
|
||||
ResultExceptionHandler<I, DbException> handler);
|
||||
|
||||
void deleteNamedGroup(ResultExceptionHandler<Void, DbException> handler);
|
||||
|
||||
@@ -17,6 +17,8 @@ import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.GroupRemovedEvent;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -40,11 +42,12 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ThreadListControllerImpl.class.getName());
|
||||
|
||||
protected final Executor cryptoExecutor;
|
||||
private final IdentityManager identityManager;
|
||||
private final Executor cryptoExecutor;
|
||||
protected final AndroidNotificationManager notificationManager;
|
||||
private final EventBus eventBus;
|
||||
|
||||
protected final Map<MessageId, String> bodyCache =
|
||||
private final Map<MessageId, String> bodyCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
private volatile GroupId groupId;
|
||||
@@ -52,10 +55,11 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
protected ThreadListListener<H> listener;
|
||||
|
||||
protected ThreadListControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
LifecycleManager lifecycleManager, IdentityManager identityManager,
|
||||
@CryptoExecutor Executor cryptoExecutor, EventBus eventBus,
|
||||
AndroidNotificationManager notificationManager) {
|
||||
super(dbExecutor, lifecycleManager);
|
||||
this.identityManager = identityManager;
|
||||
this.cryptoExecutor = cryptoExecutor;
|
||||
this.eventBus = eventBus;
|
||||
this.notificationManager = notificationManager;
|
||||
@@ -76,15 +80,14 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onActivityResume() {
|
||||
checkGroupId();
|
||||
notificationManager.blockNotification(groupId);
|
||||
notificationManager.blockNotification(getGroupId());
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onActivityPause() {
|
||||
notificationManager.unblockNotification(groupId);
|
||||
notificationManager.unblockNotification(getGroupId());
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@@ -97,7 +100,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof GroupRemovedEvent) {
|
||||
GroupRemovedEvent s = (GroupRemovedEvent) e;
|
||||
if (s.getGroup().getId().equals(groupId)) {
|
||||
if (s.getGroup().getId().equals(getGroupId())) {
|
||||
LOG.info("Group removed");
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
@@ -118,7 +121,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
G groupItem = loadGroupItem();
|
||||
G groupItem = loadNamedGroup();
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info(
|
||||
@@ -134,7 +137,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
protected abstract G loadGroupItem() throws DbException;
|
||||
protected abstract G loadNamedGroup() throws DbException;
|
||||
|
||||
@Override
|
||||
public void loadItems(
|
||||
@@ -152,10 +155,14 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading headers took " + duration + " ms");
|
||||
|
||||
// Load bodies
|
||||
// Load bodies into cache
|
||||
now = System.currentTimeMillis();
|
||||
Map<MessageId, String> bodies = loadBodies(headers);
|
||||
bodyCache.putAll(bodies);
|
||||
for (H header : headers) {
|
||||
if (!bodyCache.containsKey(header.getId())) {
|
||||
bodyCache.put(header.getId(),
|
||||
loadMessageBody(header.getId()));
|
||||
}
|
||||
}
|
||||
duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading bodies took " + duration + " ms");
|
||||
@@ -175,8 +182,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
protected abstract Collection<H> loadHeaders() throws DbException;
|
||||
|
||||
@DatabaseExecutor
|
||||
protected abstract Map<MessageId, String> loadBodies(Collection<H> headers)
|
||||
throws DbException;
|
||||
protected abstract String loadMessageBody(MessageId id) throws DbException;
|
||||
|
||||
@Override
|
||||
public void loadItem(final H header,
|
||||
@@ -186,9 +192,13 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
public void run() {
|
||||
LOG.info("Loading item...");
|
||||
try {
|
||||
String body = loadBodies(Collections.singletonList(header))
|
||||
.get(header.getId());
|
||||
bodyCache.put(header.getId(), body);
|
||||
String body;
|
||||
if (!bodyCache.containsKey(header.getId())) {
|
||||
body = loadMessageBody(header.getId());
|
||||
bodyCache.put(header.getId(), body);
|
||||
} else {
|
||||
body = bodyCache.get(header.getId());
|
||||
}
|
||||
I item = buildItem(header, body);
|
||||
handler.onResult(item);
|
||||
} catch (DbException e) {
|
||||
@@ -230,21 +240,19 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
protected abstract void markRead(MessageId id) throws DbException;
|
||||
|
||||
@Override
|
||||
public void send(String body,
|
||||
ResultExceptionHandler<I, DbException> resultHandler) {
|
||||
send(body, null, resultHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(final String body, @Nullable final MessageId parentId,
|
||||
public void createAndStoreMessage(final String body,
|
||||
@Nullable final MessageId parentId,
|
||||
final ResultExceptionHandler<I, DbException> handler) {
|
||||
cryptoExecutor.execute(new Runnable() {
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.info("Creating message...");
|
||||
try {
|
||||
M msg = createLocalMessage(body, parentId);
|
||||
storePost(msg, body, handler);
|
||||
LocalAuthor author = identityManager.getLocalAuthor();
|
||||
long timestamp = getLatestTimestamp();
|
||||
timestamp =
|
||||
Math.max(timestamp, System.currentTimeMillis());
|
||||
createMessage(body, timestamp, parentId, author,
|
||||
handler);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
@@ -255,8 +263,24 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
protected abstract M createLocalMessage(String body,
|
||||
@Nullable MessageId parentId) throws DbException;
|
||||
protected abstract long getLatestTimestamp() throws DbException;
|
||||
|
||||
private void createMessage(final String body, final long timestamp,
|
||||
final @Nullable MessageId parentId, final LocalAuthor author,
|
||||
final ResultExceptionHandler<I, DbException> handler) {
|
||||
cryptoExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.info("Creating message...");
|
||||
M msg = createLocalMessage(body, timestamp, parentId, author);
|
||||
storePost(msg, body, handler);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@CryptoExecutor
|
||||
protected abstract M createLocalMessage(String body, long timestamp,
|
||||
@Nullable MessageId parentId, LocalAuthor author);
|
||||
|
||||
private void storePost(final M msg, final String body,
|
||||
final ResultExceptionHandler<I, DbException> resultHandler) {
|
||||
@@ -292,8 +316,8 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
G groupItem = loadGroupItem();
|
||||
deleteGroupItem(groupItem);
|
||||
G groupItem = loadNamedGroup();
|
||||
deleteNamedGroup(groupItem);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Removing group took " + duration + " ms");
|
||||
@@ -309,7 +333,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
|
||||
}
|
||||
|
||||
@DatabaseExecutor
|
||||
protected abstract void deleteGroupItem(G groupItem) throws DbException;
|
||||
protected abstract void deleteNamedGroup(G groupItem) throws DbException;
|
||||
|
||||
private List<I> buildItems(Collection<H> headers) {
|
||||
List<I> entries = new ArrayList<>();
|
||||
|
||||
@@ -11,14 +11,12 @@ import org.briarproject.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricGradleTestRunner;
|
||||
@@ -82,7 +80,7 @@ public class ForumActivityTest {
|
||||
|
||||
private TestForumActivity forumActivity;
|
||||
@Captor
|
||||
private ArgumentCaptor<UiResultExceptionHandler<Collection<ForumEntry>, DbException>>
|
||||
private ArgumentCaptor<UiResultExceptionHandler<Collection<ForumItem>, DbException>>
|
||||
rc;
|
||||
|
||||
@Before
|
||||
@@ -94,14 +92,14 @@ public class ForumActivityTest {
|
||||
.withIntent(intent).create().resume().get();
|
||||
}
|
||||
|
||||
private List<ForumEntry> getDummyData() {
|
||||
ForumEntry[] forumEntries = new ForumEntry[6];
|
||||
private List<ForumItem> getDummyData() {
|
||||
ForumItem[] forumEntries = new ForumItem[6];
|
||||
for (int i = 0; i < forumEntries.length; i++) {
|
||||
AuthorId authorId = new AuthorId(TestUtils.getRandomId());
|
||||
byte[] publicKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
|
||||
Author author = new Author(authorId, AUTHORS[i], publicKey);
|
||||
forumEntries[i] =
|
||||
new ForumEntry(AUTHOR_IDS[i], PARENT_AUTHOR_IDS[i],
|
||||
new ForumItem(AUTHOR_IDS[i], PARENT_AUTHOR_IDS[i],
|
||||
AUTHORS[i], System.currentTimeMillis(), author,
|
||||
UNKNOWN);
|
||||
forumEntries[i].setLevel(LEVELS[i]);
|
||||
@@ -112,7 +110,7 @@ public class ForumActivityTest {
|
||||
@Test
|
||||
public void testNestedEntries() {
|
||||
ForumController mc = forumActivity.getController();
|
||||
List<ForumEntry> dummyData = getDummyData();
|
||||
List<ForumItem> dummyData = getDummyData();
|
||||
verify(mc, times(1)).loadItems(rc.capture());
|
||||
rc.getValue().onResult(dummyData);
|
||||
NestedForumAdapter adapter = forumActivity.getAdapter();
|
||||
|
||||
@@ -7,9 +7,9 @@ import org.briarproject.api.sharing.Shareable;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Blog extends BaseGroup implements Shareable {
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public abstract class BaseGroup {
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public abstract class NamedGroup extends BaseGroup {
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.api.sharing.Shareable;
|
||||
import org.briarproject.api.sync.Group;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Forum extends NamedGroup implements Shareable {
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.briarproject.api.forum;
|
||||
|
||||
import org.briarproject.api.clients.MessageTracker;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -22,8 +24,9 @@ public interface ForumManager extends MessageTracker {
|
||||
void removeForum(Forum f) throws DbException;
|
||||
|
||||
/** Creates a local forum post. */
|
||||
ForumPost createLocalPost(GroupId groupId, String text,
|
||||
@Nullable MessageId parentId) throws DbException;
|
||||
@CryptoExecutor
|
||||
ForumPost createLocalPost(GroupId groupId, String body, long timestamp,
|
||||
@Nullable MessageId parentId, LocalAuthor author);
|
||||
|
||||
/** Stores a local forum post. */
|
||||
ForumPostHeader addLocalPost(ForumPost p) throws DbException;
|
||||
|
||||
@@ -6,9 +6,9 @@ import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class PrivateGroup extends NamedGroup {
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.api.privategroup;
|
||||
import org.briarproject.api.clients.MessageTracker;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -21,8 +22,8 @@ public interface PrivateGroupManager extends MessageTracker {
|
||||
void removePrivateGroup(GroupId g) throws DbException;
|
||||
|
||||
/** Creates a local group message. */
|
||||
GroupMessage createLocalMessage(GroupId groupId, String text,
|
||||
@Nullable MessageId parentId) throws DbException;
|
||||
GroupMessage createLocalMessage(GroupId groupId, String body,
|
||||
long timestamp, @Nullable MessageId parentId, LocalAuthor author);
|
||||
|
||||
/** Stores (and sends) a local group message. */
|
||||
GroupMessageHeader addLocalMessage(GroupMessage p) throws DbException;
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.clients.BdfIncomingMessageHook;
|
||||
import org.briarproject.util.StringUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -64,20 +63,17 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
private final IdentityManager identityManager;
|
||||
private final ForumFactory forumFactory;
|
||||
private final ForumPostFactory forumPostFactory;
|
||||
private final Clock clock;
|
||||
private final List<RemoveForumHook> removeHooks;
|
||||
|
||||
@Inject
|
||||
ForumManagerImpl(DatabaseComponent db, IdentityManager identityManager,
|
||||
ClientHelper clientHelper, MetadataParser metadataParser,
|
||||
ForumFactory forumFactory, ForumPostFactory forumPostFactory,
|
||||
Clock clock) {
|
||||
ForumFactory forumFactory, ForumPostFactory forumPostFactory) {
|
||||
super(db, clientHelper, metadataParser);
|
||||
|
||||
this.identityManager = identityManager;
|
||||
this.forumFactory = forumFactory;
|
||||
this.forumPostFactory = forumPostFactory;
|
||||
this.clock = clock;
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveForumHook>();
|
||||
}
|
||||
|
||||
@@ -129,23 +125,9 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForumPost createLocalPost(final GroupId groupId,
|
||||
final String body, final @Nullable MessageId parentId)
|
||||
throws DbException {
|
||||
|
||||
LocalAuthor author;
|
||||
GroupCount count;
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
author = identityManager.getLocalAuthor(txn);
|
||||
count = getGroupCount(txn, groupId);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
long timestamp = clock.currentTimeMillis();
|
||||
timestamp = Math.max(timestamp, count.getLatestMsgTime());
|
||||
|
||||
public ForumPost createLocalPost(final GroupId groupId, final String body,
|
||||
final long timestamp, final @Nullable MessageId parentId,
|
||||
final LocalAuthor author) {
|
||||
ForumPost p;
|
||||
try {
|
||||
p = forumPostFactory
|
||||
|
||||
@@ -77,16 +77,13 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
|
||||
|
||||
@Override
|
||||
public GroupMessage createLocalMessage(GroupId groupId, String body,
|
||||
@Nullable MessageId parentId) throws DbException {
|
||||
|
||||
long timestamp = clock.currentTimeMillis();
|
||||
LocalAuthor author = identityManager.getLocalAuthor();
|
||||
long timestamp, @Nullable MessageId parentId, LocalAuthor author) {
|
||||
try {
|
||||
return groupMessageFactory
|
||||
.createGroupMessage(groupId, timestamp, parentId, author,
|
||||
body);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
throw new RuntimeException(e);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -86,4 +86,11 @@ public class StringUtils {
|
||||
public static String trim(String s) {
|
||||
return s.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is longer than maxLength
|
||||
*/
|
||||
public static boolean isTooLong(String s, int maxLength) {
|
||||
return toUtf8(s).length > maxLength;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user