added compare and tostring methods to mastodon model implementations, bug fix, added comments, removed unused strings

This commit is contained in:
nuclearfog 2022-11-27 20:32:49 +01:00
parent 53dce34397
commit a9f01583d4
No known key found for this signature in database
GPG Key ID: 03488A185C476379
14 changed files with 211 additions and 73 deletions

View File

@ -1,5 +1,6 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.model.Account;
@ -22,12 +23,16 @@ public class MastodonAccount implements Account {
private User user;
/**
*
* @param user user information
* @param hostname hostname of the Mastodon isntance
* @param bearer bearer token
* @param client_id app client ID
* @param client_secret app client secret
*/
public MastodonAccount(User user, String hostname, String baerer, String client_id, String client_secret) {
public MastodonAccount(User user, String hostname, String bearer, String client_id, String client_secret) {
this.user = user;
this.hostname = hostname;
this.bearer = baerer;
this.bearer = bearer;
this.client_id = client_id;
this.client_secret = client_secret;
createdAt = System.currentTimeMillis();
@ -94,4 +99,19 @@ public class MastodonAccount implements Account {
public int getApiType() {
return API_MASTODON;
}
@NonNull
@Override
public String toString() {
return user.toString();
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof Account))
return false;
return user.equals(((Account) obj).getUser());
}
}

View File

@ -1,5 +1,8 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.User;
@ -19,7 +22,10 @@ public class MastodonList implements UserList {
private String title;
private User owner;
/**
* @param json userlist json object
* @param owner owner of the list
*/
public MastodonList(JSONObject json, User owner) throws JSONException {
String idStr = json.getString("id");
title = json.getString("title");
@ -85,4 +91,19 @@ public class MastodonList implements UserList {
public int getSubscriberCount() {
return 0;
}
@NonNull
@Override
public String toString() {
return "id=" + id + " title=\"" + title + "\"";
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof UserList))
return false;
return ((UserList) obj).getId() == id;
}
}

View File

@ -1,5 +1,6 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONException;
@ -24,7 +25,10 @@ public class MastodonNotification implements Notification {
private User user;
private Status status;
/**
* @param json notification json object
* @param currentId Id of the current user
*/
public MastodonNotification(JSONObject json, long currentId) throws JSONException {
String idStr = json.getString("id");
String typeStr = json.getString("type");
@ -106,4 +110,19 @@ public class MastodonNotification implements Notification {
public Status getStatus() {
return status;
}
@NonNull
@Override
public String toString() {
return "id=" + id + " " + user;
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof Notification))
return false;
return ((Notification) obj).getId() == id;
}
}

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.Relation;
@ -16,7 +18,10 @@ public class MastodonRelation implements Relation {
private boolean blocked;
private boolean muted;
/**
* @param json Relation json object
* @param currentId ID of the current user
*/
public MastodonRelation(JSONObject json, long currentId) {
currentUser = currentId == Long.parseLong(json.optString("id", "0"));
following = json.optBoolean("following");
@ -60,4 +65,12 @@ public class MastodonRelation implements Relation {
public boolean canDm() {
return false;
}
@NonNull
@Override
public String toString() {
return "following=" + following + " follower=" + follower +
" blocked=" + blocked + " muted=" + muted;
}
}

View File

@ -5,6 +5,7 @@ import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.backend.utils.StringTools;
@ -21,32 +22,60 @@ public class MastodonStatus implements Status {
private static final long serialVersionUID = 1184375228249441241L;
private long id;
private long replyId;
private long replyUserId;
private long createdAt;
private int replyCount, favoriteCount, reblogCount;
private boolean favorited, reblogged, sensitive;
private String text, source;
private String text, source, mentions;
private User author;
/**
* @param json Mastodon status json object
* @param currentUserId Id of the current user
*/
public MastodonStatus(JSONObject json, long currentUserId) throws JSONException {
JSONObject application = json.optJSONObject("application");
id = Long.parseLong(json.getString("id"));
JSONArray mentionsJson = json.optJSONArray("mentions");
String idStr = json.getString("id");
String replyIdStr = json.optString("in_reply_to_id", "0");
String replyUserIdStr = json.optString("in_reply_to_account_id", "0");
author = new MastodonUser(json.getJSONObject("account"), currentUserId);
createdAt = StringTools.getTime2(json.optString("created_at"));
replyCount = json.optInt("replies_count");
reblogCount = json.optInt("reblogs_count");
favoriteCount = json.optInt("favourites_count");
favorited = json.optBoolean("favourited");
reblogged = json.optBoolean("reblogged");
text = json.optString("content", "");
text = json.optString("text", "");
sensitive = json.optBoolean("sensitive", false);
if (application != null)
if (mentionsJson != null) {
StringBuilder mentionsBuilder = new StringBuilder();
for (int i = 0; i < mentionsJson.length(); i++) {
String item = mentionsJson.getJSONObject(i).optString("acct", "");
mentionsBuilder.append('@').append(item).append(' ');
}
mentions = mentionsBuilder.toString();
text = mentions + ' ' + text;
} else {
mentions = "";
}
if (application != null) {
source = application.optString("name", "");
else
} else {
source = "";
author = new MastodonUser(json.getJSONObject("account"), currentUserId);
}
try {
id = Long.parseLong(idStr);
replyId = Long.parseLong(replyIdStr);
replyUserId = Long.parseLong(replyUserIdStr);
} catch (NumberFormatException e) {
throw new JSONException("bad ID:" + idStr + ' ' + replyIdStr + ' ' + replyUserIdStr);
}
}
@ -95,13 +124,13 @@ public class MastodonStatus implements Status {
@Override
public long getRepliedUserId() {
return 0;
return replyUserId;
}
@Override
public long getRepliedStatusId() {
return 0;
return replyId;
}
@ -138,13 +167,13 @@ public class MastodonStatus implements Status {
@Override
public String getUserMentions() {
return "";
return mentions;
}
@Override
public int getMediaType() {
return 0;
return MEDIA_NONE;
}
@ -182,4 +211,19 @@ public class MastodonStatus implements Status {
public String getLocationCoordinates() {
return "";
}
@NonNull
@Override
public String toString() {
return author.toString() + " text=\"" + text + "\"";
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof Status))
return false;
return ((Status) obj).getId() == id;
}
}

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.Trend;
@ -17,7 +19,10 @@ public class MastodonTrend implements Trend {
private int popularity;
private String name;
/**
* @param json trend json object
* @param pos array index
*/
public MastodonTrend(JSONObject json, int pos) {
JSONArray history = json.optJSONArray("history");
name = json.optString("name", "");
@ -53,4 +58,11 @@ public class MastodonTrend implements Trend {
public int getPopularity() {
return popularity;
}
@NonNull
@Override
public String toString() {
return "name=\"" + name + " rank=" + rank;
}
}

View File

@ -1,5 +1,8 @@
package org.nuclearfog.twidda.backend.api.mastodon.impl;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.backend.utils.StringTools;
@ -38,6 +41,7 @@ public class MastodonUser implements User {
* @param json json object used by Mastodon API
*/
public MastodonUser(JSONObject json) throws JSONException {
String idStr = json.getString("id");
screenname = json.optString("acct", "");
username = json.optString("display_name");
createdAt = StringTools.getTime2(json.optString("created_at", ""));
@ -51,9 +55,9 @@ public class MastodonUser implements User {
locked = json.optBoolean("locked");
try {
id = Long.parseLong(json.getString("id"));
id = Long.parseLong(idStr);
} catch (NumberFormatException e) {
throw new JSONException("bad user ID:" + id);
throw new JSONException("bad user ID:" + idStr);
}
}
@ -164,4 +168,19 @@ public class MastodonUser implements User {
public boolean isCurrentUser() {
return isCurrentUser;
}
@NonNull
@Override
public String toString() {
return "name=\"" + screenname + "\"";
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof User))
return false;
return ((User) obj).getId() == id;
}
}

View File

@ -11,8 +11,6 @@ import org.json.JSONObject;
import org.nuclearfog.twidda.model.Message;
import org.nuclearfog.twidda.model.User;
import java.util.regex.Pattern;
/**
* API 1.1 implementation of a directmessage
*
@ -20,8 +18,6 @@ import java.util.regex.Pattern;
*/
public class MessageV1 implements Message {
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
private long id;
private long timestamp;
private long sender_id;
@ -37,9 +33,9 @@ public class MessageV1 implements Message {
*/
public MessageV1(JSONObject json) throws JSONException {
String idStr = json.getString("id");
if (ID_PATTERN.matcher(idStr).matches()) {
try {
id = Long.parseLong(idStr);
} else {
} catch (NumberFormatException e) {
throw new JSONException("bad message ID:" + idStr);
}
timestamp = Long.parseLong(json.getString("created_timestamp"));

View File

@ -13,7 +13,6 @@ import org.nuclearfog.twidda.model.Status;
import org.nuclearfog.twidda.model.User;
import java.util.Locale;
import java.util.regex.Pattern;
/**
* API v 1.1 implementation of a tweet
@ -49,11 +48,6 @@ public class TweetV1 implements Status {
*/
private static final String MIME_V_MP4 = "video/mp4";
/**
* regex pattern to compare ID
*/
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
private long id;
private long timestamp;
private User author;
@ -72,8 +66,8 @@ public class TweetV1 implements Status {
private String text;
private String source;
private long replyUserId = -1L;
private long replyTweetId = -1L;
private long replyUserId;
private long replyTweetId;
private int mediaType = MEDIA_NONE;
private String location = "";
private String replyName = "";
@ -89,8 +83,8 @@ public class TweetV1 implements Status {
JSONObject embeddedTweetJson = json.optJSONObject("retweeted_status");
String tweetIdStr = json.optString("id_str", "");
String replyName = json.optString("in_reply_to_screen_name", "");
String replyTweetIdStr = json.optString("in_reply_to_status_id_str", "");
String replyUsrIdStr = json.optString("in_reply_to_user_id_str", "");
String replyTweetIdStr = json.optString("in_reply_to_status_id_str", "0");
String replyUsrIdStr = json.optString("in_reply_to_user_id_str", "0");
String text = createText(json);
author = new UserV1(json.getJSONObject("user"), twitterId);
@ -105,29 +99,23 @@ public class TweetV1 implements Status {
mediaLinks = addMedia(json);
userMentions = StringTools.getUserMentions(text, author.getScreenname());
if (ID_PATTERN.matcher(tweetIdStr).matches()) {
try {
id = Long.parseLong(tweetIdStr);
} else {
throw new JSONException("bad tweet ID: " + tweetIdStr);
replyTweetId = Long.parseLong(replyTweetIdStr);
replyUserId = Long.parseLong(replyUsrIdStr);
if (currentUserJson != null) {
String retweetIdStr = currentUserJson.optString("id_str", "0");
retweetId = Long.parseLong(retweetIdStr);
}
} catch (NumberFormatException e) {
throw new JSONException("bad IDs: " + tweetIdStr + "," + replyUsrIdStr + " or retweet ID");
}
if (!replyName.isEmpty() && !replyName.equals("null")) {
this.replyName = '@' + replyName;
}
if (ID_PATTERN.matcher(replyTweetIdStr).matches()) {
replyTweetId = Long.parseLong(replyTweetIdStr);
}
if (ID_PATTERN.matcher(replyUsrIdStr).matches()) {
replyUserId = Long.parseLong(replyUsrIdStr);
}
if (locationJson != null) {
location = locationJson.optString("full_name", "");
}
if (currentUserJson != null) {
String retweetIdStr = currentUserJson.optString("id_str", "");
if (ID_PATTERN.matcher(retweetIdStr).matches()) {
retweetId = Long.parseLong(retweetIdStr);
}
}
if (embeddedTweetJson != null) {
embeddedTweet = new TweetV1(embeddedTweetJson, twitterId);
}

View File

@ -1,5 +1,8 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.backend.api.twitter.Twitter;
import org.nuclearfog.twidda.model.Account;
import org.nuclearfog.twidda.model.User;
@ -102,4 +105,19 @@ public class TwitterAccount implements Account {
public int getApiType() {
return Account.API_TWITTER;
}
@NonNull
@Override
public String toString() {
return user.toString();
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof Account))
return false;
return user.equals(((Account) obj).getUser());
}
}

View File

@ -9,8 +9,6 @@ import org.nuclearfog.twidda.backend.utils.StringTools;
import org.nuclearfog.twidda.model.User;
import org.nuclearfog.twidda.model.UserList;
import java.util.regex.Pattern;
/**
* API v 1.1 user list implementation
*
@ -25,8 +23,6 @@ public class UserListV1 implements UserList {
*/
private static final String PRIVATE = "private";
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
private long id;
private long createdAt;
private String title;
@ -44,11 +40,7 @@ public class UserListV1 implements UserList {
*/
public UserListV1(JSONObject json, long currentId) throws JSONException {
String idStr = json.getString("id_str");
if (ID_PATTERN.matcher(idStr).matches()) {
id = Long.parseLong(idStr);
} else {
throw new JSONException("bad userlist ID: " + idStr);
}
owner = new UserV1(json.getJSONObject("user"), currentId);
createdAt = StringTools.getTime1(json.optString("created_at", ""));
title = json.optString("name", "");
@ -57,6 +49,11 @@ public class UserListV1 implements UserList {
subscriberCount = json.optInt("subscriber_count");
isPrivate = PRIVATE.equals(json.optString("mode"));
following = json.optBoolean("following");
try {
id = Long.parseLong(idStr);
} catch (NumberFormatException e) {
throw new JSONException("bad userlist ID: " + idStr);
}
}
@Override

View File

@ -9,8 +9,6 @@ import org.json.JSONObject;
import org.nuclearfog.twidda.backend.utils.StringTools;
import org.nuclearfog.twidda.model.User;
import java.util.regex.Pattern;
/**
* API 1.1 implementation of User
*
@ -20,8 +18,6 @@ public class UserV1 implements User {
public static final long serialVersionUID = 7893496988800499358L;
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
private long id;
private long created;
private String username;
@ -79,9 +75,9 @@ public class UserV1 implements User {
} else {
this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
}
if (ID_PATTERN.matcher(idStr).matches()) {
try {
id = Long.parseLong(idStr);
} else {
} catch (NumberFormatException e) {
throw new JSONException("bad user ID: " + idStr);
}
}

View File

@ -9,8 +9,6 @@ import org.json.JSONObject;
import org.nuclearfog.twidda.backend.utils.StringTools;
import org.nuclearfog.twidda.model.User;
import java.util.regex.Pattern;
/**
* User implementation of API V2
*
@ -20,8 +18,6 @@ public class UserV2 implements User {
public static final long serialVersionUID = 1136243062864162774L;
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
/**
* extra parameters required to fetch additional data
*/
@ -76,9 +72,9 @@ public class UserV2 implements User {
} else {
this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
}
if (ID_PATTERN.matcher(idStr).matches()) {
try {
id = Long.parseLong(idStr);
} else {
} catch (NumberFormatException e) {
throw new JSONException("bad user ID: " + idStr);
}
}

View File

@ -228,7 +228,6 @@
<string name="confirm_delete_list">delete userlist?</string>
<string name="confirm_delete_message">delete message?</string>
<string name="confirm_unfollow_list">unfollow list?</string>
<string name="trend_range_tweets">Tweets</string>
<string name="trend_range">Posts</string>
<string name="enter_username">enter username</string>
<string name="profile_banner">Banner Image</string>