Add a method for getting a client's visibility.

This commit is contained in:
akwizgran
2018-04-13 16:51:44 +01:00
parent cc6fed0298
commit a9f77f0f90
2 changed files with 30 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package org.briarproject.bramble.api.sync;
import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
@@ -34,6 +35,13 @@ public interface ClientVersioningManager {
void registerClientVersioningHook(ClientId clientId, int clientVersion,
ClientVersioningHook hook);
/**
* Returns the visibility of the given client with respect to the given
* contact.
*/
Visibility getClientVisibility(Transaction txn, ContactId contactId,
ClientId clientId, int clientVersion) throws DbException;
interface ClientVersioningHook {
void onClientVisibilityChanging(Transaction txn, Contact c,

View File

@@ -89,6 +89,28 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
hooks.put(new ClientVersion(clientId, clientVersion), hook);
}
@Override
public Visibility getClientVisibility(Transaction txn,
ContactId contactId, ClientId clientId, int clientVersion)
throws DbException {
try {
Contact contact = db.getContact(txn, contactId);
Group g = getContactGroup(contact);
LatestUpdates latest = findLatestUpdates(txn, g.getId());
if (latest.local == null) throw new DbException();
if (latest.remote == null) return INVISIBLE;
Update localUpdate = loadUpdate(txn, latest.local.messageId);
Update remoteUpdate = loadUpdate(txn, latest.remote.messageId);
Map<ClientVersion, Visibility> visibilities =
getVisibilities(localUpdate.states, remoteUpdate.states);
ClientVersion cv = new ClientVersion(clientId, clientVersion);
Visibility v = visibilities.get(cv);
return v == null ? INVISIBLE : v;
} catch (FormatException e) {
throw new DbException(e);
}
}
@Override
public void createLocalState(Transaction txn) throws DbException {
if (db.containsGroup(txn, localGroup.getId())) return;