FIX: when searching PMs also search group PMs
Users belonging to a group could not search for PMs unless explicitly added to the PM unless admin
This commit is contained in:
parent
10f2db67ba
commit
52ae63d5d7
|
@ -59,6 +59,18 @@ class Post < ActiveRecord::Base
|
||||||
|
|
||||||
SHORT_POST_CHARS = 1200
|
SHORT_POST_CHARS = 1200
|
||||||
|
|
||||||
|
scope :private_posts_for_user, ->(user) {
|
||||||
|
where("posts.topic_id IN (SELECT topic_id
|
||||||
|
FROM topic_allowed_users
|
||||||
|
WHERE user_id = :user_id
|
||||||
|
UNION ALL
|
||||||
|
SELECT tg.topic_id
|
||||||
|
FROM topic_allowed_groups tg
|
||||||
|
JOIN group_users gu ON gu.user_id = :user_id AND
|
||||||
|
gu.group_id = tg.group_id)",
|
||||||
|
user_id: user.id)
|
||||||
|
}
|
||||||
|
|
||||||
scope :by_newest, -> { order('created_at desc, id desc') }
|
scope :by_newest, -> { order('created_at desc, id desc') }
|
||||||
scope :by_post_number, -> { order('post_number ASC') }
|
scope :by_post_number, -> { order('post_number ASC') }
|
||||||
scope :with_user, -> { includes(:user) }
|
scope :with_user, -> { includes(:user) }
|
||||||
|
|
|
@ -610,7 +610,7 @@ class Search
|
||||||
posts = posts.where("topics.archetype = ?", Archetype.private_message)
|
posts = posts.where("topics.archetype = ?", Archetype.private_message)
|
||||||
|
|
||||||
unless @guardian.is_admin?
|
unless @guardian.is_admin?
|
||||||
posts = posts.where("topics.id IN (SELECT topic_id FROM topic_allowed_users WHERE user_id = ?)", @guardian.user.id)
|
posts = posts.private_posts_for_user(@guardian.user)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
posts = posts.where("topics.archetype <> ?", Archetype.private_message)
|
posts = posts.where("topics.archetype <> ?", Archetype.private_message)
|
||||||
|
@ -654,15 +654,7 @@ class Search
|
||||||
if @search_context.is_a?(User)
|
if @search_context.is_a?(User)
|
||||||
|
|
||||||
if opts[:private_messages]
|
if opts[:private_messages]
|
||||||
posts = posts.where("topics.id IN (SELECT topic_id
|
posts = posts.private_posts_for_user(@search_context)
|
||||||
FROM topic_allowed_users
|
|
||||||
WHERE user_id = :user_id
|
|
||||||
UNION ALL
|
|
||||||
SELECT tg.topic_id
|
|
||||||
FROM topic_allowed_groups tg
|
|
||||||
JOIN group_users gu ON gu.user_id = :user_id AND
|
|
||||||
gu.group_id = tg.group_id)",
|
|
||||||
user_id: @search_context.id)
|
|
||||||
else
|
else
|
||||||
posts = posts.where("posts.user_id = #{@search_context.id}")
|
posts = posts.where("posts.user_id = #{@search_context.id}")
|
||||||
end
|
end
|
||||||
|
|
|
@ -204,6 +204,20 @@ describe Search do
|
||||||
|
|
||||||
expect(results.posts.length).to eq(1)
|
expect(results.posts.length).to eq(1)
|
||||||
|
|
||||||
|
# can search group PMs as well as non admin
|
||||||
|
#
|
||||||
|
user = Fabricate(:user)
|
||||||
|
group = Fabricate.build(:group)
|
||||||
|
group.add(user)
|
||||||
|
group.save!
|
||||||
|
|
||||||
|
TopicAllowedGroup.create!(group_id: group.id, topic_id: topic.id)
|
||||||
|
|
||||||
|
results = Search.execute('mars in:private',
|
||||||
|
guardian: Guardian.new(user))
|
||||||
|
|
||||||
|
expect(results.posts.length).to eq(1)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue