Skip to content

Commit

Permalink
many fixes
Browse files Browse the repository at this point in the history
fixed needed time to get sponsorposts for newsfeed

fixed empty sponsorfeed

speedup many things

added force applying filters
  • Loading branch information
gdlbo committed Feb 3, 2024
1 parent 137133e commit feef98a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ru.vtosters.hooks.other.Preferences;
import ru.vtosters.lite.utils.AccountManagerUtils;
import ru.vtosters.sponsorpost.utils.FiltersPreferences;
import ru.vtosters.sponsorpost.utils.PostsPreferences;
Expand All @@ -15,17 +14,22 @@
import static ru.vtosters.hooks.other.Preferences.*;

public class NewsFeedFiltersUtils {
private static final String[] CUSTOM_FILTERS = {"spamfilters", "sourcenamefilter", "linkfilter"};
private static final Set<String> filters = new HashSet<>();

static {
filters.addAll(FiltersPreferences.getFiltersLists());
setCustomFilters("spamfilters");
setCustomFilters("sourcenamefilter");
setCustomFilters("linkfilter");
updateFilters();
}

public static void setCustomFilters(String list) {
if (!filters.isEmpty()) filters.addAll(Arrays.asList(getString(list).toLowerCase().split(", ")));
public static void updateFilters() {
filters.clear();

filters.addAll(FiltersPreferences.getFiltersLists());

for (String customFilter : CUSTOM_FILTERS) {
List<String> lists = List.of(getString(customFilter).toLowerCase().split("\\s*,\\s*"));
filters.addAll(lists);
}
}

public static boolean injectFiltersReposts(JSONObject obj) {
Expand Down Expand Up @@ -61,7 +65,7 @@ public static boolean checkCopyright(JSONObject json) throws JSONException {
String copyrightLink = null;
Set<String> list = filters;

if (!filters.isEmpty()) {
if (filters != null && !filters.isEmpty()) {
if (copyright != null) {
copyrightName = copyright.getString("name").toLowerCase();
copyrightLink = copyright.getString("link").toLowerCase();
Expand Down Expand Up @@ -108,8 +112,8 @@ public static boolean checkCaption(JSONObject postJson) {
}

public static JSONArray newsfeedlist(JSONArray items) throws JSONException {
var selectedItems = Preferences.getPreferences().getString("news_feed_selected_items", "");
var filtersSet = Preferences.getPreferences().getStringSet("news_feed_items_set", null);
var selectedItems = getPreferences().getString("news_feed_selected_items", "");
var filtersSet = getPreferences().getStringSet("news_feed_items_set", null);
var mutableFiltersSet = new LinkedHashSet<String>();
if (filtersSet != null)
mutableFiltersSet.addAll(filtersSet);
Expand Down Expand Up @@ -137,7 +141,7 @@ public static JSONArray newsfeedlist(JSONArray items) throws JSONException {
Log.d("NewsfeedListInj", "Unlocked " + id + " in newsfeed list");
}

Preferences.getPreferences().edit().putStringSet("news_feed_items_set", mutableFiltersSet).apply();
getPreferences().edit().putStringSet("news_feed_items_set", mutableFiltersSet).apply();

return items;
}
Expand Down Expand Up @@ -228,7 +232,7 @@ public static Boolean discoverAdBlock(JSONObject jsonObject) {
public static boolean sponsorFilters(String text) {
String textInLowerCase = text.toLowerCase();

if (!filters.isEmpty()) {
if (filters != null && !filters.isEmpty()) {
for (String adword : filters) {
if (textInLowerCase.contains(adword) && !adword.isEmpty()) {
if (dev())
Expand Down Expand Up @@ -294,44 +298,32 @@ public static Boolean isAds(JSONObject list, String type) {
}

private static int getOwnerId(JSONObject post) {
int id = post.optInt("owner_id");

if (id == 0) {
id = post.optInt("source_id");
}

return id;
return post.optInt("owner_id") != 0 ? post.optInt("owner_id") : post.optInt("source_id");
}

private static int getPostId(JSONObject post) {
int id = post.optInt("post_id");

if (id == 0) {
id = post.optInt("id");
}

return id;
return post.optInt("post_id") != 0 ? post.optInt("post_id") : post.optInt("id");
}

public static Boolean isWhitelistedFilters(JSONObject list) {
var id = String.valueOf(getOwnerId(list));

if (id.equals("-189659924") || id.equals(String.valueOf(AccountManagerUtils.getUserId()))) {
return true;
}

var whitelist = Preferences.getPreferences().getStringSet(
var whitelist = getPreferences().getStringSet(
"whitelisted_filters_groups",
Collections.emptySet()
);

return Preferences.getBoolValue("invertFilters", false) != whitelist.contains(id);
return getBoolValue("invertFilters", false) != whitelist.contains(id);
}

public static Boolean isWhitelistedAd(JSONObject list) {
var id = String.valueOf(getOwnerId(list));

var whitelist = Preferences.getPreferences().getStringSet(
var whitelist = getPreferences().getStringSet(
"whitelisted_ad_groups",
Collections.emptySet()
);
Expand All @@ -342,7 +334,7 @@ public static Boolean isWhitelistedAd(JSONObject list) {
public static Boolean isWhitelistedAdStories(JSONObject list) {
var id = String.valueOf(list.optInt("owner_id"));

var whitelist = Preferences.getPreferences().getStringSet(
var whitelist = getPreferences().getStringSet(
"whitelisted_stories_ad",
Collections.emptySet()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -86,15 +86,9 @@ public static Set<String> downloadFilter(String link) {
}

public static Set<String> stringToSet(String input) {
String[] words = input.split("\n");

Set<String> set = new HashSet<>();

for (String word : words) {
set.add(word.toLowerCase());
}

return set;
return Arrays.stream(input.split("\n"))
.map(String::toLowerCase)
.collect(Collectors.toSet());
}

private static List<Filter> parseJSON(JSONArray jsonArray) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public static List<Long> getOwnersIds() {
}
}


private static List<Long> parseJSONIds(JSONArray jsonArray) throws JSONException {
List<Long> ids = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.SharedPreferences;
import ru.vtosters.lite.concurrent.VTExecutors;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.newsfeed.NewsFeedFiltersUtils;
import ru.vtosters.sponsorpost.data.Filter;
import ru.vtosters.sponsorpost.services.FilterService;

Expand Down Expand Up @@ -47,14 +48,18 @@ public static void saveFilter(Filter filter) {
editor.putString(getPrefKey(filter.getId(), PREF_KEY_LINK), filter.getLink());
editor.apply();

VTExecutors.getSlowTasksScheduler().a(() -> downloadFilter(filter));
VTExecutors.getSlowTasksScheduler().a(() -> {
downloadFilter(filter);
NewsFeedFiltersUtils.updateFilters();
});
}

public static void downloadFilter(Filter filter) {
Set<String> list = FilterService.downloadFilter(filter.getLink());
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(getPrefKey(filter.getId(), PREF_KEY_LIST), list);
editor.apply();

preferences.edit()
.putStringSet("filter:" + filter.getId() + ":" + PREF_KEY_LIST, list)
.apply();
}

public static Set<String> getFiltersLists() {
Expand Down Expand Up @@ -89,13 +94,14 @@ private static String getPrefKey(int id, String key) {

public static void deleteFilter(int id) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove(getPrefKey(id, PREF_KEY_ID));
editor.remove(getPrefKey(id, PREF_KEY_TITLE));
editor.remove(getPrefKey(id, PREF_KEY_SUMMARY));
editor.remove(getPrefKey(id, PREF_KEY_VERSION));
editor.remove(getPrefKey(id, PREF_KEY_LINK));
editor.remove(getPrefKey(id, PREF_KEY_LIST));

preferences.getAll().keySet().stream()
.filter(key -> key.startsWith("filter:" + id + ":"))
.forEach(editor::remove);

editor.apply();

NewsFeedFiltersUtils.updateFilters();
}

public static List<Integer> getAllFilterIds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class PostsPreferences {
private static final SharedPreferences preferences;
Expand All @@ -29,19 +30,22 @@ public static void setEnabled(boolean status) {
}

public static void savePosts(List<Post> posts) {
SharedPreferences.Editor editor = preferences.edit();
Set<String> stringSet = new HashSet<>();
Set<String> stringSet = posts.stream()
.map(post -> post.getOwnerId() + "_" + post.getPostId())
.collect(Collectors.toSet());

for (Post post : posts) {
stringSet.add(post.getOwnerId() + "_" + post.getPostId());
}

editor.putStringSet("posts", stringSet);

editor.apply();
preferences.edit()
.putStringSet("posts", stringSet)
.apply();
}

public static boolean isPostAd(long ownerId, long postId) {
return isEnabled() && preferences.getStringSet("posts", null).contains(ownerId + "_" + postId);
Set<String> hasPost = preferences.getStringSet("posts", new HashSet<>());

if (!hasPost.isEmpty() && isEnabled()) {
return hasPost.contains(ownerId + "_" + postId);
} else {
return false;
}
}
}
}
17 changes: 8 additions & 9 deletions app/src/main/java/ru/vtosters/sponsorpost/utils/Updates.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import ru.vtosters.sponsorpost.services.FilterService;
import ru.vtosters.sponsorpost.services.PostService;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class Updates {
public static void updateFilters() {
Expand Down Expand Up @@ -52,16 +52,15 @@ private static Filter findLocalFilterById(List<Filter> localFilters, int id) {
}

public static void updatePosts() {
long neededPosts = LocalDate.now().minusWeeks(1).toEpochDay();
long currentTime = System.currentTimeMillis();
long weekAgo = currentTime - TimeUnit.DAYS.toMillis(7);

if (!PostsPreferences.isEnabled() || Preferences.serverFeaturesDisable() || !NetworkUtils.isNetworkConnected()) {
return;
}
if (PostsPreferences.isEnabled() && !Preferences.serverFeaturesDisable() && NetworkUtils.isNetworkConnected()) {
List<Post> posts = PostService.getPosts(weekAgo);

List<Post> posts = PostService.getPosts(neededPosts);

if (!posts.isEmpty()) {
PostsPreferences.savePosts(posts);
if (!posts.isEmpty()) {
PostsPreferences.savePosts(posts);
}
}
}
}

0 comments on commit feef98a

Please sign in to comment.