added group stories adblock

This commit is contained in:
gdlbo
2023-02-13 18:57:29 +03:00
parent b44dae69a6
commit e3c694d7c3
9 changed files with 153 additions and 46 deletions
@@ -252,6 +252,30 @@ public class AdBlockHook {
return items;
}
private static void parseStoriesItem(JSONObject item) throws JSONException {
var stories = item.optJSONArray("stories");
var newStories = new JSONArray();
if (stories == null) return;
for (int j = 0; j < stories.length(); j++) {
var story = stories.optJSONObject(j);
if (!story.optBoolean("is_ads") && !story.optBoolean("is_promo")) {
if (isStoryNotAd(story)) {
newStories.put(story);
}
} else {
if (dev()) Log.d("StoriesAds", "Fetched ad, owner id " + story.optString("owner_id") + ", caption " + story.optString("caption"));
}
}
item.put("stories", newStories);
}
private static Boolean isStoryNotAd(JSONObject json) {
return !getBoolValue("storiesGroupsAdBlock", false) || (json.has("link") ? isWhitelistedAdStories(json) : true);
}
public static JSONObject storiesads(JSONObject json, boolean isDeleteFix) throws JSONException {
if (!adsstories()) {
@@ -277,6 +301,16 @@ public class AdBlockHook {
}
}
var items = json.optJSONArray("items");
if (items != null) {
for (int i = 0; i < items.length(); i++) {
var item = items.optJSONObject(i);
if (item != null) {
parseStoriesItem(item);
}
}
}
return json;
}
@@ -340,6 +374,17 @@ public class AdBlockHook {
return whitelist.contains(id);
}
public static Boolean isWhitelistedAdStories(JSONObject list) {
var id = String.valueOf(list.optInt("owner_id"));
var whitelist = AndroidUtils.getDefaultPrefs().getStringSet(
"whitelisted_stories_ad",
Collections.emptySet()
);
return whitelist.contains(id);
}
public static Boolean hasMiniAppAds(JSONObject list) {
var attachments = list.optJSONArray("attachments");
@@ -79,6 +79,13 @@ public class NewsfeedHook {
).contains(String.valueOf(AccountManagerUtils.getUserID(eup)));
}
public static boolean isWhitelistedAdStories(ExtendedCommunityProfile eup) {
return getDefaultPrefs().getStringSet(
"whitelisted_stories_ad",
Collections.emptySet()
).contains(String.valueOf(AccountManagerUtils.getUserID(eup)));
}
public static void setWhitelistedFilter(ExtendedCommunityProfile eup, boolean needWhitelist) {
var id = String.valueOf(AccountManagerUtils.getUserID(eup));
@@ -111,6 +118,22 @@ public class NewsfeedHook {
getDefaultPrefs().edit().putStringSet("whitelisted_ad_groups", mutableAdsSet).apply();
}
public static void setWhitelistedAdStories(ExtendedCommunityProfile eup, boolean needWhitelist) {
var id = String.valueOf(AccountManagerUtils.getUserID(eup));
var mutableAdsSet = new LinkedHashSet<>(getDefaultPrefs().getStringSet(
"whitelisted_stories_ad",
Collections.emptySet()));
if (needWhitelist) {
mutableAdsSet.add(id);
} else {
mutableAdsSet.remove(id);
}
getDefaultPrefs().edit().putStringSet("whitelisted_stories_ad", mutableAdsSet).apply();
}
public static void adsParams(HashSet<String> hashSet) {
if (ads()) {
hashSet.add("ads_disabled");
@@ -64,6 +64,12 @@ public class FeedFragment extends MaterialPreferenceToolbarFragment {
findPreference("whitelisted_ad_groups").setSummary(countSet("whitelisted_ad_groups"));
findPreference("whitelisted_filters_groups").setSummary(countSet("whitelisted_filters_groups"));
findPreference("whitelisted_stories_ad").setSummary(countSet("whitelisted_stories_ad"));
findPreference("whitelisted_stories_ad").setOnPreferenceClickListener(preference -> {
remdialog("whitelisted_stories_ad", getContext());
return true;
});
findPreference("whitelisted_ad_groups").setOnPreferenceClickListener(preference -> {
remdialog("whitelisted_ad_groups", getContext());
@@ -1,8 +1,7 @@
package ru.vtosters.lite.ui.vkui;
import static ru.vtosters.lite.foaf.FoafBase.loadAndShow;
import static ru.vtosters.lite.hooks.NewsfeedHook.setWhitelistedAd;
import static ru.vtosters.lite.hooks.NewsfeedHook.setWhitelistedFilter;
import static ru.vtosters.lite.hooks.NewsfeedHook.*;
import static ru.vtosters.lite.utils.AccountManagerUtils.getUserID;
import static ru.vtosters.lite.utils.AndroidUtils.sendToast;
@@ -85,6 +84,7 @@ public class MenuBuilder {
final Context ctx = ((View) apView.get(builder)).getContext();
final boolean isPageWhitelistedFilter = NewsfeedHook.isWhitelistedFilter(ecp);
final boolean isPageWhitelistedAds = NewsfeedHook.isWhitelistedAd(ecp);
final boolean isPageWhitelistedAdsStories = NewsfeedHook.isWhitelistedAdStories(ecp);
addItem(builder, AndroidUtils.getString(R.string.menu_copy_id), () -> {
copy(ctx, String.valueOf(getUserID(ecp)));
@@ -108,6 +108,12 @@ public class MenuBuilder {
return Unit.a;
});
addItem(builder, AndroidUtils.getString(isPageWhitelistedAdsStories ? R.string.remove_from_ads_stories_whitelist : R.string.add_to_ads_stories_whitelist), () -> {
setWhitelistedAdStories(ecp, !isPageWhitelistedAdsStories);
sendToast(AndroidUtils.getString(isPageWhitelistedAdsStories ? R.string.removed_from_whitelist_success : R.string.added_to_whitelist_success));
return Unit.a;
});
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}