Skip to content

Commit

Permalink
ux: bug fix: crash on search with parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Dec 8, 2022
1 parent 6a0980a commit 278b055
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/backend/plugins/searchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,31 @@ export = {
init: function init(ssb: any) {
const containsWords = ssb.search2.operator;

function clean(text: string) {
return text.replace(/[()\[\]]/g, '').trim();
}

return {
query(text: string) {
// We want to make sure that the *exact* input is matched, *not* as a
// word prefix, so we use a word boundary, except not literally `\b`
// because it often doensn't work with Unicode (especially in
// nodejs-mobile!), so we do this instead:
const regex = new RegExp(clean(text) + '($|[ ,.;:!?\\-])', 'i');

return pull(
ssb.db.query(
where(and(type('post'), isPublic(), containsWords(text))),
where(and(type('post'), isPublic(), containsWords(clean(text)))),
descending(),
batch(20),
toPullStream(),
),
pull.filter((msg: Msg<PostContent>) =>
// We want to make sure that *exact* input is matched, *not* as a
// word prefix, so we use a word boundary, except not literally `\b`
// because it often doensn't work with Unicode (especially in
// nodejs-mobile!), so we do this instead:
new RegExp(text + '($|[ ,.;:!?\\-])', 'i').test(
msg.value.content.text,
),
),
pull.filter((msg: Msg<PostContent>) => {
if (!msg.value.content.text) return false;
if (typeof msg.value.content.text !== 'string') return false;
regex.lastIndex = 0;
return regex.test(msg.value.content.text);
}),
pull.asyncMap((msg: Msg, cb: Callback<Msg | null>) => {
ssb.friends.isBlocking(
{source: ssb.id, dest: msg.value.author},
Expand Down
11 changes: 8 additions & 3 deletions src/frontend/screens/search/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ const ELLIPSIS = String.fromCodePoint(parseInt('2026', 16));
const MIN_INDEX = 50;
const MAX_LENGTH = 150;

function cleanQuery(query: string) {
return query.replace(/[()\[\]]/g, '').trim();
}

function regExpWithBoundary(query: string) {
// Word boundary `\b` often doensn't work with Unicode, so we do this:
return new RegExp(query + '($|[ ,.;:!?\\-])', 'i');
return new RegExp(cleanQuery(query) + '($|[ ,.;:!?\\-])', 'i');
}

/** Determine result content and queryMatchIdx
Expand Down Expand Up @@ -105,9 +109,10 @@ class Result extends PureComponent<{

const [content, queryMatchIdx] = getContentAndQueryMatchIdx(msg, query);
if (queryMatchIdx < 0) return null; // dont render potential bad `msg`
const len = cleanQuery(query).length;
const preContent = content.slice(0, queryMatchIdx);
const match = content.slice(queryMatchIdx, queryMatchIdx + query.length);
const postContent = content.slice(queryMatchIdx + query.length);
const match = content.slice(queryMatchIdx, queryMatchIdx + len);
const postContent = content.slice(queryMatchIdx + len);

return h(View, [
h(Touchable, touchableProps, [
Expand Down

0 comments on commit 278b055

Please sign in to comment.