forked from j62/ctbrec
1
0
Fork 0

Remove direct messages from news stream

This commit is contained in:
0xboobface 2020-01-11 13:09:23 +01:00
parent b66989fe85
commit f5ef7c7cc1
1 changed files with 50 additions and 39 deletions

View File

@ -3,6 +3,7 @@ package ctbrec.ui.news;
import static ctbrec.io.HttpConstants.*; import static ctbrec.io.HttpConstants.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import org.json.JSONObject; import org.json.JSONObject;
@ -38,7 +39,10 @@ public class NewsTab extends Tab implements TabSelectionListener {
@Override @Override
public void selected() { public void selected() {
new Thread(() -> { new Thread(this::loadToots).start();
}
private void loadToots() {
try { try {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(URL) .url(URL)
@ -49,26 +53,9 @@ public class NewsTab extends Tab implements TabSelectionListener {
if (response.isSuccessful()) { if (response.isSuccessful()) {
String body = response.body().string(); String body = response.body().string();
if (body.startsWith("[")) { if (body.startsWith("[")) {
Moshi moshi = new Moshi.Builder().build(); onSuccess(body);
JsonAdapter<Status[]> 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("{")) { } else if (body.startsWith("{")) {
JSONObject json = new JSONObject(response.body().string()); onError(body);
if(json.has("error")) {
throw new IOException("Request not successful: " + json.getString("error"));
} else {
throw new IOException("Unexpected response: " + body);
}
} else { } else {
throw new IOException("Unexpected response: " + body); throw new IOException("Unexpected response: " + body);
} }
@ -79,11 +66,35 @@ public class NewsTab extends Tab implements TabSelectionListener {
} catch (IOException e) { } catch (IOException e) {
Dialogs.showError("News", "Couldn't load news from mastodon", e); Dialogs.showError("News", "Couldn't load news from mastodon", e);
} }
}).start(); }
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<Status[]> 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 @Override
public void deselected() { public void deselected() {
// TODO Auto-generated method stub // nothing to do
} }
} }