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 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<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("{")) {
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<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
public void deselected() {
// TODO Auto-generated method stub
// nothing to do
}
}