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 b791e62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 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
9 changes: 7 additions & 2 deletions apps/meteor/server/methods/messageSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ Meteor.methods<ServerMethods>({
if (!(await canAccessRoomIdAsync(rid, currentUserId))) {
return false;
}
} else if (settings.get('Search.defaultProvider.GlobalSearchEnabled') !== true) {
} else if (
settings.get('Search.defaultProvider.GlobalSearchEnabled') !== true ||
text.length < 3 ||
text.match(/^from?$/i) ||
text.match(/^from:[a-z0-9.\-_]{0,2}$/i)
) {
return {
message: {
docs: [],
Expand All @@ -53,7 +58,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 b791e62

Please sign in to comment.