add resharers and participants to subscribers on public posts

This commit is contained in:
Benjamin Neff 2016-06-09 01:00:46 +02:00
parent 566f4890a2
commit 7893a23927
3 changed files with 53 additions and 32 deletions

View file

@ -14,6 +14,7 @@ class Post < ActiveRecord::Base
include Diaspora::Shareable include Diaspora::Shareable
has_many :participations, dependent: :delete_all, as: :target, inverse_of: :target has_many :participations, dependent: :delete_all, as: :target, inverse_of: :target
has_many :participants, class_name: "Person", through: :participations, source: :author
attr_accessor :user_like attr_accessor :user_like
@ -21,10 +22,10 @@ class Post < ActiveRecord::Base
has_many :reports, as: :item has_many :reports, as: :item
has_many :mentions, :dependent => :destroy has_many :mentions, dependent: :destroy
has_many :reshares, :class_name => "Reshare", :foreign_key => :root_guid, :primary_key => :guid has_many :reshares, class_name: "Reshare", foreign_key: :root_guid, primary_key: :guid
has_many :resharers, :class_name => 'Person', :through => :reshares, :source => :author has_many :resharers, class_name: "Person", through: :reshares, source: :author
belongs_to :o_embed_cache belongs_to :o_embed_cache
belongs_to :open_graph_cache belongs_to :open_graph_cache
@ -147,4 +148,10 @@ class Post < ActiveRecord::Base
def nsfw def nsfw
self.author.profile.nsfw? self.author.profile.nsfw?
end end
def subscribers
super.tap do |subscribers|
subscribers.concat(resharers).concat(participants) if public?
end
end
end end

View file

@ -5,11 +5,6 @@
require 'spec_helper' require 'spec_helper'
describe Post, :type => :model do describe Post, :type => :model do
before do
@user = alice
@aspect = @user.aspects.create(:name => "winners")
end
describe 'scopes' do describe 'scopes' do
describe '.owned_or_visible_by_user' do describe '.owned_or_visible_by_user' do
before do before do
@ -184,24 +179,14 @@ describe Post, :type => :model do
describe 'deletion' do describe 'deletion' do
it 'should delete a posts comments on delete' do it 'should delete a posts comments on delete' do
post = FactoryGirl.create(:status_message, :author => @user.person) post = FactoryGirl.create(:status_message, author: alice.person)
@user.comment!(post, "hey") alice.comment!(post, "hey")
post.destroy post.destroy
expect(Post.where(:id => post.id).empty?).to eq(true) expect(Post.where(:id => post.id).empty?).to eq(true)
expect(Comment.where(:text => "hey").empty?).to eq(true) expect(Comment.where(:text => "hey").empty?).to eq(true)
end end
end end
describe 'serialization' do
it 'should serialize the handle and not the sender' do
post = @user.post :status_message, :text => "hello", :to => @aspect.id
xml = Diaspora::Federation.xml(Diaspora::Federation::Entities.status_message(post)).to_xml
expect(xml.include?("person_id")).to be false
expect(xml.include?(@user.person.diaspora_handle)).to be true
end
end
describe '.diaspora_initialize' do describe '.diaspora_initialize' do
it 'takes provider_display_name' do it 'takes provider_display_name' do
sm = FactoryGirl.create(:status_message, :provider_display_name => 'mobile') sm = FactoryGirl.create(:status_message, :provider_display_name => 'mobile')
@ -209,17 +194,50 @@ describe Post, :type => :model do
end end
end end
describe '#subscribers' do describe "#subscribers" do
it 'returns the people contained in the aspects the post appears in' do let(:user) { FactoryGirl.create(:user_with_aspect) }
post = @user.post :status_message, :text => "hello", :to => @aspect.id
expect(post.subscribers).to eq([]) # TODO before do
user.share_with(alice.person, user.aspects.first)
end end
it 'returns all a users contacts if the post is public' do context "private" do
post = @user.post :status_message, :text => "hello", :to => @aspect.id, :public => true it "returns the people contained in the aspects the post appears in" do
post = user.post(:status_message, text: "hello", to: user.aspects.first.id)
expect(post.subscribers.to_set).to eq(@user.contact_people.to_set) expect(post.subscribers).to eq([alice.person])
end
it "returns empty if posted to an empty aspect" do
empty_aspect = user.aspects.create(name: "empty")
post = user.post(:status_message, text: "hello", to: empty_aspect.id)
expect(post.subscribers).to eq([])
end
end
context "public" do
let(:post) { user.post(:status_message, text: "hello", public: true) }
it "returns all a users contacts if the post is public" do
second_aspect = user.aspects.create(name: "winners")
user.share_with(bob.person, second_aspect)
expect(post.subscribers).to eq([alice.person, bob.person])
end
it "adds resharers to subscribers" do
FactoryGirl.create(:reshare, root: post, author: eve.person)
expect(post.subscribers).to eq([alice.person, eve.person])
end
it "adds participants to subscribers" do
eve.participate!(post)
expect(post.subscribers).to eq([alice.person, eve.person])
end
end end
end end
@ -261,7 +279,7 @@ describe Post, :type => :model do
describe '#reshares_count' do describe '#reshares_count' do
before :each do before :each do
@post = @user.post :status_message, :text => "hello", :to => @aspect.id, :public => true @post = alice.post(:status_message, text: "hello", public: true)
expect(@post.reshares.size).to eq(0) expect(@post.reshares.size).to eq(0)
end end

View file

@ -793,10 +793,6 @@ describe User, :type => :model do
bob.retract(post) bob.retract(post)
end end
it "adds resharers of target post as additional subsctibers" do
skip # TODO: add resharers to subscribers of posts
end
end end
end end