Skip to content

Commit

Permalink
squash! PATCH: Minimum Text Search is for 3 chars
Browse files Browse the repository at this point in the history
* Escape dots in usernames when generating RegExps
* Ignore `from:Username` searches when username is < 3 chars.
  Similarly, ignore partially-typed searches, such as "fro" and "from"

* Consolidate all these checks into one place
  • Loading branch information
nmagedman committed Nov 14, 2024
1 parent 0684b60 commit 900b183
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apps/meteor/server/lib/parseMessageSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class MessageSearchQueryParser {
from.push(username);

// Search for case-sensitive prefix match (no substrings)
this.query['u.username'] = { $in: from.map((prefix) => RegExp(`^${prefix}`)) };
this.query['u.username'] = {
$in: from.map((prefix) => RegExp(`^${prefix.replace(/\./g, '\\.')}`)),
};

return '';
});
Expand Down
15 changes: 14 additions & 1 deletion apps/meteor/server/methods/messageSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ Meteor.methods<ServerMethods>({
};
}

// Fail-fast on search terms that are still being typed in
if (
text.length < 3 // Too short to be meaningful. But support TLAs.

Check failure on line 49 in apps/meteor/server/methods/messageSearch.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Replace `··························` with `||`
|| text.match(/^from?$/i) // `from:Username` in-progress.

Check failure on line 50 in apps/meteor/server/methods/messageSearch.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Replace `||·text.match(/^from?$/i)·················` with `text.match(/^from?$/i)·||`
|| text.match(/^from:[a-z0-9.\-_]{0,2}$/i) // Minimum 3 character usernames, lest we get too many hits
) {
return {
message: {
docs: [],
},
};
}

const user = (await Meteor.userAsync()) as IUser | undefined;

const { query, options } = parseMessageSearchQuery(text, {
Expand All @@ -53,7 +66,7 @@ Meteor.methods<ServerMethods>({
forceRegex: settings.get('Message_AlwaysSearchRegExp'),
});

if (text.length < 3 || Object.keys(query).length === 0) {
if (Object.keys(query).length === 0) {
return {
message: {
docs: [],
Expand Down

0 comments on commit 900b183

Please sign in to comment.