From 008147248937cfc2cfaa2de23aa3adbbc06ce883 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 11 Dec 2018 17:25:29 +0000 Subject: [PATCH] Add method for querying contact's client minor version. --- .../versioning/ClientVersioningManager.java | 7 ++++ .../ClientVersioningManagerImpl.java | 38 +++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java b/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java index 00b3d672c..76b261d1b 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/versioning/ClientVersioningManager.java @@ -38,6 +38,13 @@ public interface ClientVersioningManager { Visibility getClientVisibility(Transaction txn, ContactId contactId, ClientId clientId, int majorVersion) throws DbException; + /** + * Returns the minor version of the given client that is supported by the + * given contact, or -1 if the contact does not support the client. + */ + int getClientMinorVersion(Transaction txn, ContactId contactId, + ClientId clientId, int majorVersion) throws DbException; + interface ClientVersioningHook { void onClientVisibilityChanging(Transaction txn, Contact c, diff --git a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java index a7a6ac312..ccc7b41bb 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/versioning/ClientVersioningManagerImpl.java @@ -89,14 +89,9 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client, public Visibility getClientVisibility(Transaction txn, ContactId contactId, ClientId clientId, int majorVersion) throws DbException { try { - Contact contact = db.getContact(txn, contactId); - Group g = getContactGroup(contact); - // Contact may be in the process of being added or removed, so - // contact group may not exist - if (!db.containsGroup(txn, g.getId())) return INVISIBLE; - LatestUpdates latest = findLatestUpdates(txn, g.getId()); + LatestUpdates latest = findLatestUpdates(txn, contactId); + if (latest == null || latest.remote == null) return INVISIBLE; 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 visibilities = @@ -110,6 +105,24 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client, } } + @Override + public int getClientMinorVersion(Transaction txn, ContactId contactId, + ClientId clientId, int majorVersion) throws DbException { + try { + LatestUpdates latest = findLatestUpdates(txn, contactId); + if (latest == null || latest.remote == null) return -1; + Update remoteUpdate = loadUpdate(txn, latest.remote.messageId); + ClientMajorVersion cv = + new ClientMajorVersion(clientId, majorVersion); + for (ClientState remote : remoteUpdate.states) { + if (remote.majorVersion.equals(cv)) return remote.minorVersion; + } + return -1; + } catch (FormatException e) { + throw new DbException(e); + } + } + @Override public void createLocalState(Transaction txn) throws DbException { if (db.containsGroup(txn, localGroup.getId())) return; @@ -336,6 +349,17 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client, MAJOR_VERSION, c); } + @Nullable + private LatestUpdates findLatestUpdates(Transaction txn, ContactId c) + throws DbException, FormatException { + Contact contact = db.getContact(txn, c); + Group g = getContactGroup(contact); + // Contact may be in the process of being added or removed, so + // contact group may not exist + if (!db.containsGroup(txn, g.getId())) return null; + return findLatestUpdates(txn, g.getId()); + } + private LatestUpdates findLatestUpdates(Transaction txn, GroupId g) throws DbException, FormatException { Map metadata =