From 187693883c02388c3a777dea3a6e7e6f0dbd5e3b Mon Sep 17 00:00:00 2001 From: Grishka Date: Mon, 21 Nov 2022 14:10:30 +0400 Subject: [PATCH] Fix #94 TODO support 4.0 filteing --- .../org/joinmastodon/android/api/CacheController.java | 6 +++--- .../android/fragments/HomeTimelineFragment.java | 2 +- .../joinmastodon/android/fragments/ThreadFragment.java | 2 +- .../main/java/org/joinmastodon/android/model/Filter.java | 4 ++++ .../main/java/org/joinmastodon/android/model/Status.java | 8 ++++++++ .../joinmastodon/android/utils/StatusFilterPredicate.java | 3 +-- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java index 830b5a19a..7ec87d63f 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java @@ -73,7 +73,7 @@ public class CacheController{ status.hasGapAfter=((flags & POST_FLAG_GAP_AFTER)!=0); newMaxID=status.id; for(Filter filter:filters){ - if(filter.matches(status.getContentStatus().content)) + if(filter.matches(status)) continue outer; } result.add(status); @@ -145,7 +145,7 @@ public class CacheController{ newMaxID=ntf.id; if(ntf.status!=null){ for(Filter filter:filters){ - if(filter.matches(ntf.status.getContentStatus().content)) + if(filter.matches(ntf.status)) continue outer; } } @@ -166,7 +166,7 @@ public class CacheController{ callback.onSuccess(new PaginatedResponse<>(result.stream().filter(ntf->{ if(ntf.status!=null){ for(Filter filter:filters){ - if(filter.matches(ntf.status.getContentStatus().content)){ + if(filter.matches(ntf.status)){ return false; } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java index 532d47dd9..8cfc5347e 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java @@ -257,7 +257,7 @@ public class HomeTimelineFragment extends StatusListFragment{ if(idsBelowGap.contains(s.id)) break; for(Filter filter:filters){ - if(filter.matches(s.getContentStatus().content)){ + if(filter.matches(s)){ continue outer; } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ThreadFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ThreadFragment.java index 22d0eda16..4578617c4 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ThreadFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ThreadFragment.java @@ -97,7 +97,7 @@ public class ThreadFragment extends StatusListFragment{ return statuses; return statuses.stream().filter(status->{ for(Filter filter:filters){ - if(filter.matches(status.getContentStatus().content)) + if(filter.matches(status)) return false; } return true; diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/Filter.java b/mastodon/src/main/java/org/joinmastodon/android/model/Filter.java index c1a3ee879..8a629a290 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/Filter.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/Filter.java @@ -50,6 +50,10 @@ public class Filter extends BaseModel{ return pattern.matcher(text).find(); } + public boolean matches(Status status){ + return matches(status.getContentStatus().getStrippedText()); + } + @Override public String toString(){ return "Filter{"+ diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/Status.java b/mastodon/src/main/java/org/joinmastodon/android/model/Status.java index 47eeb641f..dbc3bfd5e 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/Status.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/Status.java @@ -3,6 +3,7 @@ package org.joinmastodon.android.model; import org.joinmastodon.android.api.ObjectValidationException; import org.joinmastodon.android.api.RequiredField; import org.joinmastodon.android.events.StatusCountersUpdatedEvent; +import org.joinmastodon.android.ui.text.HtmlParser; import org.parceler.Parcel; import java.time.Instant; @@ -56,6 +57,7 @@ public class Status extends BaseModel implements DisplayItemsParent{ public transient boolean spoilerRevealed; public transient boolean hasGapAfter; + private transient String strippedText; @Override public void postprocess() throws ObjectValidationException{ @@ -132,4 +134,10 @@ public class Status extends BaseModel implements DisplayItemsParent{ public Status getContentStatus(){ return reblog!=null ? reblog : this; } + + public String getStrippedText(){ + if(strippedText==null) + strippedText=HtmlParser.strip(content); + return strippedText; + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/utils/StatusFilterPredicate.java b/mastodon/src/main/java/org/joinmastodon/android/utils/StatusFilterPredicate.java index 1974c9b67..cf9e0829f 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/utils/StatusFilterPredicate.java +++ b/mastodon/src/main/java/org/joinmastodon/android/utils/StatusFilterPredicate.java @@ -21,9 +21,8 @@ public class StatusFilterPredicate implements Predicate{ @Override public boolean test(Status status){ - CharSequence content=status.getContentStatus().content; for(Filter filter:filters){ - if(filter.matches(content)) + if(filter.matches(status)) return false; } return true;