FIX: Correct `personal_messages:<username>` advanced search filter.

Renamed from `private_messages` to `personal_messages` without
deprecation because the `private_messages` advanced search filter never
worked in the first place when it was implemented.
This commit is contained in:
Guo Xiang Tan 2020-08-24 11:53:07 +08:00
parent 9022e4516f
commit 4b30799054
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
3 changed files with 40 additions and 21 deletions

View File

@ -98,13 +98,18 @@ export default createWidget("search-menu", {
query += `q=${encodeURIComponent(searchData.term)}`; query += `q=${encodeURIComponent(searchData.term)}`;
if (contextEnabled && ctx) { if (contextEnabled && ctx) {
if ( if (type === "private_messages") {
this.currentUser && if (
ctx.id.toString().toLowerCase() === this.currentUser &&
this.currentUser.get("username_lower") && ctx.id.toString().toLowerCase() ===
type === "private_messages" this.currentUser.get("username_lower")
) { ) {
query += " in:personal"; query += " in:personal";
} else {
query += encodeURIComponent(
` personal_messages:${ctx.id.toString().toLowerCase()}`
);
}
} else { } else {
query += encodeURIComponent(" " + type + ":" + ctx.id); query += encodeURIComponent(" " + type + ":" + ctx.id);
} }

View File

@ -185,9 +185,8 @@ class Search
@original_term = PG::Connection.escape_string(@term) @original_term = PG::Connection.escape_string(@term)
end end
if @search_pms && @guardian.user if @search_pms
@opts[:type_filter] = "private_messages" @opts[:type_filter] = "private_messages"
@search_context = @guardian.user
end end
if @search_all_topics && @guardian.user if @search_all_topics && @guardian.user
@ -690,13 +689,20 @@ class Search
nil nil
elsif word == 'in:personal' elsif word == 'in:personal'
@search_pms = true @search_pms = true
@search_context = @guardian.user
nil nil
elsif word == "in:personal-direct" elsif word == "in:personal-direct"
@search_pms = true @search_pms = true
@direct_pms_only = true @direct_pms_only = true
@search_context = @guardian.user
nil nil
elsif word =~ /^personal_messages:(.+)$/ elsif word =~ /^personal_messages:(.+)$/
@search_pms = true @search_pms = true
if user = User.find_by_username($1)
@search_context = user
end
nil nil
else else
found ? nil : word found ? nil : word

View File

@ -190,19 +190,20 @@ describe Search do
end end
context 'private messages' do context 'private messages' do
let!(:topic) do let!(:post) { Fabricate(:private_message_post) }
Fabricate(:topic, category_id: nil, archetype: 'private_message')
end
let!(:post) { Fabricate(:post, topic: topic) } let(:topic) { post.topic }
let!(:reply) do let!(:reply) do
Fabricate(:post, topic: topic, raw: 'hello from mars, we just landed') Fabricate(:private_message_post,
topic: post.topic,
raw: 'hello from mars, we just landed',
user: post.user
)
end end
let!(:post2) do let!(:post2) do
Fabricate(:post, Fabricate(:private_message_post,
topic: Fabricate(:topic, category_id: nil, archetype: 'private_message'),
raw: 'another secret pm from mars, testing' raw: 'another secret pm from mars, testing'
) )
end end
@ -212,9 +213,6 @@ describe Search do
Search.execute('mars', type_filter: 'private_messages') Search.execute('mars', type_filter: 'private_messages')
end.to raise_error(Discourse::InvalidAccess) end.to raise_error(Discourse::InvalidAccess)
TopicAllowedUser.create!(user_id: reply.user_id, topic_id: topic.id)
TopicAllowedUser.create!(user_id: post.user_id, topic_id: topic.id)
results = Search.execute( results = Search.execute(
'mars', 'mars',
type_filter: 'private_messages', type_filter: 'private_messages',
@ -260,7 +258,6 @@ describe Search do
results = Search.execute( results = Search.execute(
'mars in:personal', 'mars in:personal',
search_context: post.user,
guardian: Guardian.new(post.user) guardian: Guardian.new(post.user)
) )
@ -283,6 +280,17 @@ describe Search do
expect(results.posts).to contain_exactly(reply) expect(results.posts).to contain_exactly(reply)
end end
context 'personal_messages filter' do
it 'correctly searches for the PM of the given user' do
results = Search.execute(
"mars personal_messages:#{post.user.username}",
guardian: Guardian.new(post.user)
)
expect(results.posts).to contain_exactly(reply)
end
end
context 'personal-direct flag' do context 'personal-direct flag' do
let(:current) { Fabricate(:user, admin: true, username: "current_user") } let(:current) { Fabricate(:user, admin: true, username: "current_user") }
let(:participant) { Fabricate(:user, username: "participant_1") } let(:participant) { Fabricate(:user, username: "participant_1") }