Fix filters

This commit is contained in:
Thomas 2022-11-07 10:42:09 +01:00
parent 2625101883
commit ee68f4fc7d
2 changed files with 23 additions and 33 deletions

View File

@ -60,17 +60,6 @@ public class TimelineHelper {
return retrofit.create(MastodonAccountsService.class); return retrofit.create(MastodonAccountsService.class);
} }
/**
* Allows to filter statuses, should be called in API calls (background)
*
* @param context - Context
* @param statuses - List of {@link Status}
* @param filterTimeLineType - {@link Timeline.TimeLineEnum}
* @return filtered List<Status>
*/
public static List<Status> filterStatus(Context context, List<Status> statuses, Timeline.TimeLineEnum filterTimeLineType) {
return filterStatus(context, statuses, filterTimeLineType, false);
}
/** /**
* Allows to filter statuses, should be called in API calls (background) * Allows to filter statuses, should be called in API calls (background)
@ -80,7 +69,7 @@ public class TimelineHelper {
* @param filterTimeLineType - {@link Timeline.TimeLineEnum} * @param filterTimeLineType - {@link Timeline.TimeLineEnum}
* @return filtered List<Status> * @return filtered List<Status>
*/ */
public static List<Status> filterStatus(Context context, List<Status> statuses, Timeline.TimeLineEnum filterTimeLineType, boolean cached) { public static List<Status> filterStatus(Context context, List<Status> statuses, Timeline.TimeLineEnum filterTimeLineType) {
//A security to make sure filters have been fetched before displaying messages //A security to make sure filters have been fetched before displaying messages
List<Status> statusesToRemove = new ArrayList<>(); List<Status> statusesToRemove = new ArrayList<>();
if (!BaseMainActivity.filterFetched) { if (!BaseMainActivity.filterFetched) {
@ -102,9 +91,6 @@ public class TimelineHelper {
} }
//If there are filters: //If there are filters:
if (BaseMainActivity.mainFilters != null && BaseMainActivity.mainFilters.size() > 0 && statuses != null && statuses.size() > 0) { if (BaseMainActivity.mainFilters != null && BaseMainActivity.mainFilters.size() > 0 && statuses != null && statuses.size() > 0) {
for (Status status : statuses) {
status.cached = cached;
}
for (Filter filter : BaseMainActivity.mainFilters) { for (Filter filter : BaseMainActivity.mainFilters) {
if (filter.irreversible) { //Dealt by the server if (filter.irreversible) { //Dealt by the server
continue; continue;
@ -200,7 +186,7 @@ public class TimelineHelper {
for (String filterContext : filter.context) { for (String filterContext : filter.context) {
if (Timeline.TimeLineEnum.NOTIFICATION.getValue().equalsIgnoreCase(filterContext)) { if (Timeline.TimeLineEnum.NOTIFICATION.getValue().equalsIgnoreCase(filterContext)) {
if (filter.whole_word) { if (filter.whole_word) {
Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)"); Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)", Pattern.CASE_INSENSITIVE);
for (Notification notification : notifications) { for (Notification notification : notifications) {
notification.cached = cached; notification.cached = cached;
if (notification.status != null) { if (notification.status != null) {

View File

@ -413,23 +413,27 @@ public class TimelinesVM extends AndroidViewModel {
List<Status> statusList = timelineResponse.body(); List<Status> statusList = timelineResponse.body();
statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusList, timelineParams.type); statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusList, timelineParams.type);
statuses.pagination = MastodonHelper.getPagination(timelineResponse.headers()); statuses.pagination = MastodonHelper.getPagination(timelineResponse.headers());
if (statusList != null && statusList.size() > 0) { if (statuses.statuses != null && statuses.statuses.size() > 0) {
addFetchMore(statusList, timelineStatuses, timelineParams); //Fetch More is added on filtered statuses
for (Status status : statuses.statuses) { addFetchMore(statuses.statuses, timelineStatuses, timelineParams);
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext()); //All statuses (even filtered will be added to cache)
StatusCache statusCache = new StatusCache(); if (statusList != null && statusList.size() > 0) {
statusCache.instance = timelineParams.instance; for (Status status : statusList) {
statusCache.user_id = timelineParams.userId; StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
statusCache.status = status; StatusCache statusCache = new StatusCache();
statusCache.type = timelineParams.type; statusCache.instance = timelineParams.instance;
statusCache.status_id = status.id; statusCache.user_id = timelineParams.userId;
try { statusCache.status = status;
int inserted = statusCacheDAO.insertOrUpdate(statusCache, timelineParams.slug); statusCache.type = timelineParams.type;
if (inserted == 0) { statusCache.status_id = status.id;
status.cached = true; try {
int inserted = statusCacheDAO.insertOrUpdate(statusCache, timelineParams.slug);
if (inserted == 0) {
status.cached = true;
}
} catch (DBException e) {
e.printStackTrace();
} }
} catch (DBException e) {
e.printStackTrace();
} }
} }
} }
@ -465,7 +469,7 @@ public class TimelinesVM extends AndroidViewModel {
//Only not already present statuses are added //Only not already present statuses are added
statusesDb.statuses = notPresentStatuses; statusesDb.statuses = notPresentStatuses;
} }
statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusesDb.statuses, timelineParams.type, true); statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusesDb.statuses, timelineParams.type);
if (statuses.statuses != null && statuses.statuses.size() > 0) { if (statuses.statuses != null && statuses.statuses.size() > 0) {
addFetchMore(statuses.statuses, timelineStatuses, timelineParams); addFetchMore(statuses.statuses, timelineStatuses, timelineParams);
statuses.pagination = new Pagination(); statuses.pagination = new Pagination();