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,52 +39,62 @@ public class NewsTab extends Tab implements TabSelectionListener {
@Override @Override
public void selected() { public void selected() {
new Thread(() -> { new Thread(this::loadToots).start();
try { }
Request request = new Request.Builder()
.url(URL) private void loadToots() {
.header("Authorization", "Bearer " + ACCESS_TOKEN) try {
.header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion().toString()) Request request = new Request.Builder()
.build(); .url(URL)
try(Response response = CamrecApplication.httpClient.execute(request)) { .header("Authorization", "Bearer " + ACCESS_TOKEN)
if(response.isSuccessful()) { .header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion().toString())
String body = response.body().string(); .build();
if(body.startsWith("[")) { try (Response response = CamrecApplication.httpClient.execute(request)) {
Moshi moshi = new Moshi.Builder().build(); if (response.isSuccessful()) {
JsonAdapter<Status[]> statusListAdapter = moshi.adapter(Status[].class); String body = response.body().string();
Status[] statusArray = statusListAdapter.fromJson(body); if (body.startsWith("[")) {
Platform.runLater(() -> { onSuccess(body);
layout.getChildren().clear(); } else if (body.startsWith("{")) {
for (Status status : statusArray) { onError(body);
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);
}
} else { } 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<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
} }
} }