Move 'distinct' to 'allowed_to_be_mentioned_in_a_comment_to'
This commit is contained in:
parent
3d95642aca
commit
509f429cc8
3 changed files with 18 additions and 23 deletions
|
|
@ -11,7 +11,7 @@ module Notifications
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.filter_mentions(mentions, mentionable, _recipient_user_ids)
|
def self.filter_mentions(mentions, mentionable, _recipient_user_ids)
|
||||||
mentions.includes(:person).merge(Person.allowed_to_be_mentioned_in_a_comment_to(mentionable.parent)).distinct
|
mentions.includes(:person).merge(Person.allowed_to_be_mentioned_in_a_comment_to(mentionable.parent))
|
||||||
end
|
end
|
||||||
|
|
||||||
def mail_job
|
def mail_job
|
||||||
|
|
|
||||||
|
|
@ -61,11 +61,7 @@ class Person < ActiveRecord::Base
|
||||||
}
|
}
|
||||||
scope :remote, -> { where('people.owner_id IS NULL') }
|
scope :remote, -> { where('people.owner_id IS NULL') }
|
||||||
scope :local, -> { where('people.owner_id IS NOT NULL') }
|
scope :local, -> { where('people.owner_id IS NOT NULL') }
|
||||||
scope :for_json, -> {
|
scope :for_json, -> { select("people.id, people.guid, people.diaspora_handle").includes(:profile) }
|
||||||
select("people.id, people.guid, people.diaspora_handle")
|
|
||||||
.distinct
|
|
||||||
.includes(:profile)
|
|
||||||
}
|
|
||||||
|
|
||||||
# @note user is passed in here defensively
|
# @note user is passed in here defensively
|
||||||
scope :all_from_aspects, ->(aspect_ids, user) {
|
scope :all_from_aspects, ->(aspect_ids, user) {
|
||||||
|
|
@ -131,12 +127,13 @@ class Person < ActiveRecord::Base
|
||||||
# @param [Post] the post for which we query mentionable in comments people
|
# @param [Post] the post for which we query mentionable in comments people
|
||||||
# @return [Person::ActiveRecord_Relation]
|
# @return [Person::ActiveRecord_Relation]
|
||||||
scope :allowed_to_be_mentioned_in_a_comment_to, ->(post) {
|
scope :allowed_to_be_mentioned_in_a_comment_to, ->(post) {
|
||||||
if post.public?
|
allowed = if post.public?
|
||||||
all
|
all
|
||||||
else
|
else
|
||||||
left_join_visible_post_interactions_on_authorship(post.id)
|
left_join_visible_post_interactions_on_authorship(post.id)
|
||||||
.where("comments.id IS NOT NULL OR likes.id IS NOT NULL OR people.id = #{post.author_id}")
|
.where("comments.id IS NOT NULL OR likes.id IS NOT NULL OR people.id = #{post.author_id}")
|
||||||
end
|
end
|
||||||
|
allowed.distinct
|
||||||
}
|
}
|
||||||
|
|
||||||
# This scope adds sorting of people in the order, appropriate for suggesting to a user (current user) who
|
# This scope adds sorting of people in the order, appropriate for suggesting to a user (current user) who
|
||||||
|
|
|
||||||
|
|
@ -24,16 +24,6 @@ describe Person, :type => :model do
|
||||||
Person.for_json.first.serialized_public_key
|
Person.for_json.first.serialized_public_key
|
||||||
}.to raise_error ActiveModel::MissingAttributeError
|
}.to raise_error ActiveModel::MissingAttributeError
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'selects distinct people' do
|
|
||||||
aspect = bob.aspects.create(:name => 'hilarious people')
|
|
||||||
aspect.contacts << bob.contact_for(eve.person)
|
|
||||||
person_ids = Person.for_json.joins(:contacts => :aspect_memberships).
|
|
||||||
where(:contacts => {:user_id => bob.id},
|
|
||||||
:aspect_memberships => {:aspect_id => bob.aspect_ids}).map{|p| p.id}
|
|
||||||
|
|
||||||
expect(person_ids.uniq).to eq(person_ids)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.local' do
|
describe '.local' do
|
||||||
|
|
@ -131,9 +121,17 @@ describe Person, :type => :model do
|
||||||
).to match_array([alice, bob, eve, kate].map(&:person_id))
|
).to match_array([alice, bob, eve, kate].map(&:person_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "selects distinct people" do
|
||||||
|
alice.comment!(status_bob, "hyi")
|
||||||
|
alice.comment!(status_bob, "how are you?")
|
||||||
|
expect(
|
||||||
|
Person.allowed_to_be_mentioned_in_a_comment_to(status_bob).ids
|
||||||
|
).to match_array([alice, bob].map(&:person_id))
|
||||||
|
end
|
||||||
|
|
||||||
it "returns all for public posts" do
|
it "returns all for public posts" do
|
||||||
status_bob.update(public: true) # set parent public
|
status_bob.update(public: true) # set parent public
|
||||||
expect(Person.allowed_to_be_mentioned_in_a_comment_to(status_bob)).to eq(Person.all)
|
expect(Person.allowed_to_be_mentioned_in_a_comment_to(status_bob).ids).to match_array(Person.ids)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue