mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #1193 from deltachat/fix934AllowingSpecialCharsInSearchFunction
#934 removed filter for special chars. Core seems to be handling this…
This commit is contained in:
@@ -22,24 +22,6 @@ import java.util.concurrent.Executor;
|
||||
*/
|
||||
class SearchRepository {
|
||||
|
||||
private static final Set<Character> BANNED_CHARACTERS = new HashSet<>();
|
||||
static {
|
||||
// Several ranges of invalid ASCII characters
|
||||
for (int i = 33; i <= 47; i++) {
|
||||
BANNED_CHARACTERS.add((char) i);
|
||||
}
|
||||
for (int i = 58; i <= 64; i++) {
|
||||
BANNED_CHARACTERS.add((char) i);
|
||||
}
|
||||
for (int i = 91; i <= 96; i++) {
|
||||
BANNED_CHARACTERS.add((char) i);
|
||||
}
|
||||
for (int i = 123; i <= 126; i++) {
|
||||
BANNED_CHARACTERS.add((char) i);
|
||||
}
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private final ApplicationDcContext dcContext;
|
||||
private final Executor executor;
|
||||
private boolean queryMessages = true;
|
||||
@@ -47,8 +29,7 @@ class SearchRepository {
|
||||
SearchRepository(@NonNull Context context,
|
||||
@NonNull Executor executor)
|
||||
{
|
||||
this.context = context.getApplicationContext();
|
||||
this.dcContext = DcHelper.getContext(context);
|
||||
this.dcContext = DcHelper.getContext(context.getApplicationContext());
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
@@ -59,16 +40,14 @@ class SearchRepository {
|
||||
}
|
||||
|
||||
executor.execute(() -> {
|
||||
String cleanQuery = sanitizeQuery(query);
|
||||
|
||||
int[] contacts = dcContext.getContacts(DcContext.DC_GCL_ADD_SELF, cleanQuery);
|
||||
DcChatlist conversations = dcContext.getChatlist(0, cleanQuery, 0);
|
||||
int[] contacts = dcContext.getContacts(DcContext.DC_GCL_ADD_SELF, query);
|
||||
DcChatlist conversations = dcContext.getChatlist(0, query, 0);
|
||||
int[] messages = new int[0];
|
||||
if (queryMessages) {
|
||||
messages = dcContext.searchMsgs(0, cleanQuery);
|
||||
messages = dcContext.searchMsgs(0, query);
|
||||
}
|
||||
|
||||
callback.onResult(new SearchResult(cleanQuery, contacts, conversations, messages));
|
||||
callback.onResult(new SearchResult(query, contacts, conversations, messages));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -76,28 +55,6 @@ class SearchRepository {
|
||||
this.queryMessages = includeMessageQueries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unfortunately {@link DatabaseUtils#sqlEscapeString(String)} is not sufficient for our purposes.
|
||||
* MATCH queries have a separate format of their own that disallow most "special" characters.
|
||||
*
|
||||
* Also, SQLite can't search for apostrophes, meaning we can't normally find words like "I'm".
|
||||
* However, if we replace the apostrophe with a space, then the query will find the match.
|
||||
*/
|
||||
private String sanitizeQuery(@NonNull String query) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < query.length(); i++) {
|
||||
char c = query.charAt(i);
|
||||
if (!BANNED_CHARACTERS.contains(c)) {
|
||||
out.append(c);
|
||||
} else if (c == '\'') {
|
||||
out.append(' ');
|
||||
}
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
void onResult(@NonNull SearchResult result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user