Add support for parsing inline static emotes (#589)

This commit is contained in:
scria1000 2021-12-15 13:43:01 +00:00 committed by GitHub
parent ae63e20764
commit e4c64668db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

View File

@ -1,5 +1,9 @@
package ml.docilealligator.infinityforreddit.comment;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_DOWNVOTE;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_NO_VOTE;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPVOTE;
import android.os.Handler;
import android.text.Html;
@ -15,10 +19,6 @@ import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_DOWNVOTE;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_NO_VOTE;
import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPVOTE;
public class ParseComment {
public static void parseComment(Executor executor, Handler handler, String response,
ArrayList<Comment> commentData, boolean expandChildren,
@ -189,6 +189,10 @@ public class ParseComment {
String commentMarkdown = "";
if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) {
commentMarkdown = Utils.parseInlineGifInComments(Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim()));
if (!singleCommentData.isNull(JSONUtils.MEDIA_METADATA_KEY)) {
JSONObject mediaMetadataObject = singleCommentData.getJSONObject(JSONUtils.MEDIA_METADATA_KEY);
commentMarkdown = Utils.parseInlineEmotes(commentMarkdown, mediaMetadataObject);
}
}
String commentRawText = Utils.trimTrailingWhitespace(
Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString();
@ -229,7 +233,7 @@ public class ParseComment {
return new Comment(id, fullName, author, authorFlair, authorFlairHTMLBuilder.toString(),
linkAuthor, submitTime, commentMarkdown, commentRawText,
linkId, subredditName, parentId, score, voteType, isSubmitter, distinguished,
permalink, awardingsBuilder.toString(),depth, collapsed, hasReply, scoreHidden, saved);
permalink, awardingsBuilder.toString(), depth, collapsed, hasReply, scoreHidden, saved);
}
@Nullable

View File

@ -175,4 +175,5 @@ public class JSONUtils {
public static final String CAPTION_URL_KEY = "outbound_url";
public static final String FILES_KEY = "files";
public static final String MP4_MOBILE_KEY = "mp4-mobile";
public static final String STATUS_KEY = "status";
}

View File

@ -31,7 +31,9 @@ import androidx.core.text.HtmlCompat;
import com.bumptech.glide.Glide;
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
@ -72,7 +74,6 @@ public final class Utils {
Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)"),
Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)"),
Pattern.compile("!\\[gif]\\(emote\\|\\w+\\|\\w+\\)"),
Pattern.compile("!\\[img]\\(emote\\|\\w+\\|\\w+\\)")
};
public static String modifyMarkdown(String markdown) {
@ -178,6 +179,37 @@ public final class Utils {
return markdownStringBuilder.toString();
}
public static String parseInlineEmotes(String markdown, JSONObject mediaMetadataObject) throws JSONException {
JSONArray mediaMetadataNames = mediaMetadataObject.names();
for (int i = 0; i < mediaMetadataNames.length(); i++) {
if (!mediaMetadataNames.isNull(i)) {
String mediaMetadataKey = mediaMetadataNames.getString(i);
if (mediaMetadataObject.isNull(mediaMetadataKey)) {
continue;
}
JSONObject item = mediaMetadataObject.getJSONObject(mediaMetadataKey);
if (item.isNull(JSONUtils.STATUS_KEY)
|| !item.getString(JSONUtils.STATUS_KEY).equals("valid")
|| item.isNull(JSONUtils.ID_KEY)
|| item.isNull(JSONUtils.T_KEY)
|| item.isNull(JSONUtils.S_KEY)) {
continue;
}
String emote_type = item.getString(JSONUtils.T_KEY);
String emote_id = item.getString(JSONUtils.ID_KEY);
JSONObject s_key = item.getJSONObject(JSONUtils.S_KEY);
if (s_key.isNull(JSONUtils.U_KEY)) {
continue;
}
String emote_url = s_key.getString(JSONUtils.U_KEY);
markdown = markdown.replace("![img](" + emote_id + ")", "[[" + emote_type + "]](" + emote_url + ") ");
}
}
return markdown;
}
public static CharSequence trimTrailingWhitespace(CharSequence source) {
if (source == null) {