Skip to content

Commit

Permalink
Merge pull request #399 from FineFindus/fix/apply-filter-highlight
Browse files Browse the repository at this point in the history
fix: apply filter highlight
  • Loading branch information
LucasGGamerM authored May 11, 2024
2 parents 96c1c03 + d1d8f2e commit 9332172
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 251 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.joinmastodon.android.api.requests.oauth.RevokeOauthToken;
import org.joinmastodon.android.events.NotificationsMarkerUpdatedEvent;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.AltTextFilter;
import org.joinmastodon.android.model.Application;
import org.joinmastodon.android.model.FilterAction;
import org.joinmastodon.android.model.FilterContext;
Expand All @@ -37,6 +38,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -314,7 +316,7 @@ public <T> boolean filterStatusContainingObject(T object, Function<T, Status> ex
// Even with server-side filters, clients are expected to remove statuses that match a filter that hides them
if(getLocalPreferences().serverSideFiltersSupported){
for(FilterResult filter : s.filtered){
if(filter.filter.isActive() && filter.filter.filterAction==FilterAction.HIDE)
if(filter.filter.isActive() && filter.filter.filterAction==FilterAction.HIDE && filter.filter.context.contains(context))
return true;
}
}else if(wordFilters!=null){
Expand All @@ -326,6 +328,21 @@ public <T> boolean filterStatusContainingObject(T object, Function<T, Status> ex
return false;
}

public List<FilterResult> getClientSideFilters(Status status) {
List<FilterResult> filters = List.of();

// filter post that have no alt text
// it only applies when activated in the settings
AltTextFilter altTextFilter=new AltTextFilter(FilterAction.WARN, EnumSet.allOf(FilterContext.class));
if(altTextFilter.matches(status)){
FilterResult filterResult=new FilterResult();
filterResult.filter=altTextFilter;
filterResult.keywordMatches=List.of();
filters.add(filterResult);
}
return filters;
}

public void updateAccountInfo(){
AccountSessionManager.getInstance().updateSessionLocalInfo(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@

import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.timelines.GetPublicTimeline;
import org.joinmastodon.android.model.Filter;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.FilterContext;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.model.TimelineDefinition;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.utils.ProvidesAssistContent;
import org.joinmastodon.android.utils.StatusFilterPredicate;

import java.util.List;
import java.util.stream.Collectors;

import me.grishka.appkit.api.SimpleCallback;

Expand Down Expand Up @@ -53,7 +51,7 @@ public void onSuccess(List<Status> result){
if(!result.isEmpty())
maxID=result.get(result.size()-1).id;
if (getActivity() == null) return;
result=result.stream().filter(new StatusFilterPredicate(accountID, FilterContext.PUBLIC)).collect(Collectors.toList());
AccountSessionManager.get(accountID).filterStatuses(result, FilterContext.PUBLIC);
result.stream().forEach(status -> {
status.account.acct += "@"+domain;
status.mentions.forEach(mention -> mention.id = null);
Expand Down Expand Up @@ -82,7 +80,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

@Override
protected FilterContext getFilterContext() {
return null;
return FilterContext.PUBLIC;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package org.joinmastodon.android.model;

import org.joinmastodon.android.GlobalUserPreferences;
import org.jsoup.internal.StringUtil;

import java.util.EnumSet;

public class AltTextFilter extends LegacyFilter {

public AltTextFilter(FilterAction filterAction, FilterContext firstContext, FilterContext... restContexts) {
public AltTextFilter(FilterAction filterAction, EnumSet<FilterContext> filterContexts) {
this.filterAction = filterAction;
isRemote = false;
context = EnumSet.of(firstContext, restContexts);
context = filterContexts;
}

@Override
public boolean matches(Status status) {
return status.getContentStatus().mediaAttachments.stream().map(attachment -> attachment.description).anyMatch(StringUtil::isBlank);
}

@Override
public boolean isActive(){
return !GlobalUserPreferences.showPostsWithoutAlt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Attachment;
import org.joinmastodon.android.model.DisplayItemsParent;
import org.joinmastodon.android.model.FilterAction;
import org.joinmastodon.android.model.LegacyFilter;
import org.joinmastodon.android.model.FilterContext;
import org.joinmastodon.android.model.FilterResult;
import org.joinmastodon.android.model.LegacyFilter;
import org.joinmastodon.android.model.Notification;
import org.joinmastodon.android.model.Poll;
import org.joinmastodon.android.model.ScheduledStatus;
Expand All @@ -40,7 +39,6 @@
import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.viewholders.AccountViewHolder;
import org.joinmastodon.android.utils.StatusFilterPredicate;
import org.parceler.Parcels;

import java.util.ArrayList;
Expand Down Expand Up @@ -168,10 +166,6 @@ public static ArrayList<StatusDisplayItem> buildItems(BaseStatusListFragment<?>
args.putString("account", accountID);
ScheduledStatus scheduledStatus = parentObject instanceof ScheduledStatus s ? s : null;

// Hide statuses that have a filter action of hide
if(!new StatusFilterPredicate(accountID, filterContext, FilterAction.HIDE).test(status))
return new ArrayList<StatusDisplayItem>() ;

HeaderStatusDisplayItem header=null;
boolean hideCounts=!AccountSessionManager.get(accountID).getLocalPreferences().showInteractionCounts;

Expand Down Expand Up @@ -233,20 +227,16 @@ public static ArrayList<StatusDisplayItem> buildItems(BaseStatusListFragment<?>

LegacyFilter applyingFilter=null;
if(status.filtered!=null){
for(FilterResult filter:status.filtered){
List<FilterResult> filters = status.filtered;
filters.addAll(AccountSessionManager.get(accountID).getClientSideFilters(status));

for(FilterResult filter:filters){
LegacyFilter f=filter.filter;
if(f.isActive() && filterContext != null && f.context.contains(filterContext)){
applyingFilter=f;
break;
}
}

// Moshidon
if(applyingFilter==null){
StatusFilterPredicate predicate = new StatusFilterPredicate(accountID, filterContext, FilterAction.WARN);
predicate.test(status);
applyingFilter = predicate.getApplyingFilter();
}
}

ArrayList<StatusDisplayItem> contentItems;
Expand All @@ -273,8 +263,9 @@ public static ArrayList<StatusDisplayItem> buildItems(BaseStatusListFragment<?>
boolean hasSpoiler=!TextUtils.isEmpty(statusForContent.spoilerText);
if(!TextUtils.isEmpty(statusForContent.content)){
SpannableStringBuilder parsedText=HtmlParser.parse(statusForContent.content, statusForContent.emojis, statusForContent.mentions, statusForContent.tags, accountID, fragment.getContext());
HtmlParser.applyFilterHighlights(fragment.getActivity(), parsedText, status.filtered);
TextStatusDisplayItem text=new TextStatusDisplayItem(parentID, HtmlParser.parse(statusForContent.content, statusForContent.emojis, statusForContent.mentions, statusForContent.tags, accountID, fragment.getContext()), fragment, statusForContent, (flags & FLAG_NO_TRANSLATE) != 0);
if(applyingFilter!=null)
HtmlParser.applyFilterHighlights(fragment.getActivity(), parsedText, status.filtered);
TextStatusDisplayItem text=new TextStatusDisplayItem(parentID, parsedText, fragment, statusForContent, (flags & FLAG_NO_TRANSLATE) != 0);
contentItems.add(text);
}else if(!hasSpoiler && header!=null){
header.needBottomPadding=true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,11 @@ public static CharSequence parseLinks(String text){
}

public static void applyFilterHighlights(Context context, SpannableStringBuilder text, List<FilterResult> filters){
if (filters == null) return;
int fgColor=UiUtils.getThemeColor(context, R.attr.colorM3Error);
int bgColor=UiUtils.getThemeColor(context, R.attr.colorM3ErrorContainer);
for(FilterResult filter:filters){
if(!filter.filter.isActive())
continue;;
continue;
for(String word:filter.keywordMatches){
Matcher matcher=Pattern.compile("\\b"+Pattern.quote(word)+"\\b", Pattern.CASE_INSENSITIVE).matcher(text);
while(matcher.find()){
Expand Down
Loading

0 comments on commit 9332172

Please sign in to comment.