mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-02-27 17:23:57 +01:00
Handle invalid regex pattern in post filter.
This commit is contained in:
parent
15b979d3ba
commit
785bb205f6
@ -35,6 +35,8 @@ import com.r0adkll.slidr.Slidr;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
@ -540,14 +542,19 @@ public class CustomizePostFilterActivity extends BaseActivity {
|
||||
finish();
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.action_save_customize_post_filter_activity) {
|
||||
try {
|
||||
constructPostFilter();
|
||||
Intent returnIntent = new Intent();
|
||||
returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter);
|
||||
setResult(Activity.RESULT_OK, returnIntent);
|
||||
finish();
|
||||
} catch (PatternSyntaxException e) {
|
||||
Toast.makeText(this, R.string.invalid_regex, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.action_save_to_database_customize_post_filter_activity) {
|
||||
try {
|
||||
constructPostFilter();
|
||||
|
||||
if (!postFilter.name.equals("")) {
|
||||
@ -555,6 +562,9 @@ public class CustomizePostFilterActivity extends BaseActivity {
|
||||
} else {
|
||||
Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} catch (PatternSyntaxException e) {
|
||||
Toast.makeText(this, R.string.invalid_regex, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -627,7 +637,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void constructPostFilter() {
|
||||
private void constructPostFilter() throws PatternSyntaxException {
|
||||
postFilter.name = nameTextInputEditText.getText().toString();
|
||||
postFilter.maxVote = maxVoteTextInputEditText.getText() == null || maxVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxVoteTextInputEditText.getText().toString());
|
||||
postFilter.minVote = minVoteTextInputEditText.getText() == null || minVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(minVoteTextInputEditText.getText().toString());
|
||||
@ -636,7 +646,9 @@ public class CustomizePostFilterActivity extends BaseActivity {
|
||||
postFilter.maxAwards = maxAwardsTextInputEditText.getText() == null || maxAwardsTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxAwardsTextInputEditText.getText().toString());
|
||||
postFilter.minAwards = minAwardsTextInputEditText.getText() == null || minAwardsTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(minAwardsTextInputEditText.getText().toString());
|
||||
postFilter.postTitleExcludesRegex = titleExcludesRegexTextInputEditText.getText().toString();
|
||||
Pattern.compile(postFilter.postTitleExcludesRegex);
|
||||
postFilter.postTitleContainsRegex = titleContainsRegexTextInputEditText.getText().toString();
|
||||
Pattern.compile(postFilter.postTitleContainsRegex);
|
||||
postFilter.postTitleExcludesStrings = titleExcludesStringsTextInputEditText.getText().toString();
|
||||
postFilter.postTitleContainsStrings = titleContainsStringsTextInputEditText.getText().toString();
|
||||
postFilter.excludeSubreddits = excludesSubredditsTextInputEditText.getText().toString();
|
||||
|
@ -12,6 +12,7 @@ import androidx.room.PrimaryKey;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.post.Post;
|
||||
|
||||
@ -173,18 +174,24 @@ public class PostFilter implements Parcelable {
|
||||
return false;
|
||||
}
|
||||
if (postFilter.postTitleExcludesRegex != null && !postFilter.postTitleExcludesRegex.equals("")) {
|
||||
try {
|
||||
Pattern pattern = Pattern.compile(postFilter.postTitleExcludesRegex);
|
||||
Matcher matcher = pattern.matcher(post.getTitle());
|
||||
if (matcher.find()) {
|
||||
return false;
|
||||
}
|
||||
} catch (PatternSyntaxException ignore) {}
|
||||
}
|
||||
if (postFilter.postTitleContainsRegex != null && !postFilter.postTitleContainsRegex.equals("")) {
|
||||
try {
|
||||
Pattern pattern = Pattern.compile(postFilter.postTitleContainsRegex);
|
||||
Matcher matcher = pattern.matcher(post.getTitle());
|
||||
if (!matcher.find()) {
|
||||
return false;
|
||||
}
|
||||
} catch (PatternSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (postFilter.postTitleExcludesStrings != null && !postFilter.postTitleExcludesStrings.equals("")) {
|
||||
String[] titles = postFilter.postTitleExcludesStrings.split(",", 0);
|
||||
|
@ -1308,4 +1308,6 @@
|
||||
<string name="exo_controls_volume_up_description">Volume Up</string>
|
||||
<string name="exo_controls_volume_off_description">Volume Off</string>
|
||||
|
||||
<string name="invalid_regex">Invalid regex pattern</string>
|
||||
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user