added media content support for Mastodon status

This commit is contained in:
nuclearfog 2022-11-28 23:56:39 +01:00
parent b0c9f0efe2
commit 2da277da8a
No known key found for this signature in database
GPG Key ID: 03488A185C476379
1 changed files with 46 additions and 2 deletions

View File

@ -27,10 +27,12 @@ public class MastodonStatus implements Status {
private long replyUserId; private long replyUserId;
private long createdAt; private long createdAt;
private int mediaType;
private int replyCount, favoriteCount, reblogCount; private int replyCount, favoriteCount, reblogCount;
private boolean favorited, reblogged, sensitive; private boolean favorited, reblogged, sensitive;
private String text, source, mentions; private String text, source, mentions;
private String[] mediaLinks;
private User author; private User author;
@ -55,6 +57,7 @@ public class MastodonStatus implements Status {
text = json.optString("content", ""); text = json.optString("content", "");
text = Jsoup.parse(text).text(); text = Jsoup.parse(text).text();
sensitive = json.optBoolean("sensitive", false); sensitive = json.optBoolean("sensitive", false);
mediaLinks = getMediaLinks(json);
if (mentionsJson != null) { if (mentionsJson != null) {
StringBuilder mentionsBuilder = new StringBuilder(); StringBuilder mentionsBuilder = new StringBuilder();
for (int i = 0; i < mentionsJson.length(); i++) { for (int i = 0; i < mentionsJson.length(); i++) {
@ -164,7 +167,10 @@ public class MastodonStatus implements Status {
@NonNull @NonNull
@Override @Override
public Uri[] getMediaUris() { public Uri[] getMediaUris() {
return new Uri[0]; Uri[] result = new Uri[mediaLinks.length];
for (int i = 0; i < result.length; i++)
result[i] = Uri.parse(mediaLinks[i]);
return result;
} }
@ -176,7 +182,7 @@ public class MastodonStatus implements Status {
@Override @Override
public int getMediaType() { public int getMediaType() {
return MEDIA_NONE; return mediaType;
} }
@ -229,4 +235,42 @@ public class MastodonStatus implements Status {
return false; return false;
return ((Status) obj).getId() == id; return ((Status) obj).getId() == id;
} }
/**
* get medialinks from json
*
* @return media link array
*/
private String[] getMediaLinks(JSONObject json) {
try {
JSONArray attachments = json.getJSONArray("media_attachments");
String[] result = new String[attachments.length()];
if (result.length > 0) {
String type = attachments.getJSONObject(0).getString("type");
switch (type) {
case "image":
mediaType = MEDIA_PHOTO;
break;
case "gifv":
mediaType = MEDIA_GIF;
break;
case "video":
mediaType = MEDIA_VIDEO;
break;
default:
mediaType = MEDIA_NONE;
}
for (int i = 0; i < result.length; i++) {
JSONObject item = attachments.getJSONObject(i);
result[i] = item.optString("url", "");
}
}
return result;
} catch (JSONException e) {
return new String[0];
}
}
} }