bare searching for username; missing commenter explanation

This commit is contained in:
Peter Bhat Harkins 2023-09-26 21:29:04 -05:00
parent 078e454e37
commit 734476b73d
4 changed files with 28 additions and 6 deletions

View File

@ -124,7 +124,7 @@ class Search
parse_tree.each do |node|
type, value = node.first
case type
when :commenter
when :commenter, :user
n_commenters += 1
return invalid("A comment only has one commenter") if n_commenters > 1
query.joins!(:user).where!(users: {username: value.to_s})
@ -240,7 +240,7 @@ class Search
n_domains += 1
return invalid("A story can't be from multiple domains at once") if n_domains > 1
query.joins!(:domain).where!(domains: {domain: value.to_s})
when :submitter
when :submitter, :user
n_submitters += 1
return invalid("A story only has one submitter") if n_submitters > 1
query.joins!(:user).where!(user: {username: value.to_s})

View File

@ -25,6 +25,8 @@ class SearchParser < Parslet::Parser
match("[A-Za-z0-9\\-_.:@/()%~?&=#]").repeat(1)
).as(:url) >> space?
}
# User::VALID_USERNAME
rule(:user) { match("[@~]") >> match("[A-Za-z0-9_\\-]").repeat(1, 24).as(:user) >> space? }
rule(:negated) { str("-") >> (domain | tag | quoted | term).as(:negated) >> space? }
# catchall consumes ill-structured input
@ -36,9 +38,9 @@ class SearchParser < Parslet::Parser
domain |
submitter |
tag |
# title before quoted so that doesn't consume the quotes
title |
title | # title before quoted so that doesn't consume the quotes
url |
user | # user must come after commenter and submitter
# term and quoted after operators they would fail to consume
term |
quoted |

View File

@ -58,8 +58,10 @@
type, value = node.first
case type
when :domain
%>
when :commenter %>
<dt><span class="searchq">commenter:<%= value %></span></dt>
<dd>Comment by: <%= link_to value, user_path(value) %></dd>
<% when :domain %>
<dt><span class="searchq">domain:<%= value %></span></dt>
<dd>Domain: <%= link_to value, domain_path(value) %></dd>
<% when :submitter %>
@ -71,6 +73,9 @@
<% when :title %>
<dt><span class="searchq">title:<%= @search.flatten_title(value) %></span></dt>
<dd>Title: <%= @search.flatten_title(value) %></dd>
<% when :user %>
<dt><span class="searchq">@<%= value %></span></dt>
<dd><%= @search.searched_model.name %> by: <%= link_to value, user_path(value) %></dd>
<% when :negated %>
<% when :quoted %>
<dt><span class="searchq">"<%= value %>"</span></dt>

View File

@ -65,6 +65,13 @@ describe SearchParser do
it("parses multiple words") { expect(sp.quoted).to parse('"research words"') }
end
describe "commenter rule" do
it("parses username") { expect(sp.commenter).to parse("commenter:alice") }
it("parses with @") { expect(sp.commenter).to parse("commenter:@bob") }
it("parses with ~") { expect(sp.commenter).to parse("commenter:~carol") }
it("doesn't parse blank") { expect(sp.commenter).to_not parse("commenter:") }
end
describe "domain rule" do
it("parses single") { expect(sp.domain).to parse("domain:example.com") }
it("parses dash") { expect(sp.domain).to parse("domain:foo-bar.com") }
@ -90,6 +97,14 @@ describe SearchParser do
it("parses punctuation stripped from terms") { expect(sp.url).to parse("https://example.com/foo-bar&a=b") }
end
describe "user rule" do
it("parses with @") { expect(sp.user).to parse("@bob") }
it("parses with ~") { expect(sp.user).to parse("~carol") }
it("doesn't parse blank @") { expect(sp.user).to_not parse("@") }
it("doesn't parse emails") { expect(sp.user).to_not parse("user@example.com") }
it("doesn't parse blank ~") { expect(sp.user).to_not parse("~") }
end
describe "title rule" do
it("parses single") { expect(sp.title).to parse("title:seven") }
it("does parse single word quote") { expect(sp.title).to parse('title:"tips"') }