added Mastodon endpoints, restructured Mastodon class, layout fix, layout fix

This commit is contained in:
nuclearfog 2022-11-27 10:16:55 +01:00
parent 1efbbc7cdb
commit ce5fb2fdea
No known key found for this signature in database
GPG Key ID: 03488A185C476379
9 changed files with 180 additions and 93 deletions

View File

@ -83,6 +83,7 @@ public class Mastodon implements Connection {
private static final String ENDPOINT_BLOCKS = "/api/v1/blocks";
private static final String ENDPOINT_MUTES = "/api/v1/mutes";
private static final String ENDPOINT_INCOMIN_REQUESTS = "/api/v1/follow_requests";
private static final String ENDPOINT_LOOKUP_USER = "/api/v1/accounts/lookup";
MediaType TYPE_TEXT = MediaType.parse("text/plain");
@ -169,7 +170,9 @@ public class Mastodon implements Connection {
@Override
public User showUser(String name) throws MastodonException {
throw new MastodonException("not implemented!"); // todo add implementation
List<String> params = new ArrayList<>();
params.add("acct=" + name);
return getUser(ENDPOINT_LOOKUP_USER, params);
}
@ -276,49 +279,51 @@ public class Mastodon implements Connection {
@Override
public void followUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/follow", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/follow", new ArrayList<>());
}
@Override
public void unfollowUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/unfollow", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/unfollow", new ArrayList<>());
}
@Override
public void blockUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/block", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/block", new ArrayList<>());
}
@Override
public void blockUser(String name) throws MastodonException {
throw new MastodonException("not implemented!"); // todo add implementation
User user = showUser(name);
blockUser(user.getId());
}
@Override
public void unblockUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/unblock", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/unblock", new ArrayList<>());
}
@Override
public void muteUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/mute", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/mute", new ArrayList<>());
}
@Override
public void muteUser(String name) throws MastodonException {
throw new MastodonException("not implemented!"); // todo add implementation
User user = showUser(name);
muteUser(user.getId());
}
@Override
public void unmuteUser(long id) throws MastodonException {
sendPost(ENDPOINT_ACCOUNTS + id + "/unmute", new ArrayList<>());
createPost(ENDPOINT_ACCOUNTS + id + "/unmute", new ArrayList<>());
}
@ -418,37 +423,37 @@ public class Mastodon implements Connection {
@Override
public Status favoriteStatus(long id) throws MastodonException {
return getStatus(ENDPOINT_STATUS + id + "/favourite", new ArrayList<>());
return postStatus(ENDPOINT_STATUS + id + "/favourite", new ArrayList<>());
}
@Override
public Status unfavoriteStatus(long id) throws MastodonException {
return getStatus(ENDPOINT_STATUS + id + "/unfavourite", new ArrayList<>());
return postStatus(ENDPOINT_STATUS + id + "/unfavourite", new ArrayList<>());
}
@Override
public Status repostStatus(long id) throws MastodonException {
return getStatus(ENDPOINT_STATUS + id + "/reblog", new ArrayList<>());
return postStatus(ENDPOINT_STATUS + id + "/reblog", new ArrayList<>());
}
@Override
public Status removeRepost(long id) throws MastodonException {
return getStatus(ENDPOINT_STATUS + id + "/unreblog", new ArrayList<>());
return postStatus(ENDPOINT_STATUS + id + "/unreblog", new ArrayList<>());
}
@Override
public void muteConversation(long id) throws MastodonException {
sendPost(ENDPOINT_MUTES + id + "/mute", new ArrayList<>());
createPost(ENDPOINT_MUTES + id + "/mute", new ArrayList<>());
}
@Override
public void unmuteConversation(long id) throws MastodonException {
sendPost(ENDPOINT_STATUS + id + "/unmute", new ArrayList<>());
createPost(ENDPOINT_STATUS + id + "/unmute", new ArrayList<>());
}
@ -584,22 +589,16 @@ public class Mastodon implements Connection {
}
/**
* get a status from endpoint
* get information about the current user
*
* @param endpoint endpoint to use
* @param params additional parameters
* @return status
* @param host Mastodon hostname
* @param bearer bearer token to use
* @return current user information
*/
private Status getStatus(String endpoint, List<String> params) throws MastodonException {
private User getCredentials(String host, @NonNull String bearer) throws MastodonException {
try {
Response response = get(endpoint, params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonStatus(json, settings.getLogin().getId());
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
return createUser(get(host, VERIFY_CREDENTIALS, bearer, new ArrayList<>()));
} catch (IOException e) {
throw new MastodonException(e);
}
}
@ -613,31 +612,37 @@ public class Mastodon implements Connection {
*/
private User getUser(String endpoint, List<String> params) throws MastodonException {
try {
Response response = get(endpoint, params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonUser(json, settings.getLogin().getId());
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
return createUser(get(endpoint, params));
} catch (IOException e) {
throw new MastodonException(e);
}
}
/**
* send post request to ui
* get a status from endpoint
*
* @param endpoint endpoint to use
* @param params additional parameters
* @return status
*/
private void sendPost(String endpoint, List<String> params) throws MastodonException {
private Status getStatus(String endpoint, List<String> params) throws MastodonException {
try {
Response response = post(endpoint, params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null)
return;
throw new MastodonException(response);
return createStatus(get(endpoint, params));
} catch (IOException e) {
throw new MastodonException(e);
}
}
/**
* post a status from endpoint
*
* @param endpoint endpoint to use
* @param params additional parameters
* @return status
*/
private Status postStatus(String endpoint, List<String> params) throws MastodonException {
try {
return createStatus(post(endpoint, params));
} catch (IOException e) {
throw new MastodonException(e);
}
@ -659,14 +664,105 @@ public class Mastodon implements Connection {
params.add("max_id=" + maxId);
params.add("limit=" + settings.getListSize());
try {
Response response = get(endpoint, params);
return createStatuses(get(endpoint, params));
} catch (IOException e) {
throw new MastodonException(e);
}
}
/**
* get a list of users from an endpoint
*
* @param endpoint Ednpoint to use
* @param params additional parameters
* @return list of users
*/
private Users getUsers(String endpoint, List<String> params) throws MastodonException {
try {
return createUsers(get(endpoint, params));
} catch (IOException e) {
throw new MastodonException(e);
}
}
/**
* create user from response
*
* @param response endpoint response
* @return user
*/
private User createUser(Response response) throws MastodonException {
try {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonUser(json, settings.getLogin().getId());
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/**
* create a list of users from response
*
* @param response endpoint response
* @return list of users
*/
private Users createUsers(Response response) throws MastodonException {
try {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONArray array = new JSONArray(body.string());
Users result = new Users(0L, 0L);
for (int i = 0; i < array.length(); i++) {
User item = new MastodonUser(array.getJSONObject(i));
result.add(item);
}
return result;
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/**
* create status from endpoint
*
* @param response endpoint response
* @return status
*/
private Status createStatus(Response response) throws MastodonException {
try {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonStatus(json, settings.getLogin().getId());
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/**
* create a list of statuses from a resposne
*
* @param response endpoint response
* @return list of statuses
*/
private List<Status> createStatuses(Response response) throws MastodonException {
try {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONArray statuses;
if (SEARCH_TIMELINE.equals(endpoint))
statuses = new JSONObject(body.string()).getJSONArray("statuses");
String jsonStr = body.string();
if (jsonStr.startsWith("{"))
statuses = new JSONObject(jsonStr).getJSONArray("statuses");
else
statuses = new JSONArray(body.string());
statuses = new JSONArray(jsonStr);
List<Status> result = new ArrayList<>(statuses.length());
long currentId = settings.getLogin().getId();
for (int i = 0; i < statuses.length(); i++) {
@ -681,48 +777,19 @@ public class Mastodon implements Connection {
}
/**
* get a list of users from an endpoint
* send post request without return
*
* @param endpoint Ednpoint to use
* @param params additional parameters
* @return list of users
* @param endpoint endpoint to use
* @param params additional parameters
*/
private Users getUsers(String endpoint, List<String> params) throws MastodonException {
private void createPost(String endpoint, List<String> params) throws MastodonException {
try {
Response response = get(endpoint, params);
Response response = post(endpoint, params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONArray array = new JSONArray(body.string());
Users result = new Users(0L, 0L);
for (int i = 0 ; i < array.length() ; i++) {
User item = new MastodonUser(array.getJSONObject(i));
result.add(item);
}
return result;
}
if (response.code() == 200 && body != null)
return;
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/**
* get information about the current user
*
* @param host Mastodon hostname
* @param bearer bearer token to use
* @return current user information
*/
private User getCredentials(String host, @NonNull String bearer) throws MastodonException {
try {
Response response = get(host, VERIFY_CREDENTIALS, bearer, new ArrayList<>());
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonUser(json);
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
} catch (IOException e) {
throw new MastodonException(e);
}
}

View File

@ -18,6 +18,8 @@ import org.nuclearfog.twidda.model.User;
*/
public class MastodonStatus implements Status {
private static final long serialVersionUID = 1184375228249441241L;
private long id;
private long createdAt;

View File

@ -11,6 +11,8 @@ import org.nuclearfog.twidda.model.Trend;
*/
public class MastodonTrend implements Trend {
private static final long serialVersionUID = 4328931229081239280L;
private int rank;
private int popularity;
private String name;

View File

@ -38,7 +38,6 @@ public class MastodonUser implements User {
* @param json json object used by Mastodon API
*/
public MastodonUser(JSONObject json) throws JSONException {
id = Long.parseLong(json.getString("id"));
screenname = json.optString("acct", "");
username = json.optString("display_name");
createdAt = StringTools.getTime2(json.optString("created_at", ""));
@ -46,11 +45,16 @@ public class MastodonUser implements User {
bannerUrl = json.optString("banner");
description = json.optString("note");
url = json.optString("url");
following = json.optInt("following_count");
follower = json.optInt("followers_count");
statusCount = json.optInt("statuses_count");
locked = json.optBoolean("locked");
try {
id = Long.parseLong(json.getString("id"));
} catch (NumberFormatException e) {
throw new JSONException("bad user ID:" + id);
}
}

View File

@ -12,6 +12,8 @@ import org.nuclearfog.twidda.model.Trend;
*/
public class TrendV1 implements Trend {
private static final long serialVersionUID = -2405773547644847221L;
private int rank;
private int popularity;
private int locationId;

View File

@ -14,6 +14,8 @@ import org.nuclearfog.twidda.model.Trend;
*/
public class TrendImpl implements Trend {
private static final long serialVersionUID = 1799880502954768985L;
/**
* SQLite columns
*/

View File

@ -1,11 +1,13 @@
package org.nuclearfog.twidda.model;
import java.io.Serializable;
/**
* interface for trend implementations
*
* @author nuclearfog
*/
public interface Trend {
public interface Trend extends Serializable {
/**
* @return trend name

View File

@ -21,7 +21,8 @@
android:id="@+id/login_first_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/loginpage_number_padding"
android:layout_marginStart="@dimen/loginpage_number_margin"
android:layout_marginEnd="@dimen/loginpage_number_margin"
android:text="@string/login_first_opt"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
@ -125,13 +126,15 @@
android:maxLines="1"
app:layout_constraintStart_toEndOf="@id/login_enter_key2"
app:layout_constraintTop_toBottomOf="@id/login_network_selector"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
android:inputType="textUri" />
<TextView
android:id="@+id/login_sec_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/loginpage_number_padding"
android:layout_marginStart="@dimen/loginpage_number_margin"
android:layout_marginEnd="@dimen/loginpage_number_margin"
android:text="@string/login_sec_opt"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
@ -159,7 +162,8 @@
android:id="@+id/login_third_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/loginpage_number_padding"
android:layout_marginStart="@dimen/loginpage_number_margin"
android:layout_marginEnd="@dimen/loginpage_number_margin"
android:text="@string/login_trd_opt"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
@ -179,6 +183,7 @@
android:inputType="numberPassword"
android:textSize="@dimen/loginpage_textsize_login_key"
android:maxLines="1"
android:layout_marginEnd="@dimen/loginpage_layout_margin"
app:layout_constraintStart_toEndOf="@id/login_third_opt"
app:layout_constraintTop_toBottomOf="@id/login_get_link"
app:layout_constraintBottom_toTopOf="@id/login_verifier"
@ -188,7 +193,8 @@
android:id="@+id/login_4th_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/loginpage_number_padding"
android:layout_marginStart="@dimen/loginpage_number_margin"
android:layout_marginEnd="@dimen/loginpage_number_margin"
android:text="@string/login_4th_opt"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"

View File

@ -149,7 +149,7 @@
<dimen name="loginpage_textsize_switch">12sp</dimen>
<dimen name="loginpage_textsize_api_key">16sp</dimen>
<dimen name="loginpage_textsize_login_key">20sp</dimen>
<dimen name="loginpage_number_padding">10dp</dimen>
<dimen name="loginpage_number_margin">10dp</dimen>
<dimen name="loginpage_label_max_width">110dp</dimen>
<!--dimens of page_metrics.xml-->