Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
refactor filtering
Browse files Browse the repository at this point in the history
hopefully also fixes #816
  • Loading branch information
sk22 committed Oct 7, 2023
1 parent d96d4dd commit c930db6
Showing 1 changed file with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
Expand Down Expand Up @@ -255,57 +254,62 @@ public <T> void filterStatusContainingObjects(List<T> objects, Function<T, Statu
filterStatusContainingObjects(objects, extractor, context, null);
}

public <T> void filterStatusContainingObjects(List<T> objects, Function<T, Status> extractor, FilterContext context, Account profile){
AccountLocalPreferences lp=getLocalPreferences();
Predicate<Status> statusIsOnOwnProfile=(s)->self != null && profile != null && s.account != null
private boolean statusIsOnOwnProfile(Status s, Account profile){
return self != null && profile != null && s.account != null
&& Objects.equals(self.id, profile.id) && Objects.equals(self.id, s.account.id);
Predicate<Status> isFilteredType=(s)->(!lp.showReplies && s.inReplyToId != null)
|| (!lp.showBoosts && s.reblog != null);
}

if(lp.serverSideFiltersSupported){
// Even with server-side filters, clients are expected to remove statuses that match a filter that hides them
objects.removeIf(o->{
Status s=extractor.apply(o);
if(s==null)
return false;
if(s.filtered==null)
return false;
// don't hide own posts in own profile
if(statusIsOnOwnProfile.test(s))
return false;
if(isFilteredType.test(s))
return true;
for(FilterResult filter:s.filtered){
if(filter.filter.isActive() && filter.filter.filterAction==FilterAction.HIDE)
return true;
}
return false;
});
return;
}
for(T obj:objects){
private boolean isFilteredType(Status s){
return (!localPreferences.showReplies && s.inReplyToId != null)
|| (!localPreferences.showBoosts && s.reblog != null);
}

public <T> void filterStatusContainingObjects(List<T> objects, Function<T, Status> extractor, FilterContext context, Account profile){
if(!localPreferences.serverSideFiltersSupported) for(T obj:objects){
Status s=extractor.apply(obj);
if(s!=null && s.filtered!=null){
lp.serverSideFiltersSupported=true;
lp.save();
return;
localPreferences.serverSideFiltersSupported=true;
localPreferences.save();
}
}

List<T> removeUs=new ArrayList<>();
for(int i=0; i<objects.size(); i++){
T o=objects.get(i);
if(filterStatusContainingObject(o, extractor, context, profile)){
Status s=extractor.apply(o);
removeUs.add(o);
if(s!=null && s.hasGapAfter && i > 0){
Status prev=extractor.apply(objects.get(i - 1));
if(prev!=null) prev.hasGapAfter=true;
}
}
}
objects.removeIf(o->{
Status s=extractor.apply(o);
if(s==null)
return false;
// don't hide own posts in own profile
if(statusIsOnOwnProfile.test(s))
return false;
if(isFilteredType.test(s))
return true;
if(wordFilters!=null) for(LegacyFilter filter:wordFilters){
objects.removeAll(removeUs);
}

public <T> boolean filterStatusContainingObject(T object, Function<T, Status> extractor, FilterContext context, Account profile){
Status s=extractor.apply(object);
if(s==null)
return false;
// don't hide own posts in own profile
if(statusIsOnOwnProfile(s, profile))
return false;
if(isFilteredType(s))
return true;
// Even with server-side filters, clients are expected to remove statuses that match a filter that hides them
if(localPreferences.serverSideFiltersSupported){
for(FilterResult filter : s.filtered){
if(filter.filter.isActive() && filter.filter.filterAction==FilterAction.HIDE)
return true;
}
}else if(wordFilters!=null){
for(LegacyFilter filter : wordFilters){
if(filter.context.contains(context) && filter.matches(s) && filter.isActive())
return true;
}
return false;
});
}
return false;
}

public void updateAccountInfo(){
Expand Down

0 comments on commit c930db6

Please sign in to comment.