added userlist endpoints, bug fix, layout fix

This commit is contained in:
nuclearfog 2022-12-08 21:05:31 +01:00
parent 518aceed0f
commit a955df5fcc
No known key found for this signature in database
GPG Key ID: 03488A185C476379
8 changed files with 97 additions and 48 deletions

View File

@ -95,34 +95,45 @@ public class UserlistHolder extends ViewHolder implements OnClickListener {
User owner = userlist.getListOwner();
title.setText(userlist.getTitle());
description.setText(userlist.getDescription());
username.setText(owner.getUsername());
screenname.setText(owner.getScreenname());
date.setText(StringTools.formatCreationTime(itemView.getResources(), userlist.getTimestamp()));
member.setText(NUM_FORMAT.format(userlist.getMemberCount()));
subscriber.setText(NUM_FORMAT.format(userlist.getSubscriberCount()));
String profileImageUrl = owner.getProfileImageThumbnailUrl();
if (settings.imagesEnabled() && !profileImageUrl.isEmpty()) {
Transformation roundCorner = new RoundedCornersTransformation(3, 0);
picasso.load(profileImageUrl).transform(roundCorner).error(R.drawable.no_image).into(profile);
} else {
profile.setImageResource(0);
}
if (!userlist.getListOwner().isCurrentUser() && userlist.isFollowing()) {
follow.setVisibility(View.VISIBLE);
followList.setVisibility(View.VISIBLE);
} else {
follow.setVisibility(View.GONE);
followList.setVisibility(View.GONE);
}
if (owner.isVerified()) {
verified.setVisibility(View.VISIBLE);
} else {
verified.setVisibility(View.GONE);
}
if (owner.isProtected()) {
locked.setVisibility(View.VISIBLE);
if (owner != null) {
username.setText(owner.getUsername());
screenname.setText(owner.getScreenname());
String profileImageUrl = owner.getProfileImageThumbnailUrl();
if (settings.imagesEnabled() && !profileImageUrl.isEmpty()) {
Transformation roundCorner = new RoundedCornersTransformation(3, 0);
picasso.load(profileImageUrl).transform(roundCorner).error(R.drawable.no_image).into(profile);
} else {
profile.setImageResource(0);
}
if (!owner.isCurrentUser() && userlist.isFollowing()) {
follow.setVisibility(View.VISIBLE);
followList.setVisibility(View.VISIBLE);
} else {
follow.setVisibility(View.GONE);
followList.setVisibility(View.GONE);
}
if (owner.isVerified()) {
verified.setVisibility(View.VISIBLE);
} else {
verified.setVisibility(View.GONE);
}
if (owner.isProtected()) {
locked.setVisibility(View.VISIBLE);
} else {
locked.setVisibility(View.GONE);
}
} else {
locked.setVisibility(View.GONE);
verified.setVisibility(View.GONE);
follow.setVisibility(View.GONE);
followList.setVisibility(View.GONE);
profile.setVisibility(View.GONE);
username.setVisibility(View.GONE);
screenname.setVisibility(View.GONE);
}
if (userlist.isPrivate()) {
privateList.setVisibility(View.VISIBLE);

View File

@ -148,15 +148,13 @@ public class Mastodon implements Connection {
List<String> params = new ArrayList<>();
String host = link.getScheme() + "://" + link.getHost();
String client_id = link.getQueryParameter("client_id");
String client_secret = link.getQueryParameter("client_secret");
String clientSecret = link.getQueryParameter("client_secret");
params.add("client_id=" + client_id);
params.add("client_secret=" + client_secret);
params.add("client_secret=" + clientSecret);
params.add("grant_type=authorization_code");
params.add("code=" + paramsStr[1]);
params.add("redirect_uri=" + REDIRECT_URI);
params.add("scope=" + AUTH_SCOPES);
try {
Response response = post(host, LOGIN_APP, null, params);
ResponseBody body = response.body();
@ -164,7 +162,7 @@ public class Mastodon implements Connection {
JSONObject json = new JSONObject(body.string());
String bearer = json.getString("access_token");
User user = getCredentials(host, bearer);
Account account = new MastodonAccount(user, host, bearer, client_id, client_secret);
Account account = new MastodonAccount(user, host, bearer, client_id, clientSecret);
settings.setLogin(account, false);
return account;
}
@ -275,8 +273,8 @@ public class Mastodon implements Connection {
@Override
public Users getOutgoingFollowRequests(long cursor) throws MastodonException {
throw new MastodonException("not implemented!"); // todo add implementation
public Users getOutgoingFollowRequests(long cursor) {
return new Users(0L, 0L); // not yet implemented in the mastodon API
}
@ -428,7 +426,7 @@ public class Mastodon implements Connection {
@Override
public List<Status> getStatusReplies(String name, long id, long minId, long maxId) throws MastodonException {
public List<Status> getStatusReplies(String name, long id, long minId, long maxId) {
return new ArrayList<>(0); // todo add implementation
}
@ -568,14 +566,17 @@ public class Mastodon implements Connection {
@Override
public UserLists getUserlistOwnerships(long id, String name, long cursor) throws MastodonException {
throw new MastodonException("not supported!");
public UserLists getUserlistOwnerships(long id, String name, long cursor) {
return new UserLists(0L, 0L); // not implemented yet in the official API
}
@Override
public UserLists getUserlistMemberships(long id, String name, long cursor) throws MastodonException {
throw new MastodonException("not supported!");
List<String> params = new ArrayList<>();
if (cursor > 0)
params.add("since_id=" + cursor);
return getUserLists(ENDPOINT_ACCOUNTS + '/' + id + "/lists", params);
}
@ -827,6 +828,32 @@ public class Mastodon implements Connection {
}
}
/**
* create userlists from GET endpoint
*
* @param endpoint userlist endpoint
* @param params additional parameters
* @return userlists
*/
private UserLists getUserLists(String endpoint, List<String> params) throws MastodonException {
params.add("limit=" + settings.getListSize());
try {
Response response = get(endpoint, params);
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONArray array = new JSONArray(body.string());
UserLists result = new UserLists(0L, 0L);// todo add pagination
for (int i = 0 ; i < array.length(); i++) {
result.add(new MastodonList(array.getJSONObject(i)));
}
return result;
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {
throw new MastodonException(e);
}
}
/**
* create user from response
*
@ -900,8 +927,9 @@ public class Mastodon implements Connection {
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
User currentUser = getCredentials();
return new MastodonList(json, currentUser);
MastodonList result = new MastodonList(json);
result.setOwner(getCredentials());
return result;
}
throw new MastodonException(response);
} catch (IOException | JSONException e) {

View File

@ -33,11 +33,8 @@ public class MastodonException extends ConnectionException {
break;
case 403:
errorCode = HTTP_FORBIDDEN;
break;
case 404:
errorCode = RESOURCE_NOT_FOUND;
errorCode = HTTP_FORBIDDEN;
break;
case 429:

View File

@ -24,12 +24,10 @@ public class MastodonList implements UserList {
/**
* @param json userlist json object
* @param owner owner of the list
*/
public MastodonList(JSONObject json, User owner) throws JSONException {
public MastodonList(JSONObject json) throws JSONException {
String idStr = json.getString("id");
title = json.getString("title");
this.owner = owner;
try {
id = Long.parseLong(idStr);
@ -106,4 +104,13 @@ public class MastodonList implements UserList {
return false;
return ((UserList) obj).getId() == id;
}
/**
* setup list owner
*
* @param owner owner of this list
*/
public void setOwner(User owner) {
this.owner = owner;
}
}

View File

@ -223,9 +223,9 @@ public class Twitter implements Connection {
public Account loginApp(String... paramsStr) throws TwitterException {
try {
List<String> params = new ArrayList<>();
String oauthToken = Uri.parse(paramsStr[0]).getQueryParameter("oauth_token");
String tempOauthToken = Uri.parse(paramsStr[0]).getQueryParameter("oauth_token");
params.add("oauth_verifier=" + paramsStr[1]);
params.add("oauth_token=" + oauthToken);
params.add("oauth_token=" + tempOauthToken);
Response response;
if (paramsStr.length == 4)
response = post(OAUTH_VERIFIER, params, paramsStr[2], paramsStr[3]);
@ -236,7 +236,7 @@ public class Twitter implements Connection {
// extract tokens from link
String res = body.string();
Uri uri = Uri.parse(OAUTH_VERIFIER + "?" + res);
oauthToken = uri.getQueryParameter("oauth_token");
String oauthToken = uri.getQueryParameter("oauth_token");
String tokenSecret = uri.getQueryParameter("oauth_token_secret");
// check if login works
User user;

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.model;
import androidx.annotation.Nullable;
import java.io.Serializable;
/**
@ -32,6 +34,7 @@ public interface UserList extends Serializable {
/**
* @return owner of the list
*/
@Nullable
User getListOwner();
/**

View File

@ -138,7 +138,8 @@ public class UserlistActivity extends AppCompatActivity implements OnTabSelected
userList = (UserList) data;
toolbar.setTitle(userList.getTitle());
toolbar.setSubtitle(userList.getDescription());
adapter.setupListContentPage(userList.getId(), userList.getListOwner().isCurrentUser());
boolean isOwner = userList.getListOwner() != null && userList.getListOwner().isCurrentUser();
adapter.setupListContentPage(userList.getId(), isOwner);
}
setSupportActionBar(toolbar);
@ -191,7 +192,7 @@ public class UserlistActivity extends AppCompatActivity implements OnTabSelected
SearchView searchUser = (SearchView) search.getActionView();
AppStyles.setTheme(searchUser, Color.TRANSPARENT);
if (userList != null) {
if (userList.getListOwner().isCurrentUser()) {
if (userList.getListOwner() != null && userList.getListOwner().isCurrentUser()) {
searchUser.setQueryHint(getString(R.string.menu_add_user));
searchUser.setOnQueryTextListener(this);
editList.setVisible(true);

View File

@ -126,6 +126,7 @@
android:layout_marginTop="@dimen/loginpage_layout_margin"
android:layout_marginEnd="@dimen/loginpage_layout_margin"
android:maxLines="1"
android:gravity="center"
app:layout_constraintStart_toEndOf="@id/login_enter_key2"
app:layout_constraintTop_toBottomOf="@id/login_network_selector"
app:layout_constraintEnd_toEndOf="parent"
@ -188,6 +189,7 @@
android:layout_marginEnd="@dimen/loginpage_layout_margin"
android:ellipsize="end"
android:drawableStart="@drawable/key"
android:gravity="center"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintStart_toEndOf="@id/login_third_opt"
app:layout_constraintTop_toBottomOf="@id/login_get_link"