From 106bacb4b1b273ee0c3f8975870f51856937c3b4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 16 Sep 2020 16:21:03 +0200 Subject: [PATCH] Add logic --- .../fedilabtube/client/APIResponse.java | 17 +- .../fedilabtube/client/PeertubeAPI.java | 108 +++++++- .../fedilabtube/client/entities/Instance.java | 244 ++++++++++++------ .../client/entities/InstanceParams.java | 77 ++++++ 4 files changed, 344 insertions(+), 102 deletions(-) create mode 100644 app/src/main/java/app/fedilab/fedilabtube/client/entities/InstanceParams.java diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java index 47178b4..c296154 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java @@ -40,7 +40,7 @@ public class APIResponse { private Error error = null; private String since_id, max_id; private List playlistForVideos; - private Instance instance; + private List instances; private String stringData; private int statusCode; @@ -130,13 +130,6 @@ public class APIResponse { } - public Instance getInstance() { - return instance; - } - - public void setInstance(Instance instance) { - this.instance = instance; - } public List getStatuses() { return statuses; @@ -169,4 +162,12 @@ public class APIResponse { public void setRelationships(List relationships) { this.relationships = relationships; } + + public List getInstances() { + return instances; + } + + public void setInstances(List instances) { + this.instances = instances; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 9631719..618c9af 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -50,6 +50,7 @@ import app.fedilab.fedilabtube.client.entities.ChannelCreation; import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.Instance; import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo; +import app.fedilab.fedilabtube.client.entities.InstanceParams; import app.fedilab.fedilabtube.client.entities.NodeInfo; import app.fedilab.fedilabtube.client.entities.Peertube; import app.fedilab.fedilabtube.client.entities.PeertubeAccountNotification; @@ -589,11 +590,41 @@ public class PeertubeAPI { * Get info on the current Instance *synchronously* * @return APIResponse */ - public APIResponse getInstance() { + public APIResponse getInstances(InstanceParams instanceParams) { + LinkedHashMap params = new LinkedHashMap<>(); + params.put("start", "0"); + params.put("count", "250"); + params.put("healthy", "true"); + params.put("signup", "true"); + if (instanceParams != null) { + if (instanceParams.getCategoriesOr() != null && instanceParams.getCategoriesOr().size() > 0) { + StringBuilder parameters = new StringBuilder(); + parameters.append("[]&"); + for (Integer category : instanceParams.getCategoriesOr()) + parameters.append("categoriesOr=").append(category).append("&"); + String strParam = parameters.toString(); + strParam = strParam.substring(0, strParam.length() - 1); + params.put("categoriesOr[]", strParam); + } + + if (instanceParams.getLanguagesOr() != null && instanceParams.getLanguagesOr().size() > 0) { + StringBuilder parameters = new StringBuilder(); + parameters.append("[]&"); + for (String language : instanceParams.getLanguagesOr()) + parameters.append("languagesOr=").append(language).append("&"); + String strParam = parameters.toString(); + strParam = strParam.substring(0, strParam.length() - 1); + params.put("languagesOr[]", strParam); + } + params.put("minUserQuota", instanceParams.getMinUserQuota()); + params.put("nsfwPolicy", instanceParams.getNsfwPolicy()); + } + try { - String response = new HttpsConnection(context).get(getAbsoluteUrl("/instance"), 30, null, prefKeyOauthTokenT); - Instance instanceEntity = parseInstance(new JSONObject(response)); - apiResponse.setInstance(instanceEntity); + String response = new HttpsConnection(context).get("https://instances.joinpeertube.org/api/v1/instances", 30, params, null); + JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); + List instances = parseInstances(jsonArray); + apiResponse.setInstances(instances); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { @@ -2024,6 +2055,30 @@ public class PeertubeAPI { return peertubes; } + + /** + * Parse json array response for instances + * + * @param jsonArray JSONArray + * @return List + */ + private List parseInstances(JSONArray jsonArray) { + List instances = new ArrayList<>(); + try { + int i = 0; + while (i < jsonArray.length()) { + JSONObject resobj = jsonArray.getJSONObject(i); + Instance instance = parseInstance(resobj); + i++; + instances.add(instance); + } + } catch (JSONException e) { + setDefaultError(e); + } + return instances; + } + + /** * Parse json response an unique instance * @@ -2034,11 +2089,48 @@ public class PeertubeAPI { Instance instance = new Instance(); try { - instance.setUri(resobj.getString("uri")); - instance.setTitle(resobj.getString("title")); - instance.setDescription(resobj.getString("description")); - instance.setEmail(resobj.getString("email")); + instance.setAutoBlacklistUserVideosEnabled(resobj.getBoolean("autoBlacklistUserVideosEnabled")); + if (resobj.has("categories")) { + JSONArray jsonArray = resobj.getJSONArray("categories"); + LinkedHashMap categories = new LinkedHashMap<>(); + int i = 0; + while (i < jsonArray.length()) { + categories.put(i, jsonArray.getInt(i)); + i++; + } + instance.setCategories(categories); + } + instance.setCountry(resobj.getString("country")); + instance.setCreatedAt(Helper.stringToDate(context, resobj.getString("createdAt"))); + instance.setDefaultNSFWPolicy(resobj.getString("defaultNSFWPolicy")); + instance.setHealth(resobj.getInt("health")); + instance.setId(resobj.getString("id")); + instance.setHost(resobj.getString("host")); + instance.setName(resobj.getString("name")); instance.setVersion(resobj.getString("version")); + instance.setShortDescription(resobj.getString("shortDescription")); + instance.setNSFW(resobj.getBoolean("isNSFW")); + instance.setSignupAllowed(resobj.getBoolean("signupAllowed")); + instance.setSupportsIPv6(resobj.getBoolean("supportsIPv6")); + instance.setTotalInstanceFollowers(resobj.getInt("totalInstanceFollowers")); + + instance.setTotalInstanceFollowing(resobj.getInt("totalInstanceFollowing")); + instance.setTotalLocalVideos(resobj.getInt("totalLocalVideos")); + instance.setTotalUsers(resobj.getInt("totalUsers")); + instance.setTotalVideos(resobj.getInt("totalVideos")); + instance.setUserVideoQuota(resobj.getString("userVideoQuota")); + + if (resobj.has("languages")) { + JSONArray jsonArray = resobj.getJSONArray("languages"); + LinkedHashMap languages = new LinkedHashMap<>(); + int i = 0; + while (i < jsonArray.length()) { + languages.put(i, jsonArray.getString(i)); + i++; + } + instance.setLanguages(languages); + } + } catch (JSONException e) { setDefaultError(e); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Instance.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Instance.java index 2992bc7..73f9555 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Instance.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Instance.java @@ -14,57 +14,177 @@ package app.fedilab.fedilabtube.client.entities; * You should have received a copy of the GNU General Public License along with TubeLab; if not, * see . */ -import java.util.HashMap; +import java.util.Date; +import java.util.LinkedHashMap; -@SuppressWarnings("unused") public class Instance { - private String uri; - private String title; - private String description; - private String email; + private boolean autoBlacklistUserVideosEnabled; + private LinkedHashMap categories; + private String country; + private Date createdAt; + private String defaultNSFWPolicy; + private int health; + private String host; + private String id; + private LinkedHashMap languages; + private String name; + private String shortDescription; + private boolean signupAllowed; + private boolean supportsIPv6; + private int totalInstanceFollowers; + private int totalInstanceFollowing; + private int totalLocalVideos; + private int totalUsers; + private int totalVideos; + private String userVideoQuota; private String version; - private boolean registration; - private boolean approval_required; - private Account contactAccount; - private int userCount; - private int statusCount; - private int domainCount; - private String thumbnail; + private boolean isNSFW; - private HashMap poll_limits; - public String getUri() { - return uri; + public boolean isAutoBlacklistUserVideosEnabled() { + return autoBlacklistUserVideosEnabled; } - public void setUri(String uri) { - this.uri = uri; + public void setAutoBlacklistUserVideosEnabled(boolean autoBlacklistUserVideosEnabled) { + this.autoBlacklistUserVideosEnabled = autoBlacklistUserVideosEnabled; } - public String getTitle() { - return title; + public LinkedHashMap getCategories() { + return categories; } - public void setTitle(String title) { - this.title = title; + public void setCategories(LinkedHashMap categories) { + this.categories = categories; } - public String getDescription() { - return description; + public String getCountry() { + return country; } - public void setDescription(String description) { - this.description = description; + public void setCountry(String country) { + this.country = country; } - public String getEmail() { - return email; + public Date getCreatedAt() { + return createdAt; } - public void setEmail(String email) { - this.email = email; + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public String getDefaultNSFWPolicy() { + return defaultNSFWPolicy; + } + + public void setDefaultNSFWPolicy(String defaultNSFWPolicy) { + this.defaultNSFWPolicy = defaultNSFWPolicy; + } + + public int getHealth() { + return health; + } + + public void setHealth(int health) { + this.health = health; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public LinkedHashMap getLanguages() { + return languages; + } + + public void setLanguages(LinkedHashMap languages) { + this.languages = languages; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getShortDescription() { + return shortDescription; + } + + public void setShortDescription(String shortDescription) { + this.shortDescription = shortDescription; + } + + public boolean isSignupAllowed() { + return signupAllowed; + } + + public void setSignupAllowed(boolean signupAllowed) { + this.signupAllowed = signupAllowed; + } + + public boolean isSupportsIPv6() { + return supportsIPv6; + } + + public void setSupportsIPv6(boolean supportsIPv6) { + this.supportsIPv6 = supportsIPv6; + } + + public int getTotalInstanceFollowers() { + return totalInstanceFollowers; + } + + public void setTotalInstanceFollowers(int totalInstanceFollowers) { + this.totalInstanceFollowers = totalInstanceFollowers; + } + + public int getTotalInstanceFollowing() { + return totalInstanceFollowing; + } + + public void setTotalInstanceFollowing(int totalInstanceFollowing) { + this.totalInstanceFollowing = totalInstanceFollowing; + } + + public int getTotalLocalVideos() { + return totalLocalVideos; + } + + public void setTotalLocalVideos(int totalLocalVideos) { + this.totalLocalVideos = totalLocalVideos; + } + + public int getTotalUsers() { + return totalUsers; + } + + public void setTotalUsers(int totalUsers) { + this.totalUsers = totalUsers; + } + + public int getTotalVideos() { + return totalVideos; + } + + public void setTotalVideos(int totalVideos) { + this.totalVideos = totalVideos; } public String getVersion() { @@ -75,67 +195,19 @@ public class Instance { this.version = version; } - public HashMap getPoll_limits() { - return poll_limits; + public boolean isNSFW() { + return isNSFW; } - public void setPoll_limits(HashMap poll_limits) { - this.poll_limits = poll_limits; + public void setNSFW(boolean NSFW) { + isNSFW = NSFW; } - public boolean isRegistration() { - return registration; + public String getUserVideoQuota() { + return userVideoQuota; } - public void setRegistration(boolean registration) { - this.registration = registration; - } - - public boolean isApproval_required() { - return approval_required; - } - - public void setApproval_required(boolean approval_required) { - this.approval_required = approval_required; - } - - public Account getContactAccount() { - return contactAccount; - } - - public void setContactAccount(Account contactAccount) { - this.contactAccount = contactAccount; - } - - public int getUserCount() { - return userCount; - } - - public void setUserCount(int userCount) { - this.userCount = userCount; - } - - public int getStatusCount() { - return statusCount; - } - - public void setStatusCount(int statusCount) { - this.statusCount = statusCount; - } - - public int getDomainCount() { - return domainCount; - } - - public void setDomainCount(int domainCount) { - this.domainCount = domainCount; - } - - public String getThumbnail() { - return thumbnail; - } - - public void setThumbnail(String thumbnail) { - this.thumbnail = thumbnail; + public void setUserVideoQuota(String userVideoQuota) { + this.userVideoQuota = userVideoQuota; } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/InstanceParams.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/InstanceParams.java new file mode 100644 index 0000000..ffbfc8b --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/InstanceParams.java @@ -0,0 +1,77 @@ +package app.fedilab.fedilabtube.client.entities; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * 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. + * + * TubeLab 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 TubeLab; if not, + * see . */ + +import java.util.List; + +public class InstanceParams { + + private boolean healthy = true; + private boolean signup = true; + private List categoriesOr; + private String nsfwPolicy = "do_not_list"; + private List languagesOr; + private String minUserQuota = "5000000000"; + + + public boolean isHealthy() { + return healthy; + } + + public void setHealthy(boolean healthy) { + this.healthy = healthy; + } + + public boolean isSignup() { + return signup; + } + + public void setSignup(boolean signup) { + this.signup = signup; + } + + public List getCategoriesOr() { + return categoriesOr; + } + + public void setCategoriesOr(List categoriesOr) { + this.categoriesOr = categoriesOr; + } + + public String getNsfwPolicy() { + return nsfwPolicy; + } + + public void setNsfwPolicy(String nsfwPolicy) { + this.nsfwPolicy = nsfwPolicy; + } + + + public String getMinUserQuota() { + return minUserQuota; + } + + public void setMinUserQuota(String minUserQuota) { + this.minUserQuota = minUserQuota; + } + + public List getLanguagesOr() { + return languagesOr; + } + + public void setLanguagesOr(List languagesOr) { + this.languagesOr = languagesOr; + } +}