From a0dbe115aaf402f0ff9a5a1a84316af449e690cc Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Wed, 11 Jan 2017 13:20:24 +1030 Subject: [PATCH] Add Java API for managing subscriptions --- .../servaldna/ServalDClient.java | 11 ++++- .../servaldna/meshmb/MeshMBCommon.java | 9 ++-- .../servaldna/meshmb/MeshMBSubscription.java | 21 +++++++++ .../meshmb/MeshMBSubscriptionList.java | 44 +++++++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscription.java create mode 100644 java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscriptionList.java diff --git a/java-api/src/org/servalproject/servaldna/ServalDClient.java b/java-api/src/org/servalproject/servaldna/ServalDClient.java index 7609723e..b91e29db 100644 --- a/java-api/src/org/servalproject/servaldna/ServalDClient.java +++ b/java-api/src/org/servalproject/servaldna/ServalDClient.java @@ -25,6 +25,7 @@ import org.servalproject.servaldna.keyring.KeyringCommon; import org.servalproject.servaldna.keyring.KeyringIdentity; import org.servalproject.servaldna.keyring.KeyringIdentityList; import org.servalproject.servaldna.meshmb.MeshMBCommon; +import org.servalproject.servaldna.meshmb.MeshMBSubscriptionList; import org.servalproject.servaldna.meshmb.MessagePlyList; import org.servalproject.servaldna.meshms.MeshMSCommon; import org.servalproject.servaldna.meshms.MeshMSConversationList; @@ -204,14 +205,20 @@ public class ServalDClient implements ServalDHttpConnectionFactory { return list; } - public int meshmbFollow(SigningKey id, SigningKey peer) throws ServalDInterfaceException, IOException { + public int meshmbFollow(Subscriber id, SigningKey peer) throws ServalDInterfaceException, IOException { return MeshMBCommon.follow(this, id, peer); } - public int meshmbIgnore(SigningKey id, SigningKey peer) throws ServalDInterfaceException, IOException { + public int meshmbIgnore(Subscriber id, SigningKey peer) throws ServalDInterfaceException, IOException { return MeshMBCommon.ignore(this, id, peer); } + public MeshMBSubscriptionList meshmbSubscriptions(Subscriber identity) throws IOException, ServalDInterfaceException { + MeshMBSubscriptionList list = new MeshMBSubscriptionList(this, identity); + list.connect(); + return list; + } + // interface ServalDHttpConnectionFactory public HttpURLConnection newServalDHttpConnection(String path) throws ServalDInterfaceException, IOException { diff --git a/java-api/src/org/servalproject/servaldna/meshmb/MeshMBCommon.java b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBCommon.java index 5d55300c..bbd1ea97 100644 --- a/java-api/src/org/servalproject/servaldna/meshmb/MeshMBCommon.java +++ b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBCommon.java @@ -5,6 +5,7 @@ import org.servalproject.servaldna.ServalDClient; import org.servalproject.servaldna.ServalDHttpConnectionFactory; import org.servalproject.servaldna.ServalDInterfaceException; import org.servalproject.servaldna.SigningKey; +import org.servalproject.servaldna.Subscriber; import java.io.IOException; import java.net.HttpURLConnection; @@ -27,15 +28,15 @@ public class MeshMBCommon { return conn.getResponseCode(); } - public static int ignore(ServalDHttpConnectionFactory connector, SigningKey id, SigningKey peer) throws ServalDInterfaceException, IOException { - HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.toHex() + "/follow/" + peer.toHex()); + public static int ignore(ServalDHttpConnectionFactory connector, Subscriber id, SigningKey peer) throws ServalDInterfaceException, IOException { + HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.signingKey.toHex() + "/follow/" + peer.toHex()); conn.setRequestMethod("POST"); conn.connect(); return conn.getResponseCode(); } - public static int follow(ServalDHttpConnectionFactory connector, SigningKey id, SigningKey peer) throws ServalDInterfaceException, IOException { - HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.toHex() + "/ignore/" + peer.toHex()); + public static int follow(ServalDHttpConnectionFactory connector, Subscriber id, SigningKey peer) throws ServalDInterfaceException, IOException { + HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.signingKey.toHex() + "/ignore/" + peer.toHex()); conn.setRequestMethod("POST"); conn.connect(); return conn.getResponseCode(); diff --git a/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscription.java b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscription.java new file mode 100644 index 00000000..ad53995e --- /dev/null +++ b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscription.java @@ -0,0 +1,21 @@ +package org.servalproject.servaldna.meshmb; + +import org.servalproject.servaldna.BundleId; +import org.servalproject.servaldna.SigningKey; + +/** + * Created by jeremy on 11/01/17. + */ +public class MeshMBSubscription { + public final SigningKey id; + public final String name; + public final long timestamp; + public final String lastMessage; + + public MeshMBSubscription(SigningKey id, String name, long timestamp, String lastMessage){ + this.id = id; + this.name = name; + this.lastMessage = lastMessage; + this.timestamp = timestamp; + } +} diff --git a/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscriptionList.java b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscriptionList.java new file mode 100644 index 00000000..1dd8f952 --- /dev/null +++ b/java-api/src/org/servalproject/servaldna/meshmb/MeshMBSubscriptionList.java @@ -0,0 +1,44 @@ +package org.servalproject.servaldna.meshmb; + +import org.servalproject.json.JSONTableScanner; +import org.servalproject.servaldna.AbstractJsonList; +import org.servalproject.servaldna.ServalDHttpConnectionFactory; +import org.servalproject.servaldna.ServalDInterfaceException; +import org.servalproject.servaldna.SigningKey; +import org.servalproject.servaldna.Subscriber; + +import java.io.IOException; +import java.util.Map; + +/** + * Created by jeremy on 11/01/17. + */ + +public class MeshMBSubscriptionList extends AbstractJsonList { + + public final Subscriber identity; + + public MeshMBSubscriptionList(ServalDHttpConnectionFactory httpConnector, Subscriber identity){ + super(httpConnector, new JSONTableScanner() + .addColumn("id", SigningKey.class) + .addColumn("name", String.class) + .addColumn("timestamp", Long.class) + .addColumn("last_message", String.class) + ); + this.identity = identity; + } + @Override + protected String getUrl() { + return "/restful/meshmb/" + identity.signingKey.toHex() + "/feedlist.json"; + } + + @Override + protected MeshMBSubscription factory(Map row, long rowCount) throws ServalDInterfaceException { + return new MeshMBSubscription( + (SigningKey) row.get("id"), + (String) row.get("name"), + (Long) row.get("timestamp"), + (String) row.get("last_message") + ); + } +}