diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index f11f990c7..0375aa73b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -54,8 +54,10 @@ import fr.gouv.etalab.mastodon.client.Entities.Error; import fr.gouv.etalab.mastodon.client.Entities.Filters; import fr.gouv.etalab.mastodon.client.Entities.HowToVideo; import fr.gouv.etalab.mastodon.client.Entities.Instance; +import fr.gouv.etalab.mastodon.client.Entities.InstanceNodeInfo; import fr.gouv.etalab.mastodon.client.Entities.InstanceSocial; import fr.gouv.etalab.mastodon.client.Entities.Mention; +import fr.gouv.etalab.mastodon.client.Entities.NodeInfo; import fr.gouv.etalab.mastodon.client.Entities.Notification; import fr.gouv.etalab.mastodon.client.Entities.Peertube; import fr.gouv.etalab.mastodon.client.Entities.Relationship; @@ -162,6 +164,85 @@ public class API { APIError = null; } + public InstanceNodeInfo getNodeInfo(String domain){ + String response; + InstanceNodeInfo instanceNodeInfo = new InstanceNodeInfo(); + try { + response = new HttpsConnection(context).get("https://" + domain + "/.well-known/nodeinfo", 30, null, null); + JSONArray jsonArray = new JSONObject(response).getJSONArray("links"); + ArrayList nodeInfos = new ArrayList<>(); + try { + int i = 0; + while (i < jsonArray.length() ){ + + JSONObject resobj = jsonArray.getJSONObject(i); + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setHref(resobj.getString("href")); + nodeInfo.setRel(resobj.getString("rel")); + i++; + nodeInfos.add(nodeInfo); + } + if( nodeInfos.size() > 0){ + NodeInfo nodeInfo = nodeInfos.get(nodeInfos.size()-1); + response = new HttpsConnection(context).get(nodeInfo.getHref(), 30, null, null); + JSONObject resobj = new JSONObject(response); + JSONObject jsonObject = resobj.getJSONObject("software"); + String name = "MASTODON"; + if( jsonObject.getString("name") != null ){ + switch (jsonObject.getString("name").toUpperCase()){ + case "PEERTUBE": + name = "PEERTUBE"; + break; + case "PLEROMA": + name = "PLEROMA"; + break; + case "FRIENDICA": + name = "GNU"; + break; + } + } + instanceNodeInfo.setName(name); + instanceNodeInfo.setVersion(jsonObject.getString("version")); + instanceNodeInfo.setOpenRegistrations(resobj.getBoolean("openRegistrations")); + } + } catch (JSONException e) { + setDefaultError(e); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (HttpsConnection.HttpsConnectionException e) { + try { + response = new HttpsConnection(context).get("https://" + domain + "/api/v1/instance", 30, null, null); + JSONObject jsonObject = new JSONObject(response); + instanceNodeInfo.setName("MASTODON"); + instanceNodeInfo.setVersion(jsonObject.getString("version")); + instanceNodeInfo.setOpenRegistrations(true); + } catch (IOException e1) { + e1.printStackTrace(); + } catch (NoSuchAlgorithmException e1) { + e1.printStackTrace(); + } catch (KeyManagementException e1) { + e1.printStackTrace(); + } catch (HttpsConnection.HttpsConnectionException e1) { + e1.printStackTrace(); + } catch (JSONException e1) { + instanceNodeInfo.setName("GNU"); + instanceNodeInfo.setVersion("unknown"); + instanceNodeInfo.setOpenRegistrations(true); + e1.printStackTrace(); + } + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return instanceNodeInfo; + } + + public API(Context context, String instance, String token) { this.context = context; if( context == null) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceNodeInfo.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceNodeInfo.java new file mode 100644 index 000000000..abc6e5aa9 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/InstanceNodeInfo.java @@ -0,0 +1,46 @@ +/* Copyright 2019 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.client.Entities; + +public class InstanceNodeInfo { + + private String name; + private String version; + private boolean openRegistrations; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public boolean isOpenRegistrations() { + return openRegistrations; + } + + public void setOpenRegistrations(boolean openRegistrations) { + this.openRegistrations = openRegistrations; + } +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/NodeInfo.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/NodeInfo.java new file mode 100644 index 000000000..388e21086 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/NodeInfo.java @@ -0,0 +1,36 @@ +/* Copyright 2019 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.client.Entities; + +public class NodeInfo { + private String rel; + private String href; + + public String getRel() { + return rel; + } + + public void setRel(String rel) { + this.rel = rel; + } + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } +}