Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Quote): hide quotes of accounts that are muted/blocked or filtered. #490

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +17,10 @@
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.AccountSession;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.fragments.BaseStatusListFragment;
import org.joinmastodon.android.fragments.HashtagTimelineFragment;
Expand All @@ -35,6 +37,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 @@ -339,7 +342,7 @@ public static ArrayList<StatusDisplayItem> buildItems(BaseStatusListFragment<?>
contentItems.add(new DummyStatusDisplayItem(parentID, fragment));
contentItems.addAll(buildItems(fragment, statusForContent.quote, accountID, parentObject, knownAccounts, filterContext, FLAG_NO_FOOTER|FLAG_INSET|FLAG_NO_EMOJI_REACTIONS|FLAG_IS_FOR_QUOTE));
} else if((flags & FLAG_INSET)==0 && statusForContent.mediaAttachments.isEmpty()){
tryAddNonOfficialQuote(statusForContent, fragment, accountID);
tryAddNonOfficialQuote(statusForContent, fragment, accountID, filterContext);
}
if(contentItems!=items && statusForContent.spoilerRevealed){
items.addAll(contentItems);
Expand Down Expand Up @@ -421,29 +424,54 @@ public static void buildPollItems(String parentID, BaseStatusListFragment fragme
* Tries to adds a non-official quote to a status.
* A non-official quote is a quote on an instance that does not support quotes officially.
*/
private static void tryAddNonOfficialQuote(Status status, BaseStatusListFragment fragment, String accountID) {
private static void tryAddNonOfficialQuote(Status status, BaseStatusListFragment fragment, String accountID, FilterContext filterContext) {
Matcher matcher=QUOTE_PATTERN.matcher(status.getStrippedText());

if(!matcher.find())
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){
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);
String selfId=AccountSessionManager.get(accountID).self.id;
if(!status.account.id.equals(selfId) && (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
Loading