Move post/message creation into clients

This way the forum and private group client do not need to keep track of
message timestamps themselves and do not need to interact with
post/message factories.
This commit is contained in:
Torsten Grote
2016-10-11 18:28:37 -03:00
parent 6db59ffce5
commit 7bf4aebdaf
12 changed files with 217 additions and 218 deletions

View File

@@ -2,45 +2,46 @@ package org.briarproject.android.privategroup.conversation;
import android.support.annotation.Nullable;
import org.briarproject.android.controller.handler.ResultExceptionHandler;
import org.briarproject.android.api.AndroidNotificationManager;
import org.briarproject.android.threaded.ThreadListControllerImpl;
import org.briarproject.api.FormatException;
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.LocalAuthor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.privategroup.GroupMessage;
import org.briarproject.api.privategroup.GroupMessageFactory;
import org.briarproject.api.privategroup.GroupMessageHeader;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupManager;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.identity.Author.Status.OURSELVES;
public class GroupControllerImpl
extends ThreadListControllerImpl<PrivateGroup, GroupMessageItem, GroupMessageHeader>
extends ThreadListControllerImpl<PrivateGroup, GroupMessageItem, GroupMessageHeader, GroupMessage>
implements GroupController {
private static final Logger LOG =
Logger.getLogger(GroupControllerImpl.class.getName());
@Inject
volatile GroupMessageFactory groupMessageFactory;
@Inject
volatile PrivateGroupManager privateGroupManager;
private final PrivateGroupManager privateGroupManager;
@Inject
GroupControllerImpl() {
super();
GroupControllerImpl(@DatabaseExecutor Executor dbExecutor,
LifecycleManager lifecycleManager,
@CryptoExecutor Executor cryptoExecutor,
PrivateGroupManager privateGroupManager, EventBus eventBus,
AndroidNotificationManager notificationManager) {
super(dbExecutor, lifecycleManager, cryptoExecutor, eventBus,
notificationManager);
this.privateGroupManager = privateGroupManager;
}
@Override
@@ -58,7 +59,6 @@ public class GroupControllerImpl
if (!pe.isLocal() && pe.getGroupId().equals(groupId)) {
LOG.info("Group message received, adding...");
final GroupMessageHeader fph = pe.getHeader();
updateNewestTimestamp(fph.getTimestamp());
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
@Override
public void run() {
@@ -97,59 +97,15 @@ public class GroupControllerImpl
}
@Override
public void send(final String body, @Nullable final MessageId parentId,
final ResultExceptionHandler<GroupMessageItem, DbException> handler) {
cryptoExecutor.execute(new Runnable() {
@Override
public void run() {
LOG.info("Create message...");
long timestamp = System.currentTimeMillis();
timestamp = Math.max(timestamp, newestTimeStamp.get());
GroupMessage gm;
try {
LocalAuthor a = identityManager.getLocalAuthor();
gm = groupMessageFactory.createGroupMessage(groupId,
timestamp, parentId, a, body);
} catch (GeneralSecurityException | FormatException e) {
throw new RuntimeException(e);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
handler.onException(e);
return;
}
bodyCache.put(gm.getMessage().getId(), body);
storeMessage(gm, handler);
}
});
protected GroupMessage createLocalMessage(GroupId g, String body,
@Nullable MessageId parentId) throws DbException {
return privateGroupManager.createLocalMessage(groupId, body, parentId);
}
private void storeMessage(final GroupMessage gm,
final ResultExceptionHandler<GroupMessageItem, DbException> handler) {
runOnDbThread(new Runnable() {
@Override
public void run() {
try {
LOG.info("Store message...");
long now = System.currentTimeMillis();
privateGroupManager.addLocalMessage(gm);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Storing message took " + duration + " ms");
GroupMessageHeader h = new GroupMessageHeader(groupId,
gm.getMessage().getId(), gm.getParent(),
gm.getMessage().getTimestamp(), gm.getAuthor(),
OURSELVES, true);
handler.onResult(buildItem(h));
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
handler.onException(e);
}
}
});
@Override
protected GroupMessageHeader addLocalMessage(GroupMessage message)
throws DbException {
return privateGroupManager.addLocalMessage(message);
}
@Override