FIX: do not show PM topics when moving posts to an existing public topic (#6876)

This commit is contained in:
Arpit Jalan 2019-01-14 15:00:45 +05:30 committed by GitHub
parent 78748f1501
commit a121d40771
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 17 deletions

View File

@ -32,20 +32,22 @@ export default Ember.Component.extend({
return;
}
searchForTerm(title, { typeFilter: "topic", searchForId: true }).then(
function(results) {
if (results && results.posts && results.posts.length > 0) {
self.set(
"topics",
results.posts
.mapBy("topic")
.filter(t => t.get("id") !== currentTopicId)
);
} else {
self.setProperties({ topics: null, loading: false });
}
searchForTerm(title, {
typeFilter: "topic",
searchForId: true,
restrictToArchetype: "regular"
}).then(function(results) {
if (results && results.posts && results.posts.length > 0) {
self.set(
"topics",
results.posts
.mapBy("topic")
.filter(t => t.get("id") !== currentTopicId)
);
} else {
self.setProperties({ topics: null, loading: false });
}
);
});
}, 300),
actions: {

View File

@ -107,6 +107,8 @@ export function searchForTerm(term, opts) {
const data = { term: term, include_blurbs: "true" };
if (opts.typeFilter) data.type_filter = opts.typeFilter;
if (opts.searchForId) data.search_for_id = true;
if (opts.restrictToArchetype)
data.restrict_to_archetype = opts.restrictToArchetype;
if (opts.searchContext) {
data.search_context = {

View File

@ -68,6 +68,7 @@ class SearchController < ApplicationController
search_args[:search_type] = :header
search_args[:ip_address] = request.remote_ip
search_args[:user_id] = current_user.id if current_user.present?
search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[:restrict_to_archetype].present?
search = Search.new(params[:term], search_args)
result = search.execute

View File

@ -227,7 +227,7 @@ class Search
end
# If the term is a number or url to a topic, just include that topic
if @opts[:search_for_id] && @results.type_filter == 'topic'
if @opts[:search_for_id] && (@results.type_filter == 'topic' || @results.type_filter == 'private_messages')
if @term =~ /^\d+$/
single_topic(@term.to_i)
else
@ -629,7 +629,14 @@ class Search
# If we're searching for a single topic
def single_topic(id)
post = Post.find_by(topic_id: id, post_number: 1)
if @opts[:restrict_to_archetype].present?
archetype = @opts[:restrict_to_archetype] == Archetype.default ? Archetype.default : Archetype.private_message
post = Post.joins(:topic)
.where("topics.id = :id AND topics.archetype = :archetype AND posts.post_number = 1", id: id, archetype: archetype)
.first
else
post = Post.find_by(topic_id: id, post_number: 1)
end
return nil unless @guardian.can_see?(post)
@results.add(post)

View File

@ -341,12 +341,32 @@ describe Search do
end
context "search for a topic by url" do
let(:result) { Search.execute(topic.relative_url, search_for_id: true, type_filter: 'topic') }
it 'returns the topic' do
result = Search.execute(topic.relative_url, search_for_id: true, type_filter: 'topic')
expect(result.posts.length).to eq(1)
expect(result.posts.first.id).to eq(post.id)
end
context 'restrict_to_archetype' do
let(:personal_message) { Fabricate(:private_message_topic) }
let!(:p1) { Fabricate(:post, topic: personal_message, post_number: 1) }
it 'restricts result to topics' do
result = Search.execute(personal_message.relative_url, search_for_id: true, type_filter: 'topic', restrict_to_archetype: Archetype.default)
expect(result.posts.length).to eq(0)
result = Search.execute(topic.relative_url, search_for_id: true, type_filter: 'topic', restrict_to_archetype: Archetype.default)
expect(result.posts.length).to eq(1)
end
it 'restricts result to messages' do
result = Search.execute(topic.relative_url, search_for_id: true, type_filter: 'private_messages', guardian: Guardian.new(Fabricate(:admin)), restrict_to_archetype: Archetype.private_message)
expect(result.posts.length).to eq(0)
result = Search.execute(personal_message.relative_url, search_for_id: true, type_filter: 'private_messages', guardian: Guardian.new(Fabricate(:admin)), restrict_to_archetype: Archetype.private_message)
expect(result.posts.length).to eq(1)
end
end
end
context 'security' do