From f5ef7c7cc1e74b78dbecd700fa2ba157eae55af8 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sat, 11 Jan 2020 13:09:23 +0100 Subject: [PATCH] Remove direct messages from news stream --- .../src/main/java/ctbrec/ui/news/NewsTab.java | 89 +++++++++++-------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/news/NewsTab.java b/client/src/main/java/ctbrec/ui/news/NewsTab.java index e8dd957f..bf4e075f 100644 --- a/client/src/main/java/ctbrec/ui/news/NewsTab.java +++ b/client/src/main/java/ctbrec/ui/news/NewsTab.java @@ -3,6 +3,7 @@ package ctbrec.ui.news; import static ctbrec.io.HttpConstants.*; import java.io.IOException; +import java.util.Objects; import org.json.JSONObject; @@ -38,52 +39,62 @@ public class NewsTab extends Tab implements TabSelectionListener { @Override public void selected() { - new Thread(() -> { - try { - Request request = new Request.Builder() - .url(URL) - .header("Authorization", "Bearer " + ACCESS_TOKEN) - .header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion().toString()) - .build(); - try(Response response = CamrecApplication.httpClient.execute(request)) { - if(response.isSuccessful()) { - String body = response.body().string(); - if(body.startsWith("[")) { - Moshi moshi = new Moshi.Builder().build(); - JsonAdapter statusListAdapter = moshi.adapter(Status[].class); - Status[] statusArray = statusListAdapter.fromJson(body); - Platform.runLater(() -> { - layout.getChildren().clear(); - for (Status status : statusArray) { - if(status.getInReplyToId() == null) { - StatusPane stp = new StatusPane(status); - layout.getChildren().add(stp); - VBox.setMargin(stp, new Insets(10)); - } - } - }); - } else if(body.startsWith("{")) { - JSONObject json = new JSONObject(response.body().string()); - if(json.has("error")) { - throw new IOException("Request not successful: " + json.getString("error")); - } else { - throw new IOException("Unexpected response: " + body); - } - } else { - throw new IOException("Unexpected response: " + body); - } + new Thread(this::loadToots).start(); + } + + private void loadToots() { + try { + Request request = new Request.Builder() + .url(URL) + .header("Authorization", "Bearer " + ACCESS_TOKEN) + .header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion().toString()) + .build(); + try (Response response = CamrecApplication.httpClient.execute(request)) { + if (response.isSuccessful()) { + String body = response.body().string(); + if (body.startsWith("[")) { + onSuccess(body); + } else if (body.startsWith("{")) { + onError(body); } else { - throw new HttpException(response.code(), response.message()); + throw new IOException("Unexpected response: " + body); } + } else { + throw new HttpException(response.code(), response.message()); } - } catch (IOException e) { - Dialogs.showError("News", "Couldn't load news from mastodon", e); } - }).start(); + } catch (IOException e) { + Dialogs.showError("News", "Couldn't load news from mastodon", e); + } + } + + private void onError(String body) throws IOException { + JSONObject json = new JSONObject(body); + if (json.has("error")) { + throw new IOException("Request not successful: " + json.getString("error")); + } else { + throw new IOException("Unexpected response: " + body); + } + } + + private void onSuccess(String body) throws IOException { + Moshi moshi = new Moshi.Builder().build(); + JsonAdapter statusListAdapter = moshi.adapter(Status[].class); + Status[] statusArray = statusListAdapter.fromJson(body); + Platform.runLater(() -> { + layout.getChildren().clear(); + for (Status status : statusArray) { + if (status.getInReplyToId() == null && !Objects.equals("direct", status.getVisibility())) { + StatusPane stp = new StatusPane(status); + layout.getChildren().add(stp); + VBox.setMargin(stp, new Insets(10)); + } + } + }); } @Override public void deselected() { - // TODO Auto-generated method stub + // nothing to do } }