Skip to content

Commit

Permalink
feat(StatusDisplayItem): hide statuses with quotes of muted/blocked
Browse files Browse the repository at this point in the history
accounts

Hides Statuses with non-official quotes of accounts that are
blocked/muted. This is equivalent to how misskey handles muted quotes.

Closes #488.
  • Loading branch information
FineFindus committed Aug 3, 2024
1 parent 55259f1 commit dbef984
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
Expand Down Expand Up @@ -709,26 +710,17 @@ public void onRevealSpoilerClick(SpoilerStatusDisplayItem.Holder holder){
}

public void updateStatusWithQuote(DisplayItemsParent parent) {
int startIndex=-1;
int endIndex=-1;
for(int i=0; i<displayItems.size(); i++){
StatusDisplayItem item = displayItems.get(i);
if(item.parentID.equals(parent.getID())) {
startIndex= startIndex==-1 ? i : startIndex;
endIndex=i;
}
}

if (startIndex==-1 || endIndex==-1)
Pair<Integer, Integer> items=findAllItemsOfParent(parent);
if (items==null)
return;

// Only StatusListFragments/NotificationsListFragments can display status with quotes
assert (this instanceof StatusListFragment) || (this instanceof NotificationsListFragment);
List<StatusDisplayItem> oldItems = displayItems.subList(startIndex, endIndex+1);
List<StatusDisplayItem> oldItems = displayItems.subList(items.first, items.second+1);
List<StatusDisplayItem> newItems=this.buildDisplayItems((T) parent);
int prevSize=oldItems.size();
oldItems.clear();
displayItems.addAll(startIndex, newItems);
displayItems.addAll(items.first, newItems);

// Update the cache
final CacheController cache=AccountSessionManager.get(accountID).getCacheController();
Expand All @@ -738,8 +730,19 @@ public void updateStatusWithQuote(DisplayItemsParent parent) {
cache.updateNotification((Notification) parent);
}

adapter.notifyItemRangeRemoved(startIndex, prevSize);
adapter.notifyItemRangeInserted(startIndex, newItems.size());
adapter.notifyItemRangeRemoved(items.first, prevSize);
adapter.notifyItemRangeInserted(items.first, newItems.size());
}

public void removeStatus(DisplayItemsParent parent) {
Pair<Integer, Integer> items=findAllItemsOfParent(parent);
if (items==null)
return;

List<StatusDisplayItem> statusDisplayItems = displayItems.subList(items.first, items.second+1);
int prevSize=statusDisplayItems.size();
statusDisplayItems.clear();
adapter.notifyItemRangeRemoved(items.first, prevSize);
}

public void onVisibilityIconClick(HeaderStatusDisplayItem.Holder holder) {
Expand Down Expand Up @@ -943,6 +946,23 @@ protected <I extends StatusDisplayItem, H extends StatusDisplayItem.Holder<I>> H
return null;
}

@Nullable
protected Pair<Integer, Integer> findAllItemsOfParent(DisplayItemsParent parent){
int startIndex=-1;
int endIndex=-1;
for(int i=0; i<displayItems.size(); i++){
StatusDisplayItem item = displayItems.get(i);
if(item.parentID.equals(parent.getID())) {
startIndex= startIndex==-1 ? i : startIndex;
endIndex=i;
}
}

if(startIndex==-1 || endIndex==-1)
return null;
return Pair.create(startIndex, endIndex);
}

protected <I extends StatusDisplayItem, H extends StatusDisplayItem.Holder<I>> List<H> findAllHoldersOfType(String id, Class<H> type){
ArrayList<H> holders=new ArrayList<>();
for(int i=0;i<list.getChildCount();i++){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.annotation.NonNull;

import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships;
import org.joinmastodon.android.api.requests.search.GetSearchResults;
import org.joinmastodon.android.api.session.AccountLocalPreferences;
import org.joinmastodon.android.api.session.AccountSessionManager;
Expand All @@ -35,6 +36,7 @@
import org.joinmastodon.android.model.LegacyFilter;
import org.joinmastodon.android.model.Notification;
import org.joinmastodon.android.model.Poll;
import org.joinmastodon.android.model.Relationship;
import org.joinmastodon.android.model.ScheduledStatus;
import org.joinmastodon.android.model.SearchResults;
import org.joinmastodon.android.model.Status;
Expand Down Expand Up @@ -428,23 +430,46 @@ private static void tryAddNonOfficialQuote(Status status, BaseStatusListFragment
return;
String quoteURL=matcher.group();

if (UiUtils.looksLikeFediverseUrl(quoteURL)) {
new GetSearchResults(quoteURL, GetSearchResults.Type.STATUSES, true, null, 0, 0).setCallback(new Callback<>(){
@Override
public void onSuccess(SearchResults results){
AccountSessionManager.get(accountID).filterStatuses(results.statuses, filterContext);
if (!results.statuses.isEmpty()){
status.quote=results.statuses.get(0);
fragment.updateStatusWithQuote(status);
}
}
if (!UiUtils.looksLikeFediverseUrl(quoteURL))
return;

@Override
public void onError(ErrorResponse error){
Log.w("StatusDisplayItem", "onError: failed to find quote status with URL: " + quoteURL + " " + error);
}
}).exec(accountID);
}
new GetSearchResults(quoteURL, GetSearchResults.Type.STATUSES, true, null, 0, 0).setCallback(new Callback<>(){
@Override
public void onSuccess(SearchResults results){
AccountSessionManager.get(accountID).filterStatuses(results.statuses, filterContext);
if (results.statuses.isEmpty())
return;

Status quote=results.statuses.get(0);
new GetAccountRelationships(Collections.singletonList(quote.account.id))
.setCallback(new Callback<>(){
@Override
public void onSuccess(List<Relationship> relationships){
if(relationships.isEmpty())
return;

Relationship relationship=relationships.get(0);
if(relationship.domainBlocking || relationship.muting || relationship.blocking) {
// do not show posts that are quoting a muted/blocked user
fragment.removeStatus(status);
return;
}

status.quote=results.statuses.get(0);
fragment.updateStatusWithQuote(status);
}

@Override
public void onError(ErrorResponse error){}
})
.exec(accountID);
}

@Override
public void onError(ErrorResponse error){
Log.w("StatusDisplayItem", "onError: failed to find quote status with URL: " + quoteURL + " " + error);
}
}).exec(accountID);
}

public enum Type{
Expand Down

0 comments on commit dbef984

Please sign in to comment.