renamed classes, added comments, removed unused methods

This commit is contained in:
nuclearfog 2022-12-20 10:29:03 +01:00
parent 16ca1a6b6a
commit ec928dee70
No known key found for this signature in database
GPG Key ID: 03488A185C476379
5 changed files with 111 additions and 83 deletions

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.JSONException;
import org.json.JSONObject;
@ -22,7 +24,9 @@ public class MastodonPoll implements Poll {
private int voteCount;
private MastodonOption[] options;
/**
* @param json Mastodon poll jswon format
*/
public MastodonPoll(JSONObject json) throws JSONException {
String idStr = json.getString("id");
String exTimeStr = json.getString("expires_at");
@ -89,8 +93,20 @@ public class MastodonPoll implements Poll {
return options;
}
@NonNull
@Override
public String toString() {
StringBuilder optionsBuf = new StringBuilder(" options=(");
for (Option option : options) {
optionsBuf.append(option).append(',');
}
optionsBuf.deleteCharAt(optionsBuf.length() - 1).append(')');
return "id=" + id + " expired=" + expired + " options=" + optionsBuf;
}
/**
*
* Mastodon poll option implementation
*/
private static class MastodonOption implements Option {
@ -100,7 +116,10 @@ public class MastodonPoll implements Poll {
private int voteCount;
private boolean selected = false;
MastodonOption(JSONObject json) throws JSONException {
/**
* @param json mastodon poll json format
*/
private MastodonOption(JSONObject json) throws JSONException {
voteCount = json.getInt("votes_count");
title = json.getString("title");
}
@ -123,10 +142,17 @@ public class MastodonPoll implements Poll {
return selected;
}
@NonNull
@Override
public String toString() {
return "title=\"" + title + "\" votes=" + voteCount + " selected=" + selected;
}
/**
* mark this option as selected
*/
void setSelected() {
private void setSelected() {
selected = true;
}
}

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import androidx.annotation.NonNull;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.Metrics;
@ -12,11 +14,7 @@ public class MetricsV2 implements Metrics {
private static final long serialVersionUID = -305086994844228862L;
private long statusId;
private int impressions;
private int retweets;
private int likes;
private int replies;
private int quotes;
private int linkClicks;
private int profileClicks;
@ -25,24 +23,13 @@ public class MetricsV2 implements Metrics {
/**
* @param metricsPublic json of public metrics
* @param nonPublicMetrics json of non public metrics
* @param statusId Id of the status
*/
public MetricsV2(JSONObject metricsPublic, JSONObject nonPublicMetrics, long statusId) {
public MetricsV2(JSONObject metricsPublic, JSONObject nonPublicMetrics) {
impressions = nonPublicMetrics.optInt("impression_count", 0);
retweets = nonPublicMetrics.optInt("retweet_count", 0);
likes = nonPublicMetrics.optInt("like_count", 0);
replies = nonPublicMetrics.optInt("reply_count", 0);
quotes = metricsPublic.optInt("quote_count", 0);
linkClicks = nonPublicMetrics.optInt("url_link_clicks", 0);
profileClicks = nonPublicMetrics.optInt("user_profile_clicks", 0);
videoViews = nonPublicMetrics.optInt("view_count", 0);
this.statusId = statusId;
}
@Override
public long getStatusId() {
return statusId;
}
@ -52,24 +39,6 @@ public class MetricsV2 implements Metrics {
}
@Override
public int getReposts() {
return retweets;
}
@Override
public int getFavorits() {
return likes;
}
@Override
public int getReplies() {
return replies;
}
@Override
public int getQuoteCount() {
return quotes;
@ -92,4 +61,12 @@ public class MetricsV2 implements Metrics {
public int getVideoViews() {
return videoViews;
}
@NonNull
@Override
public String toString() {
return "impressions=" + impressions + " profile_clicks=" + profileClicks +
" link_clicks=" + linkClicks + " quotes=" + quotes + "video_views=" + videoViews;
}
}

View File

@ -27,11 +27,35 @@ public class TweetV2 implements Status {
private static final long serialVersionUID = -2740140825640061692L;
/**
* parameter to add user object
*/
public static final String FIELDS_USER = UserV2.PARAMS;
public static final String FIELDS_TWEET = "id%2Ctext%2Cattachments%2Cconversation_id%2Centities%2Cpublic_metrics%2Creply_settings%2Cgeo%2Csource%2Cpossibly_sensitive";
public static final String FIELDS_TWEET_PRIVATE = FIELDS_TWEET + "%2Cnon_public_metrics";
public static final String FIELDS_POLL ="duration_minutes%2Cend_datetime%2Cid%2Coptions%2Cvoting_status";
/**
* fields to enable tweet expansions with extra information
*/
public static final String FIELDS_EXPANSION = "attachments.poll_ids%2Cauthor_id%2Creferenced_tweets.id%2Cattachments.media_keys";
/**
* default tweet fields
*/
public static final String FIELDS_TWEET = "id%2Ctext%2Cattachments%2Cconversation_id%2Centities%2Cpublic_metrics%2Creply_settings%2Cgeo%2Csource%2Cpossibly_sensitive";
/**
* default tweet fields with non public metrics
* (only valid if current user is the author of this tweet, the tweet isn't a retweet and the tweet isn't older than 30 days)
*/
public static final String FIELDS_TWEET_PRIVATE = FIELDS_TWEET + "%2Cnon_public_metrics";
/**
* fields to add twitter poll object
*/
public static final String FIELDS_POLL ="duration_minutes%2Cend_datetime%2Cid%2Coptions%2Cvoting_status";
/**
* fields to add extra media information
*/
public static final String FIELDS_MEDIA = "media_key%2Cpreview_image_url%2Ctype%2Curl";
private long id;
@ -109,11 +133,11 @@ public class TweetV2 implements Status {
}
// add poll
if (polls != null && polls.length() > 0) {
poll = new TweetPoll(polls.getJSONObject(0));
poll = new TwitterPoll(polls.getJSONObject(0));
}
// add metrics
if (nonPublicMetrics != null) {
metrics = new MetricsV2(publicMetrics, nonPublicMetrics, id);
metrics = new MetricsV2(publicMetrics, nonPublicMetrics);
}
// add mentioned usernames
if (mentionsJson != null && mentionsJson.length() > 0) {

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import androidx.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -11,7 +13,7 @@ import org.nuclearfog.twidda.model.Poll;
*
* @author nuclearfog
*/
public class TweetPoll implements Poll {
public class TwitterPoll implements Poll {
private static final long serialVersionUID = 4587084581361253962L;
@ -23,18 +25,21 @@ public class TweetPoll implements Poll {
private Option[] options;
private int count = 0;
public TweetPoll(JSONObject json) throws JSONException {
/**
* @param json tweet poll json format
*/
public TwitterPoll(JSONObject json) throws JSONException {
JSONArray optionsJson = json.getJSONArray("options");
String idStr = json.getString("id");
expired = VOTE_CLOSED.equals(json.getString("voting_status"));
expiredAt = StringTools.getTime(json.optString("end_datetime"), StringTools.TIME_TWITTER_V2);
// add options
options = new Option[optionsJson.length()];
for (int i = 0 ; i < optionsJson.length() ; i++) {
options[i] = new TweetOption(optionsJson.getJSONObject(i));
options[i] = new TwitterPollOption(optionsJson.getJSONObject(i));
count += options[i].getVotes();
}
// add ID
try {
id = Long.parseLong(idStr);
} catch (NumberFormatException e) {
@ -79,27 +84,42 @@ public class TweetPoll implements Poll {
return options;
}
@NonNull
@Override
public String toString() {
StringBuilder optionsBuf = new StringBuilder(" options=(");
for (Option option : options) {
optionsBuf.append(option).append(',');
}
optionsBuf.deleteCharAt(optionsBuf.length() - 1).append(')');
return "id=" + id + " expired=" + expired + " options=" + optionsBuf;
}
/**
*
* implementation of a poll option
*/
private static class TweetOption implements Option {
private static class TwitterPollOption implements Option {
private static final long serialVersionUID = -7594109890754209971L;
private String name;
private String title;
private int voteCount;
private boolean voted;
private boolean selected;
TweetOption(JSONObject json) throws JSONException {
name = json.getString("label");
/**
* @param json Twitter poll option json
*/
private TwitterPollOption(JSONObject json) throws JSONException {
title = json.getString("label");
voteCount = json.getInt("votes");
voted = false; // todo implement this
selected = false; // todo implement this
}
@Override
public String getTitle() {
return name;
return title;
}
@ -111,7 +131,14 @@ public class TweetPoll implements Poll {
@Override
public boolean selected() {
return voted;
return selected;
}
@NonNull
@Override
public String toString() {
return "title=\"" + title + "\" votes=" + voteCount + " selected=" + selected;
}
}
}

View File

@ -9,11 +9,6 @@ import java.io.Serializable;
*/
public interface Metrics extends Serializable {
/**
* @return Id of the status
*/
long getStatusId();
/**
* get view count of the status
*
@ -21,27 +16,6 @@ public interface Metrics extends Serializable {
*/
int getViews();
/**
* get repost count
*
* @return repost count
*/
int getReposts();
/**
* get like/favorite count
*
* @return like count
*/
int getFavorits();
/**
* get reply count
*
* @return reply count
*/
int getReplies();
/**
* get number of quotes
*