Fix issue #442 - Tags not filtered

This commit is contained in:
Thomas 2022-11-16 15:49:20 +01:00
parent 1dae37549b
commit 2fa46290a1
4 changed files with 83 additions and 49 deletions

View File

@ -120,6 +120,9 @@ public class FilterActivity extends BaseActivity implements FilterAdapter.Delete
case "thread": case "thread":
popupAddFilterBinding.contextConversation.setChecked(true); popupAddFilterBinding.contextConversation.setChecked(true);
break; break;
case "account":
popupAddFilterBinding.contextProfiles.setChecked(true);
break;
} }
} }
popupAddFilterBinding.contextWholeWord.setChecked(filter.whole_word); popupAddFilterBinding.contextWholeWord.setChecked(filter.whole_word);
@ -137,7 +140,7 @@ public class FilterActivity extends BaseActivity implements FilterAdapter.Delete
popupAddFilterBinding.addPhrase.setError(context.getString(R.string.cannot_be_empty)); popupAddFilterBinding.addPhrase.setError(context.getString(R.string.cannot_be_empty));
return; return;
} }
if (!popupAddFilterBinding.contextConversation.isChecked() && !popupAddFilterBinding.contextHome.isChecked() && !popupAddFilterBinding.contextPublic.isChecked() && !popupAddFilterBinding.contextNotification.isChecked()) { if (!popupAddFilterBinding.contextConversation.isChecked() && !popupAddFilterBinding.contextHome.isChecked() && !popupAddFilterBinding.contextPublic.isChecked() && !popupAddFilterBinding.contextNotification.isChecked() && !popupAddFilterBinding.contextProfiles.isChecked()) {
popupAddFilterBinding.contextDescription.setError(context.getString(R.string.cannot_be_empty)); popupAddFilterBinding.contextDescription.setError(context.getString(R.string.cannot_be_empty));
return; return;
} }
@ -152,6 +155,8 @@ public class FilterActivity extends BaseActivity implements FilterAdapter.Delete
contextFilter.add("notifications"); contextFilter.add("notifications");
if (popupAddFilterBinding.contextConversation.isChecked()) if (popupAddFilterBinding.contextConversation.isChecked())
contextFilter.add("thread"); contextFilter.add("thread");
if (popupAddFilterBinding.contextProfiles.isChecked())
contextFilter.add("account");
filterSent.context = contextFilter; filterSent.context = contextFilter;
filterSent.expires_at_sent = expire[0]; filterSent.expires_at_sent = expire[0];
filterSent.phrase = popupAddFilterBinding.addPhrase.getText().toString(); filterSent.phrase = popupAddFilterBinding.addPhrase.getText().toString();

View File

@ -26,6 +26,7 @@ import androidx.lifecycle.ViewModelStoreOwner;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -91,63 +92,76 @@ 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) {
//Loop through filters
for (Filter filter : BaseMainActivity.mainFilters) { for (Filter filter : BaseMainActivity.mainFilters) {
if (filter.expires_at != null && filter.expires_at.before(new Date())) {
//Expired filter
continue;
}
for (String filterContext : filter.context) { if (filterTimeLineType == Timeline.TimeLineEnum.HOME) {
if (filterTimeLineType.getValue().equalsIgnoreCase(filterContext)) { if (!filter.context.contains("home")) continue;
if (filter.whole_word) { } else if (filterTimeLineType == Timeline.TimeLineEnum.NOTIFICATION) {
if (!filter.context.contains("notification")) continue;
} else if (filterTimeLineType == Timeline.TimeLineEnum.CONTEXT) {
if (!filter.context.contains("thread")) continue;
} else if (filterTimeLineType == Timeline.TimeLineEnum.ACCOUNT_TIMELINE) {
if (!filter.context.contains("account")) continue;
} else {
if (!filter.context.contains("public")) continue;
}
Pattern p = Pattern.compile("\\b(" + Pattern.quote(filter.phrase) + ")\\b", Pattern.CASE_INSENSITIVE); if (filter.whole_word) {
for (Status status : statuses) { Pattern p = Pattern.compile("(^|\\W)(" + Pattern.quote(filter.phrase) + ")($|\\W)", Pattern.CASE_INSENSITIVE);
String content; for (Status status : statuses) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) String content;
content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
else content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString();
content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content).toString(); else
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);
continue; continue;
} }
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.reblog != null ? status.reblog.spoiler_text : 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.reblog != null ? status.reblog.spoiler_text : 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);
}
}
} }
} else { }
for (Status status : statuses) { }
String content; } else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) for (Status status : statuses) {
content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString(); String content;
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content).toString(); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content, Html.FROM_HTML_MODE_LEGACY).toString();
if (content.contains(filter.phrase)) { else
statusesToRemove.add(status); content = Html.fromHtml(status.reblog != null ? status.reblog.content : status.content).toString();
continue; if (content.contains(filter.phrase)) {
} statusesToRemove.add(status);
continue;
}
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.reblog != null ? status.reblog.spoiler_text : 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.reblog != null ? status.reblog.spoiler_text : 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);
}
}
} }
} }
} }
} }
} }
} }
if (statuses != null) { if (statuses != null) {

View File

@ -109,6 +109,20 @@
android:text="@string/context_conversation" android:text="@string/context_conversation"
app:buttonTint="@color/cyanea_accent_dark_reference" /> app:buttonTint="@color/cyanea_accent_dark_reference" />
</androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/context_profiles"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/profiles"
app:buttonTint="@color/cyanea_accent_dark_reference" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.checkbox.MaterialCheckBox <com.google.android.material.checkbox.MaterialCheckBox

View File

@ -1984,4 +1984,5 @@
<string name="followed_tags">Followed tags</string> <string name="followed_tags">Followed tags</string>
<string name="follow_tag">Follow tag</string> <string name="follow_tag">Follow tag</string>
<string name="action_lists_edit">Edit list</string> <string name="action_lists_edit">Edit list</string>
<string name="profiles">Profiles</string>
</resources> </resources>