Mark relationship visible when syncing group with peer.

This commit is contained in:
akwizgran
2016-11-11 11:29:55 +00:00
parent ade7e50f65
commit 98cf6b5bba
2 changed files with 33 additions and 7 deletions

View File

@@ -443,21 +443,26 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(txn, g); BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(txn, g);
BdfList members = meta.getList(GROUP_KEY_MEMBERS); BdfList members = meta.getList(GROUP_KEY_MEMBERS);
Visibility v = INVISIBLE; Visibility v = INVISIBLE;
boolean foundMember = false; boolean foundMember = false, changed = false;
for (int i = 0 ; i < members.size(); i++) { for (int i = 0 ; i < members.size(); i++) {
BdfDictionary d = members.getDictionary(i); BdfDictionary d = members.getDictionary(i);
AuthorId memberId = new AuthorId(d.getRaw(KEY_MEMBER_ID)); AuthorId memberId = new AuthorId(d.getRaw(KEY_MEMBER_ID));
if (a.equals(memberId)) { if (a.equals(memberId)) {
foundMember = true; foundMember = true;
Visibility vOld = getVisibility(d); // Don't update the visibility if the contact is already visible
if (vOld != INVISIBLE) throw new ProtocolStateException(); if (getVisibility(d) == INVISIBLE) {
v = byContact ? REVEALED_BY_CONTACT : REVEALED_BY_US; changed = true;
d.put(GROUP_KEY_VISIBILITY, v.getInt()); v = byContact ? REVEALED_BY_CONTACT : REVEALED_BY_US;
d.put(GROUP_KEY_VISIBILITY, v.getInt());
}
break;
} }
} }
if (!foundMember) throw new ProtocolStateException(); if (!foundMember) throw new ProtocolStateException();
clientHelper.mergeGroupMetadata(txn, g, meta); if (changed) {
txn.attach(new ContactRelationshipRevealedEvent(g, v)); clientHelper.mergeGroupMetadata(txn, g, meta);
txn.attach(new ContactRelationshipRevealedEvent(g, v));
}
} }
@Override @Override

View File

@@ -3,6 +3,8 @@ package org.briarproject.privategroup.invitation;
import org.briarproject.api.FormatException; import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.ProtocolStateException; import org.briarproject.api.clients.ProtocolStateException;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction; import org.briarproject.api.db.Transaction;
@@ -179,6 +181,7 @@ class PeerProtocolEngine extends AbstractProtocolEngine<PeerSession> {
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); // Invalid group metadata throw new DbException(e); // Invalid group metadata
} }
// The relationship is already marked visible to the group
// Move to the BOTH_JOINED state // Move to the BOTH_JOINED state
return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(), return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(),
sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(), sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(),
@@ -228,6 +231,12 @@ class PeerProtocolEngine extends AbstractProtocolEngine<PeerSession> {
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); // Invalid group metadata throw new DbException(e); // Invalid group metadata
} }
try {
// Mark the relationship visible to the group, revealed by contact
relationshipRevealed(txn, s, true);
} catch (FormatException e) {
throw new DbException(e); // Invalid group metadata
}
// Move to the BOTH_JOINED state // Move to the BOTH_JOINED state
return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(), return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(),
sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(), sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(),
@@ -254,6 +263,8 @@ class PeerProtocolEngine extends AbstractProtocolEngine<PeerSession> {
Message sent = sendJoinMessage(txn, s, false); Message sent = sendJoinMessage(txn, s, false);
// Start syncing the private group with the contact // Start syncing the private group with the contact
syncPrivateGroupWithContact(txn, s, true); syncPrivateGroupWithContact(txn, s, true);
// Mark the relationship visible to the group, revealed by contact
relationshipRevealed(txn, s, true);
// Move to the BOTH_JOINED state // Move to the BOTH_JOINED state
return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(), return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(),
sent.getId(), m.getId(), sent.getTimestamp(), BOTH_JOINED); sent.getId(), m.getId(), sent.getTimestamp(), BOTH_JOINED);
@@ -266,6 +277,8 @@ class PeerProtocolEngine extends AbstractProtocolEngine<PeerSession> {
return abort(txn, s); return abort(txn, s);
// Start syncing the private group with the contact // Start syncing the private group with the contact
syncPrivateGroupWithContact(txn, s, true); syncPrivateGroupWithContact(txn, s, true);
// Mark the relationship visible to the group, revealed by us
relationshipRevealed(txn, s, false);
// Move to the BOTH_JOINED state // Move to the BOTH_JOINED state
return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(), return new PeerSession(s.getContactGroupId(), s.getPrivateGroupId(),
s.getLastLocalMessageId(), m.getId(), s.getLocalTimestamp(), s.getLastLocalMessageId(), m.getId(), s.getLocalTimestamp(),
@@ -321,4 +334,12 @@ class PeerProtocolEngine extends AbstractProtocolEngine<PeerSession> {
sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(), sent.getId(), s.getLastRemoteMessageId(), sent.getTimestamp(),
ERROR); ERROR);
} }
private void relationshipRevealed(Transaction txn, PeerSession s,
boolean byContact) throws DbException, FormatException {
ContactId contactId = getContactId(txn, s.getContactGroupId());
Contact contact = db.getContact(txn, contactId);
privateGroupManager.relationshipRevealed(txn, s.getPrivateGroupId(),
contact.getAuthor().getId(), byContact);
}
} }