Comment issue #412 - Filters not applied

This commit is contained in:
Thomas 2022-11-07 10:01:10 +01:00
parent 730468ee72
commit c640505fe6
2 changed files with 17 additions and 19 deletions

View file

@ -100,7 +100,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) { for (Status status : statuses) {
@ -113,13 +112,13 @@ public class TimelineHelper {
for (String filterContext : filter.context) { for (String filterContext : filter.context) {
if (filterTimeLineType.getValue().equalsIgnoreCase(filterContext)) { if (filterTimeLineType.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 (Status status : statuses) { for (Status status : statuses) {
String content; String content;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString();
else else
content = Html.fromHtml(status.content).toString(); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content).toString();
Matcher m = p.matcher(content); Matcher m = p.matcher(content);
if (m.find()) { if (m.find()) {
statusesToRemove.add(status); statusesToRemove.add(status);
@ -128,9 +127,9 @@ public class TimelineHelper {
if (status.spoiler_text != null) { if (status.spoiler_text != null) {
String spoilerText; String spoilerText;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spoilerText = Html.fromHtml(status.spoiler_text, Html.FROM_HTML_MODE_LEGACY).toString(); spoilerText = Html.fromHtml(status.reblog != null ? status.reblog.spoiler_text : status.spoiler_text, Html.FROM_HTML_MODE_LEGACY).toString();
else else
spoilerText = Html.fromHtml(status.spoiler_text).toString(); spoilerText = Html.fromHtml(status.reblog != null ? status.reblog.spoiler_text : status.spoiler_text).toString();
Matcher ms = p.matcher(spoilerText); Matcher ms = p.matcher(spoilerText);
if (ms.find()) { if (ms.find()) {
statusesToRemove.add(status); statusesToRemove.add(status);
@ -141,9 +140,9 @@ public class TimelineHelper {
for (Status status : statuses) { for (Status status : statuses) {
String content; String content;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString();
else else
content = Html.fromHtml(status.content).toString(); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content).toString();
if (content.contains(filter.phrase)) { if (content.contains(filter.phrase)) {
statusesToRemove.add(status); statusesToRemove.add(status);
continue; continue;
@ -152,9 +151,9 @@ public class TimelineHelper {
if (status.spoiler_text != null) { if (status.spoiler_text != null) {
String spoilerText; String spoilerText;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spoilerText = Html.fromHtml(status.spoiler_text, Html.FROM_HTML_MODE_LEGACY).toString(); spoilerText = Html.fromHtml(status.reblog != null ? status.reblog.spoiler_text : status.spoiler_text, Html.FROM_HTML_MODE_LEGACY).toString();
else else
spoilerText = Html.fromHtml(status.spoiler_text).toString(); spoilerText = Html.fromHtml(status.reblog != null ? status.reblog.spoiler_text : status.spoiler_text).toString();
if (spoilerText.contains(filter.phrase)) { if (spoilerText.contains(filter.phrase)) {
statusesToRemove.add(status); statusesToRemove.add(status);
} }

View file

@ -414,7 +414,6 @@ public class TimelinesVM extends AndroidViewModel {
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 (statusList != null && statusList.size() > 0) {
addFetchMore(statusList, timelineStatuses, timelineParams); addFetchMore(statusList, timelineStatuses, timelineParams);
for (Status status : statuses.statuses) { for (Status status : statuses.statuses) {
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext()); StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
@ -434,6 +433,7 @@ public class TimelinesVM extends AndroidViewModel {
} }
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -450,22 +450,22 @@ public class TimelinesVM extends AndroidViewModel {
statusesMutableLiveData = new MutableLiveData<>(); statusesMutableLiveData = new MutableLiveData<>();
new Thread(() -> { new Thread(() -> {
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext()); StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
Statuses statuses = null; Statuses statuses = new Statuses();
try { try {
statuses = statusCacheDAO.geStatuses(timelineParams.slug, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId); Statuses statusesDb = statusCacheDAO.geStatuses(timelineParams.slug, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
if (statuses != null && statuses.statuses != null && statuses.statuses.size() > 0) { if (statusesDb != null && statusesDb.statuses != null && statusesDb.statuses.size() > 0) {
if (timelineStatuses != null) { if (timelineStatuses != null) {
List<Status> notPresentStatuses = new ArrayList<>(); List<Status> notPresentStatuses = new ArrayList<>();
for (Status status : statuses.statuses) { for (Status status : statusesDb.statuses) {
if (!timelineStatuses.contains(status)) { if (!timelineStatuses.contains(status)) {
status.cached = true; status.cached = true;
notPresentStatuses.add(status); notPresentStatuses.add(status);
} }
} }
//Only not already present statuses are added //Only not already present statuses are added
statuses.statuses = notPresentStatuses; statusesDb.statuses = notPresentStatuses;
} }
TimelineHelper.filterStatus(getApplication().getApplicationContext(), statuses.statuses, timelineParams.type, true); statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusesDb.statuses, timelineParams.type, true);
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();
@ -477,8 +477,7 @@ public class TimelinesVM extends AndroidViewModel {
e.printStackTrace(); e.printStackTrace();
} }
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
Statuses finalStatuses = statuses; Runnable myRunnable = () -> statusesMutableLiveData.setValue(statuses);
Runnable myRunnable = () -> statusesMutableLiveData.setValue(finalStatuses);
mainHandler.post(myRunnable); mainHandler.post(myRunnable);
}).start(); }).start();
return statusesMutableLiveData; return statusesMutableLiveData;