From 75c9a52cf7c91c37bb3b350db1ce8b664822b250 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 10 Apr 2021 22:54:12 +0800 Subject: Hide post selftext if it contains spoiler in post feed. --- .../infinityforreddit/post/ParsePost.java | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 30118395..9df78a45 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; @@ -176,7 +178,8 @@ public class ParsePost { if (data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { - post.setSelfText(Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim())); + String selfText = Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim()); + post.setSelfText(selfText); if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { post.setSelfTextPlainTrimmed(""); } else { @@ -186,7 +189,17 @@ public class ParsePost { if (selfTextPlain.length() > 250) { selfTextPlain = selfTextPlain.substring(0, 250); } - post.setSelfTextPlainTrimmed(selfTextPlain); + if (!selfText.equals("")) { + Pattern p = Pattern.compile(">!.+!<"); + Matcher m = p.matcher(selfText.substring(0, Math.min(selfText.length(), 400))); + if (m.find()) { + post.setSelfTextPlainTrimmed(""); + } else { + post.setSelfTextPlainTrimmed(selfTextPlain); + } + } else { + post.setSelfTextPlainTrimmed(selfTextPlain); + } } } } else { @@ -320,7 +333,8 @@ public class ParsePost { if (data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { - post.setSelfText(Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim())); + String selfText = Utils.modifyMarkdown(data.getString(JSONUtils.SELFTEXT_KEY).trim()); + post.setSelfText(selfText); if (data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { post.setSelfTextPlainTrimmed(""); } else { @@ -330,7 +344,17 @@ public class ParsePost { if (selfTextPlain.length() > 250) { selfTextPlain = selfTextPlain.substring(0, 250); } - post.setSelfTextPlainTrimmed(selfTextPlain); + if (!selfText.equals("")) { + Pattern p = Pattern.compile(">!.+!<"); + Matcher m = p.matcher(selfText.substring(0, Math.min(selfText.length(), 400))); + if (m.find()) { + post.setSelfTextPlainTrimmed(""); + } else { + post.setSelfTextPlainTrimmed(selfTextPlain); + } + } else { + post.setSelfTextPlainTrimmed(selfTextPlain); + } } } } else { -- cgit v1.2.3