Skip to content

Commit

Permalink
FEATURE: Make search filters case insensitive (#10715)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbianca committed Sep 23, 2020
1 parent 18f0cef commit 4abbe3d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 64 deletions.
Expand Up @@ -114,7 +114,7 @@ export default Component.extend({
this.setSearchedTermValueForTags();

let regExpInMatch = this.inOptions.map((option) => option.value).join("|");
const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`);
const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`, "i");

this.setSearchedTermValue(
"searchedTerms.in",
Expand Down Expand Up @@ -145,7 +145,10 @@ export default Component.extend({
let regExpStatusMatch = this.statusOptions
.map((status) => status.value)
.join("|");
const REGEXP_STATUS_MATCH = new RegExp(`status:(${regExpStatusMatch})`);
const REGEXP_STATUS_MATCH = new RegExp(
`status:(${regExpStatusMatch})`,
"i"
);

this.setSearchedTermValue(
"searchedTerms.status",
Expand Down Expand Up @@ -203,7 +206,7 @@ export default Component.extend({

let val = this.get(key);
if (match.length !== 0) {
const userInput = match[0].replace(replaceRegEx, "");
const userInput = match[0].replace(replaceRegEx, "").toLowerCase();

if (val !== userInput && userInput.length) {
this.set(key, userInput);
Expand Down Expand Up @@ -298,7 +301,9 @@ export default Component.extend({

if (match.length !== 0) {
const existingInputWhen = this.get("searchedTerms.time.when");
const userInputWhen = match[0].match(REGEXP_POST_TIME_WHEN)[0];
const userInputWhen = match[0]
.match(REGEXP_POST_TIME_WHEN)[0]
.toLowerCase();
const existingInputDays = this.get("searchedTerms.time.days");
const userInputDays = match[0].replace(REGEXP_POST_TIME_PREFIX, "");
const properties = {};
Expand Down Expand Up @@ -549,7 +554,7 @@ export default Component.extend({

_updateSearchTermForIn() {
let regExpInMatch = this.inOptions.map((option) => option.value).join("|");
const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`);
const REGEXP_IN_MATCH = new RegExp(`(in|with):(${regExpInMatch})`, "i");

const match = this.filterBlocks(REGEXP_IN_MATCH);
const inFilter = this.get("searchedTerms.in");
Expand Down Expand Up @@ -577,7 +582,10 @@ export default Component.extend({
let regExpStatusMatch = this.statusOptions
.map((status) => status.value)
.join("|");
const REGEXP_STATUS_MATCH = new RegExp(`status:(${regExpStatusMatch})`);
const REGEXP_STATUS_MATCH = new RegExp(
`status:(${regExpStatusMatch})`,
"i"
);

const match = this.filterBlocks(REGEXP_STATUS_MATCH);
const statusFilter = this.get("searchedTerms.status");
Expand Down
86 changes: 43 additions & 43 deletions lib/search.rb
Expand Up @@ -305,7 +305,7 @@ def self.custom_topic_eager_loads
Array.wrap(@custom_topic_eager_loads)
end

advanced_filter(/^in:personal-direct$/) do |posts|
advanced_filter(/^in:personal-direct$/i) do |posts|
if @guardian.user
posts
.joins("LEFT JOIN topic_allowed_groups tg ON posts.topic_id = tg.topic_id")
Expand All @@ -325,67 +325,67 @@ def self.custom_topic_eager_loads
end
end

advanced_filter(/^in:tagged$/) do |posts|
advanced_filter(/^in:tagged$/i) do |posts|
posts
.where('EXISTS (SELECT 1 FROM topic_tags WHERE topic_tags.topic_id = posts.topic_id)')
end

advanced_filter(/^in:untagged$/) do |posts|
advanced_filter(/^in:untagged$/i) do |posts|
posts
.joins("LEFT JOIN topic_tags ON
topic_tags.topic_id = posts.topic_id")
.where("topic_tags.id IS NULL")
end

advanced_filter(/^status:open$/) do |posts|
advanced_filter(/^status:open$/i) do |posts|
posts.where('NOT topics.closed AND NOT topics.archived')
end

advanced_filter(/^status:closed$/) do |posts|
advanced_filter(/^status:closed$/i) do |posts|
posts.where('topics.closed')
end

advanced_filter(/^status:public$/) do |posts|
advanced_filter(/^status:public$/i) do |posts|
category_ids = Category
.where(read_restricted: false)
.pluck(:id)

posts.where("topics.category_id in (?)", category_ids)
end

advanced_filter(/^status:archived$/) do |posts|
advanced_filter(/^status:archived$/i) do |posts|
posts.where('topics.archived')
end

advanced_filter(/^status:noreplies$/) do |posts|
advanced_filter(/^status:noreplies$/i) do |posts|
posts.where("topics.posts_count = 1")
end

advanced_filter(/^status:single_user$/) do |posts|
advanced_filter(/^status:single_user$/i) do |posts|
posts.where("topics.participant_count = 1")
end

advanced_filter(/^posts_count:(\d+)$/) do |posts, match|
advanced_filter(/^posts_count:(\d+)$/i) do |posts, match|
posts.where("topics.posts_count = ?", match.to_i)
end

advanced_filter(/^min_post_count:(\d+)$/) do |posts, match|
advanced_filter(/^min_post_count:(\d+)$/i) do |posts, match|
posts.where("topics.posts_count >= ?", match.to_i)
end

advanced_filter(/^in:first|^f$/) do |posts|
advanced_filter(/^in:first|^f$/i) do |posts|
posts.where("posts.post_number = 1")
end

advanced_filter(/^in:pinned$/) do |posts|
advanced_filter(/^in:pinned$/i) do |posts|
posts.where("topics.pinned_at IS NOT NULL")
end

advanced_filter(/^in:wiki$/) do |posts, match|
advanced_filter(/^in:wiki$/i) do |posts, match|
posts.where(wiki: true)
end

advanced_filter(/^badge:(.*)$/) do |posts, match|
advanced_filter(/^badge:(.*)$/i) do |posts, match|
badge_id = Badge.where('name ilike ? OR id = ?', match, match.to_i).pluck_first(:id)
if badge_id
posts.where('posts.user_id IN (SELECT ub.user_id FROM user_badges ub WHERE ub.badge_id = ?)', badge_id)
Expand All @@ -403,34 +403,34 @@ def post_action_type_filter(posts, post_action_type)
)")
end

advanced_filter(/^in:(likes)$/) do |posts, match|
advanced_filter(/^in:(likes)$/i) do |posts, match|
if @guardian.user
post_action_type_filter(posts, PostActionType.types[:like])
end
end

advanced_filter(/^in:(bookmarks)$/) do |posts, match|
advanced_filter(/^in:(bookmarks)$/i) do |posts, match|
if @guardian.user
posts.where("posts.id IN (SELECT post_id FROM bookmarks WHERE bookmarks.user_id = #{@guardian.user.id})")
end
end

advanced_filter(/^in:posted$/) do |posts|
advanced_filter(/^in:posted$/i) do |posts|
posts.where("posts.user_id = #{@guardian.user.id}") if @guardian.user
end

advanced_filter(/^in:created$/) do |posts|
advanced_filter(/^in:created$/i) do |posts|
posts.where(user_id: @guardian.user.id, post_number: 1) if @guardian.user
end

advanced_filter(/^created:@(.*)$/) do |posts, match|
advanced_filter(/^created:@(.*)$/i) do |posts, match|
user_id = User.where(username: match.downcase).pluck_first(:id)
posts.where(user_id: user_id, post_number: 1)
end

advanced_filter(/^in:(watching|tracking)$/) do |posts, match|
advanced_filter(/^in:(watching|tracking)$/i) do |posts, match|
if @guardian.user
level = TopicUser.notification_levels[match.to_sym]
level = TopicUser.notification_levels[match.downcase.to_sym]
posts.where("posts.topic_id IN (
SELECT tu.topic_id FROM topic_users tu
WHERE tu.user_id = :user_id AND
Expand All @@ -440,7 +440,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^in:seen$/) do |posts|
advanced_filter(/^in:seen$/i) do |posts|
if @guardian.user
posts
.joins("INNER JOIN post_timings ON
Expand All @@ -451,7 +451,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^in:unseen$/) do |posts|
advanced_filter(/^in:unseen$/i) do |posts|
if @guardian.user
posts
.joins("LEFT JOIN post_timings ON
Expand All @@ -463,11 +463,11 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^with:images$/) do |posts|
advanced_filter(/^with:images$/i) do |posts|
posts.where("posts.image_upload_id IS NOT NULL")
end

advanced_filter(/^category:(.+)$/) do |posts, match|
advanced_filter(/^category:(.+)$/i) do |posts, match|
exact = false

if match[0] == "="
Expand All @@ -491,7 +491,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^\#([\p{L}\p{M}0-9\-:=]+)$/) do |posts, match|
advanced_filter(/^\#([\p{L}\p{M}0-9\-:=]+)$/i) do |posts, match|

exact = true

Expand Down Expand Up @@ -570,7 +570,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^group:(.+)$/) do |posts, match|
advanced_filter(/^group:(.+)$/i) do |posts, match|
group_id = Group.where('name ilike ? OR (id = ? AND id > 0)', match, match.to_i).pluck_first(:id)
if group_id
posts.where("posts.user_id IN (select gu.user_id from group_users gu where gu.group_id = ?)", group_id)
Expand All @@ -579,7 +579,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^user:(.+)$/) do |posts, match|
advanced_filter(/^user:(.+)$/i) do |posts, match|
user_id = User.where(staged: false).where('username_lower = ? OR id = ?', match.downcase, match.to_i).pluck_first(:id)
if user_id
posts.where("posts.user_id = #{user_id}")
Expand All @@ -588,7 +588,7 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^\@([a-zA-Z0-9_\-.]+)$/) do |posts, match|
advanced_filter(/^\@([a-zA-Z0-9_\-.]+)$/i) do |posts, match|
user_id = User.where(staged: false).where(username_lower: match.downcase).pluck_first(:id)
if user_id
posts.where("posts.user_id = #{user_id}")
Expand All @@ -597,31 +597,31 @@ def post_action_type_filter(posts, post_action_type)
end
end

advanced_filter(/^before:(.*)$/) do |posts, match|
advanced_filter(/^before:(.*)$/i) do |posts, match|
if date = Search.word_to_date(match)
posts.where("posts.created_at < ?", date)
else
posts
end
end

advanced_filter(/^after:(.*)$/) do |posts, match|
advanced_filter(/^after:(.*)$/i) do |posts, match|
if date = Search.word_to_date(match)
posts.where("posts.created_at > ?", date)
else
posts
end
end

advanced_filter(/^tags?:([\p{L}\p{M}0-9,\-_+]+)$/) do |posts, match|
advanced_filter(/^tags?:([\p{L}\p{M}0-9,\-_+]+)$/i) do |posts, match|
search_tags(posts, match, positive: true)
end

advanced_filter(/^\-tags?:([\p{L}\p{M}0-9,\-_+]+)$/) do |posts, match|
advanced_filter(/^\-tags?:([\p{L}\p{M}0-9,\-_+]+)$/i) do |posts, match|
search_tags(posts, match, positive: false)
end

advanced_filter(/^filetypes?:([a-zA-Z0-9,\-_]+)$/) do |posts, match|
advanced_filter(/^filetypes?:([a-zA-Z0-9,\-_]+)$/i) do |posts, match|
file_extensions = match.split(",").map(&:downcase)
posts.where("posts.id IN (
SELECT post_id
Expand Down Expand Up @@ -682,13 +682,13 @@ def process_advanced_search!(term)
if word == 'l'
@order = :latest
nil
elsif word =~ /order:\w+/
@order = word.gsub('order:', '').to_sym
elsif word =~ /^order:\w+$/i
@order = word.downcase.gsub('order:', '').to_sym
nil
elsif word == 'in:title' || word == 't'
elsif word =~ /^in:title$/i || word == 't'
@in_title = true
nil
elsif word =~ /topic:(\d+)/
elsif word =~ /^topic:(\d+)$/i
topic_id = $1.to_i
if topic_id > 1
topic = Topic.find_by(id: topic_id)
Expand All @@ -697,16 +697,16 @@ def process_advanced_search!(term)
end
end
nil
elsif word == 'in:all'
elsif word =~ /^in:all$/i
@search_all_topics = true
nil
elsif word == 'in:personal'
elsif word =~ /^in:personal$/i
@search_pms = true
nil
elsif word == "in:personal-direct"
elsif word =~ /^in:personal-direct$/i
@search_pms = true
nil
elsif word =~ /^personal_messages:(.+)$/
elsif word =~ /^personal_messages:(.+)$/i
if user = User.find_by_username($1)
@search_pms = true
@search_context = user
Expand Down

0 comments on commit 4abbe3d

Please sign in to comment.