Commit 3df4f6ce authored by Thomas's avatar Thomas
Browse files

Some cleaning

parent 5042dba8
......@@ -503,7 +503,7 @@ public class API {
* @param jsonArray JSONArray
* @return List<Status>
*/
private static List<Status> parseStatuses(Context context, filterFor action, JSONArray jsonArray) {
private static List<Status> parseStatuses(Context context, JSONArray jsonArray) {
List<Status> statuses = new ArrayList<>();
try {
......@@ -1267,7 +1267,7 @@ public class API {
if (!resobj.isNull("action_taken_by_account")) {
report.setAction_taken_by_account(parseAccountAdminResponse(context, resobj.getJSONObject("action_taken_by_account")));
}
report.setStatuses(parseStatuses(context, null, resobj.getJSONArray("statuses")));
report.setStatuses(parseStatuses(context, resobj.getJSONArray("statuses")));
} catch (Exception e) {
e.printStackTrace();
}
......@@ -1786,7 +1786,7 @@ public class API {
return statuses;
}
private List<IdentityProof> parseIdentityProof(Context context, JSONArray jsonArray) {
private List<IdentityProof> parseIdentityProof(JSONArray jsonArray) {
List<IdentityProof> identityProofs = new ArrayList<>();
try {
int i = 0;
......@@ -2758,7 +2758,7 @@ public class API {
try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/statuses", accountId)), 10, params, prefKeyOauthTokenT);
statuses = parseStatuses(context, filterFor.STATUSES_ACCOUNT, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
} catch (HttpsConnection.HttpsConnectionException e) {
......@@ -2913,7 +2913,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUr2l(String.format("/status/%s/replies", statusId)), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -3019,7 +3019,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUrl("/timelines/direct"), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -3124,7 +3124,7 @@ public class API {
if (since_id == null) {
statuses = parseStatusesForCache(context, new JSONArray(response));
} else {
statuses = parseStatuses(context, filterFor.HOME, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
}
} catch (UnknownHostException e) {
if (since_id == null) {
......@@ -3153,7 +3153,7 @@ public class API {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/identity_proofs", userId)), 10, null, prefKeyOauthTokenT);
identityProofs = parseIdentityProof(context, new JSONArray(response));
identityProofs = parseIdentityProof(new JSONArray(response));
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e) {
......@@ -3214,7 +3214,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUrlRemote(remoteInstance, "/timelines/public/"), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, filterFor.PUBLIC, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException | HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
......@@ -3563,7 +3563,7 @@ public class API {
String response = httpsConnection.get(url, 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, local ? filterFor.LOCAL : filterFor.PUBLIC, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
......@@ -3595,7 +3595,7 @@ public class API {
String response = httpsConnection.get("https://toot.fedilab.app/api/v1/timelines/tag/fedilab", 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
List<Status> tmp_status = parseStatuses(context, null, new JSONArray(response));
List<Status> tmp_status = parseStatuses(context, new JSONArray(response));
if (tmp_status.size() > 0) {
for (Status status : tmp_status) {
if (status.getAccount().getAcct().equals("fedilab")) {
......@@ -3651,7 +3651,7 @@ public class API {
String response = httpsConnection.get(url, 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -3831,7 +3831,7 @@ public class API {
response = httpsConnection.get(getAbsoluteUrlRemote(instance, String.format("/timelines/tag/%s", query)), 10, params, null);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
......@@ -4058,7 +4058,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUrl("/favourites"), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -4108,7 +4108,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUrl("/bookmarks"), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -4604,6 +4604,7 @@ public class API {
jsonObject.addProperty("sensitive", Boolean.toString(status.isSensitive()));
if (status.getSpoiler_text() != null)
jsonObject.addProperty("spoiler_text", status.getSpoiler_text());
if (status.getPoll() != null) {
JsonObject poll = new JsonObject();
JsonArray options = new JsonArray();
......@@ -5252,7 +5253,7 @@ public class API {
String response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/list/%s", list_id)), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
statuses = parseStatuses(context, null, new JSONArray(response));
statuses = parseStatuses(context, new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
......@@ -5482,7 +5483,7 @@ public class API {
Results results = new Results();
try {
results.setAccounts(parseAccountResponse(resobj.getJSONArray("accounts")));
results.setStatuses(parseStatuses(context, null, resobj.getJSONArray("statuses")));
results.setStatuses(parseStatuses(context, resobj.getJSONArray("statuses")));
results.setTrends(parseTrends(resobj.getJSONArray("hashtags")));
results.setHashtags(parseTags(resobj.getJSONArray("hashtags")));
} catch (JSONException e) {
......@@ -6073,8 +6074,8 @@ public class API {
app.fedilab.android.client.Entities.Context context = new app.fedilab.android.client.Entities.Context();
try {
context.setAncestors(parseStatuses(this.context, null, jsonObject.getJSONArray("ancestors")));
context.setDescendants(parseStatuses(this.context, null, jsonObject.getJSONArray("descendants")));
context.setAncestors(parseStatuses(this.context, jsonObject.getJSONArray("ancestors")));
context.setDescendants(parseStatuses(this.context, jsonObject.getJSONArray("descendants")));
} catch (JSONException e) {
setDefaultError(e);
}
......@@ -6167,13 +6168,6 @@ public class API {
return "https://communitywiki.org/trunk/api/v1" + action;
}
enum filterFor {
STATUSES_ACCOUNT,
HOME,
LOCAL,
PUBLIC
}
public enum searchType {
TAGS,
STATUSES,
......
......@@ -125,7 +125,7 @@ public class PeertubeAPI {
* @param resobj JSONObject
* @return Peertube
*/
private static PeertubeNotification parsePeertubeNotifications(Context context, JSONObject resobj) {
private static PeertubeNotification parsePeertubeNotifications(JSONObject resobj) {
PeertubeNotification peertubeNotification = new PeertubeNotification();
try {
peertubeNotification.setId(resobj.get("id").toString());
......@@ -230,7 +230,7 @@ public class PeertubeAPI {
* @param resobj JSONObject
* @return Peertube
*/
private static Peertube parsePeertube(Context context, JSONObject resobj) {
private static Peertube parsePeertube(JSONObject resobj) {
Peertube peertube = new Peertube();
if (resobj.has("video")) {
try {
......@@ -248,9 +248,9 @@ public class PeertubeAPI {
peertube.setEmbedPath(resobj.get("embedPath").toString());
peertube.setPreviewPath(resobj.get("previewPath").toString());
peertube.setThumbnailPath(resobj.get("thumbnailPath").toString());
peertube.setAccount(parseAccountResponsePeertube(context, resobj.getJSONObject("account")));
peertube.setAccount(parseAccountResponsePeertube(resobj.getJSONObject("account")));
try {
peertube.setChannel(parseAccountResponsePeertube(context, resobj.getJSONObject("channel")));
peertube.setChannel(parseAccountResponsePeertube(resobj.getJSONObject("channel")));
} catch (Exception ignored) {
}
peertube.setView(Integer.parseInt(resobj.get("views").toString()));
......@@ -298,7 +298,7 @@ public class PeertubeAPI {
* @param resobj JSONObject
* @return Peertube
*/
private static Peertube parseSinglePeertube(Context context, String instance, JSONObject resobj) {
private static Peertube parseSinglePeertube(String instance, JSONObject resobj) {
Peertube peertube = new Peertube();
try {
peertube.setId(resobj.get("id").toString());
......@@ -316,7 +316,7 @@ public class PeertubeAPI {
peertube.setCommentsEnabled(Boolean.parseBoolean(resobj.get("commentsEnabled").toString()));
peertube.setDislike(Integer.parseInt(resobj.get("dislikes").toString()));
peertube.setDuration(Integer.parseInt(resobj.get("duration").toString()));
peertube.setAccount(parseAccountResponsePeertube(context, resobj.getJSONObject("account")));
peertube.setAccount(parseAccountResponsePeertube(resobj.getJSONObject("account")));
List<String> tags = new ArrayList<>();
try {
JSONArray tagsA = resobj.getJSONArray("tags");
......@@ -328,7 +328,7 @@ public class PeertubeAPI {
} catch (Exception ignored) {
}
try {
peertube.setChannel(parseAccountResponsePeertube(context, resobj.getJSONObject("channel")));
peertube.setChannel(parseAccountResponsePeertube(resobj.getJSONObject("channel")));
} catch (Exception ignored) {
}
peertube.setSensitive(Boolean.parseBoolean(resobj.get("nsfw").toString()));
......@@ -404,7 +404,7 @@ public class PeertubeAPI {
playlist.setLocal(resobj.getBoolean("isLocal"));
playlist.setVideoChannelId(resobj.getString("videoChannel"));
playlist.setThumbnailPath(resobj.getString("thumbnailPath"));
playlist.setOwnerAccount(parseAccountResponsePeertube(context, resobj.getJSONObject("ownerAccount")));
playlist.setOwnerAccount(parseAccountResponsePeertube(resobj.getJSONObject("ownerAccount")));
playlist.setVideosLength(resobj.getInt("videosLength"));
try {
LinkedHashMap<Integer, String> type = new LinkedHashMap<>();
......@@ -430,7 +430,7 @@ public class PeertubeAPI {
* @param accountObject JSONObject
* @return Account
*/
private static Account parseAccountResponsePeertube(Context context, JSONObject accountObject) {
private static Account parseAccountResponsePeertube(JSONObject accountObject) {
Account account = new Account();
try {
account.setId(accountObject.get("id").toString());
......@@ -657,7 +657,7 @@ public class PeertubeAPI {
try {
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT);
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
account = parseAccountResponsePeertube(context, accountObject);
account = parseAccountResponsePeertube( accountObject);
if (social != null) {
account.setSocial(social.toUpperCase());
}
......@@ -688,7 +688,7 @@ public class PeertubeAPI {
try {
response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/users/me"), 60, null, targetedAccount.getToken());
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
account = parseAccountResponsePeertube(context, accountObject);
account = parseAccountResponsePeertube(accountObject);
if (social != null) {
account.setSocial(social.toUpperCase());
}
......@@ -782,7 +782,7 @@ public class PeertubeAPI {
account = new Account();
try {
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(String.format("/accounts/%s", accountId)), 60, null, prefKeyOauthTokenT);
account = parseAccountResponsePeertube(context, new JSONObject(response));
account = parseAccountResponsePeertube(new JSONObject(response));
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
setError(e.getStatusCode(), e);
......@@ -1059,9 +1059,9 @@ public class PeertubeAPI {
try {
return getTL("/users/me/subscriptions/videos", "-publishedAt", null, max_id, null, null);
} catch (HttpsConnection.HttpsConnectionException e) {
if (e.getStatusCode() == 401 || e.getStatusCode() == 403) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
if (targetedAccount != null && (e.getStatusCode() == 401 || e.getStatusCode() == 403)) {
HashMap<String, String> values = refreshToken(targetedAccount.getClient_id(), targetedAccount.getClient_secret(), targetedAccount.getRefresh_token());
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (values.containsKey("access_token") && values.get("access_token") != null) {
......@@ -1240,7 +1240,7 @@ public class PeertubeAPI {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/video-channels", name)), 60, null, null);
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
accounts = parseAccountResponsePeertube(context, jsonArray);
accounts = parseAccountResponsePeertube(jsonArray);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -1262,7 +1262,7 @@ public class PeertubeAPI {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(String.format("https://" + instance + "/api/v1/videos/%s", videoId), 60, null, token);
JSONObject jsonObject = new JSONObject(response);
peertube = parseSinglePeertube(context, instance, jsonObject);
peertube = parseSinglePeertube(instance, jsonObject);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
......@@ -1566,7 +1566,7 @@ public class PeertubeAPI {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
PeertubeNotification peertubeNotification = parsePeertubeNotifications(context, resobj);
PeertubeNotification peertubeNotification = parsePeertubeNotifications(resobj);
i++;
peertubeNotifications.add(peertubeNotification);
}
......@@ -1638,7 +1638,7 @@ public class PeertubeAPI {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
Peertube peertube = parsePeertube(context, resobj);
Peertube peertube = parsePeertube(resobj);
i++;
peertubes.add(peertube);
}
......@@ -1691,13 +1691,13 @@ public class PeertubeAPI {
return playlists;
}
private List<Account> parseAccountResponsePeertube(Context context, JSONArray jsonArray) {
private List<Account> parseAccountResponsePeertube(JSONArray jsonArray) {
List<Account> accounts = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
Account account = parseAccountResponsePeertube(context, resobj);
Account account = parseAccountResponsePeertube(resobj);
accounts.add(account);
i++;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment