Creator automatically joins the group after creating it

This commit is contained in:
Torsten Grote
2016-10-20 12:45:56 -02:00
parent e06726b2f9
commit 4f4f1956eb
7 changed files with 178 additions and 51 deletions

View File

@@ -11,4 +11,6 @@ interface Constants {
String KEY_AUTHOR_NAME = "authorName";
String KEY_AUTHOR_PUBLIC_KEY = "authorPublicKey";
// Messaging Group Metadata
String KEY_PREVIOUS_MSG_ID = "previousMsgId";
}

View File

@@ -3,16 +3,15 @@ package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.privategroup.GroupMessage;
import org.briarproject.api.privategroup.GroupMessageFactory;
import org.briarproject.api.privategroup.GroupMessageHeader;
import org.briarproject.api.privategroup.MessageType;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.privategroup.PrivateGroupManager;
@@ -21,13 +20,10 @@ 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.NotNull;
import org.jetbrains.annotations.Nullable;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -36,6 +32,12 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static org.briarproject.api.identity.Author.Status.OURSELVES;
import static org.briarproject.privategroup.Constants.KEY_AUTHOR_NAME;
import static org.briarproject.privategroup.Constants.KEY_AUTHOR_PUBLIC_KEY;
import static org.briarproject.privategroup.Constants.KEY_PREVIOUS_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_READ;
import static org.briarproject.privategroup.Constants.KEY_TIMESTAMP;
import static org.briarproject.privategroup.Constants.KEY_TYPE;
public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
PrivateGroupManager {
@@ -46,23 +48,15 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
StringUtils.fromHexString("5072697661746547726f75704d616e61"
+ "67657220627920546f727374656e2047"));
private final IdentityManager identityManager;
private final PrivateGroupFactory privateGroupFactory;
private final GroupMessageFactory groupMessageFactory;
private final Clock clock;
@Inject
PrivateGroupManagerImpl(ClientHelper clientHelper,
MetadataParser metadataParser, DatabaseComponent db,
IdentityManager identityManager,
PrivateGroupFactory privateGroupFactory,
GroupMessageFactory groupMessageFactory, Clock clock) {
PrivateGroupFactory privateGroupFactory) {
super(db, clientHelper, metadataParser);
this.identityManager = identityManager;
this.privateGroupFactory = privateGroupFactory;
this.groupMessageFactory = groupMessageFactory;
this.clock = clock;
}
@NotNull
@@ -72,36 +66,81 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
}
@Override
public GroupId addPrivateGroup(String name) throws DbException {
PrivateGroup group;
public void addPrivateGroup(PrivateGroup group,
GroupMessage newMemberMsg, GroupMessage joinMsg)
throws DbException {
Transaction txn = db.startTransaction(false);
try {
LocalAuthor a = identityManager.getLocalAuthor(txn);
group = privateGroupFactory.createPrivateGroup(name, a);
db.addGroup(txn, group.getGroup());
announceNewMember(txn, newMemberMsg);
joinPrivateGroup(txn, joinMsg);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return group.getId();
}
private void announceNewMember(Transaction txn, GroupMessage m)
throws DbException, FormatException {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TYPE, MessageType.NEW_MEMBER.getInt());
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
}
private void joinPrivateGroup(Transaction txn, GroupMessage m)
throws DbException, FormatException {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TYPE, MessageType.JOIN.getInt());
addMessageMetadata(meta, m, true);
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
trackOutgoingMessage(txn, m.getMessage());
setPreviousMsgId(txn, m.getMessage().getGroupId(),
m.getMessage().getId());
}
@Override
public void removePrivateGroup(GroupId g) throws DbException {
// TODO
}
@Override
public GroupMessage createLocalMessage(GroupId groupId, String body,
long timestamp, @Nullable MessageId parentId, LocalAuthor author) {
public MessageId getPreviousMsgId(GroupId g) throws DbException {
MessageId previousMsgId;
Transaction txn = db.startTransaction(true);
try {
return groupMessageFactory
.createGroupMessage(groupId, timestamp, parentId, author,
body);
previousMsgId = getPreviousMsgId(txn, g);
txn.setComplete();
} catch (FormatException e) {
throw new RuntimeException(e);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return previousMsgId;
}
private MessageId getPreviousMsgId(Transaction txn, GroupId g)
throws DbException, FormatException {
BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g);
byte[] previousMsgIdBytes = d.getOptionalRaw(KEY_PREVIOUS_MSG_ID);
if (previousMsgIdBytes == null) throw new DbException();
return new MessageId(previousMsgIdBytes);
}
private void setPreviousMsgId(Transaction txn, GroupId g,
MessageId previousMsgId) throws DbException, FormatException {
BdfDictionary d = BdfDictionary
.of(new BdfEntry(KEY_PREVIOUS_MSG_ID, previousMsgId));
clientHelper.mergeGroupMetadata(txn, g, d);
}
public long getMessageTimestamp(MessageId id) throws DbException {
try {
BdfDictionary d = clientHelper.getMessageMetadataAsDictionary(id);
return d.getLong(KEY_TIMESTAMP);
} catch (FormatException e) {
throw new DbException(e);
}
}
@@ -111,6 +150,8 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
Transaction txn = db.startTransaction(false);
try {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TYPE, MessageType.POST.getInt());
addMessageMetadata(meta, m, true);
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
trackOutgoingMessage(txn, m.getMessage());
txn.setComplete();
@@ -121,7 +162,7 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
}
return new GroupMessageHeader(m.getMessage().getGroupId(),
m.getMessage().getId(), m.getParent(),
m.getMessage().getTimestamp(), m.getAuthor(), OURSELVES, true);
m.getMessage().getTimestamp(), m.getMember(), OURSELVES, true);
}
@NotNull
@@ -198,7 +239,6 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
trackIncomingMessage(txn, m);
// TODO POST timestamp must be greater than the timestamps of the parent post, if any, and the member's previous message
// TODO JOIN timestamp must be equal to the timestamp of the new member message.
@@ -207,4 +247,12 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
return true;
}
private void addMessageMetadata(BdfDictionary meta, GroupMessage m,
boolean read) {
meta.put(KEY_TIMESTAMP, m.getMessage().getTimestamp());
meta.put(KEY_READ, read);
meta.put(KEY_AUTHOR_NAME, m.getMember().getName());
meta.put(KEY_AUTHOR_PUBLIC_KEY, m.getMember().getPublicKey());
}
}