Commit ac52cef5 authored by Jonne Haß's avatar Jonne Haß Committed by Dennis Schubert

Only return the current_users participation for post interactions

closes #6007
parent b026a2af
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
* Fix a freeze in new post parsing [#5965](https://github.com/diaspora/diaspora/pull/5965) * Fix a freeze in new post parsing [#5965](https://github.com/diaspora/diaspora/pull/5965)
* Add case insensitive unconfirmed email addresses as authentication key [#5967](https://github.com/diaspora/diaspora/pull/5967) * Add case insensitive unconfirmed email addresses as authentication key [#5967](https://github.com/diaspora/diaspora/pull/5967)
* Fix liking on single post views when accessed via GUID [#5978](https://github.com/diaspora/diaspora/pull/5978) * Fix liking on single post views when accessed via GUID [#5978](https://github.com/diaspora/diaspora/pull/5978)
* Only return the current_users participation for post interactions [#6007](https://github.com/diaspora/diaspora/pull/6007)
## Features ## Features
* Hide post title of limited post in comment notification email [#5843](https://github.com/diaspora/diaspora/pull/5843) * Hide post title of limited post in comment notification email [#5843](https://github.com/diaspora/diaspora/pull/5843)
......
...@@ -11,8 +11,7 @@ class Post < ActiveRecord::Base ...@@ -11,8 +11,7 @@ class Post < ActiveRecord::Base
include Diaspora::Commentable include Diaspora::Commentable
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
attr_accessor :user_like attr_accessor :user_like
......
class PostInteractionPresenter
def initialize(post, current_user)
@post = post
@current_user = current_user
end
def as_json(_options={})
{
likes: as_api(@post.likes),
reshares: PostPresenter.collection_json(@post.reshares, @current_user),
comments: CommentPresenter.as_collection(@post.comments.order("created_at ASC")),
participations: as_api(participations),
comments_count: @post.comments_count,
likes_count: @post.likes_count,
reshares_count: @post.reshares_count
}
end
def participations
return @post.participations.none unless @current_user
@post.participations.where(author: @current_user.person)
end
def as_api(collection)
collection.includes(author: :profile).map {|element|
element.as_api_response(:backbone)
}
end
end
...@@ -92,28 +92,3 @@ class PostPresenter ...@@ -92,28 +92,3 @@ class PostPresenter
end end
end end
class PostInteractionPresenter
def initialize(post, current_user)
@post = post
@current_user = current_user
end
def as_json(options={})
{
likes: as_api(@post.likes),
reshares: PostPresenter.collection_json(@post.reshares, @current_user),
comments: CommentPresenter.as_collection(@post.comments.order("created_at ASC")),
participations: as_api(@post.participations),
comments_count: @post.comments_count,
likes_count: @post.likes_count,
reshares_count: @post.reshares_count
}
end
def as_api(collection)
collection.includes(:author => :profile).map do |element|
element.as_api_response(:backbone)
end
end
end
...@@ -26,12 +26,14 @@ FactoryGirl.define do ...@@ -26,12 +26,14 @@ FactoryGirl.define do
image_url_small "http://example.com/image_small.jpg" image_url_small "http://example.com/image_small.jpg"
end end
factory :person do factory(:person, aliases: %i(author)) do
sequence(:diaspora_handle) { |n| "bob-person-#{n}#{r_str}@example.net" } sequence(:diaspora_handle) {|n| "bob-person-#{n}#{r_str}@example.net" }
url AppConfig.pod_uri.to_s url AppConfig.pod_uri.to_s
serialized_public_key OpenSSL::PKey::RSA.generate(1024).public_key.export serialized_public_key OpenSSL::PKey::RSA.generate(1024).public_key.export
after(:build) do |person| after(:build) do |person|
person.profile = FactoryGirl.build(:profile, :person => person) unless person.profile.first_name.present? unless person.profile.first_name.present?
person.profile = FactoryGirl.build(:profile, person: person)
end
end end
after(:create) do |person| after(:create) do |person|
person.profile.save person.profile.save
...@@ -84,32 +86,50 @@ FactoryGirl.define do ...@@ -84,32 +86,50 @@ FactoryGirl.define do
user user
end end
factory(:status_message) do factory(:status_message, aliases: %i(status_message_without_participation)) do
sequence(:text) { |n| "jimmy's #{n} whales" } sequence(:text) {|n| "jimmy's #{n} whales" }
association :author, :factory => :person author
after(:build) do |sm| after(:build) do |sm|
sm.diaspora_handle = sm.author.diaspora_handle sm.diaspora_handle = sm.author.diaspora_handle
end end
end
factory(:status_message_with_poll, :parent => :status_message) do factory(:status_message_with_poll) do
after(:build) do |sm| after(:build) do |sm|
FactoryGirl.create(:poll, :status_message => sm) FactoryGirl.create(:poll, status_message: sm)
end
end end
end
factory(:status_message_with_photo, :parent => :status_message) do factory(:status_message_with_photo) do
sequence(:text) { |n| "There are #{n} ninjas in this photo." } sequence(:text) {|n| "There are #{n} ninjas in this photo." }
after(:build) do |sm| after(:build) do |sm|
FactoryGirl.create(:photo, :author => sm.author, :status_message => sm, :pending => false, :public => sm.public) FactoryGirl.create(
:photo,
author: sm.author,
status_message: sm,
pending: false,
public: sm.public
)
end
end
factory(:status_message_in_aspect) do
public false
after(:build) do |sm|
sm.author = FactoryGirl.create(:user_with_aspect).person
sm.aspects << sm.author.owner.aspects.first
end
end end
end
factory(:status_message_in_aspect, parent: :status_message) do factory(:status_message_with_participations) do
self.public false transient do
after :build do |sm| participants []
sm.author = FactoryGirl.create(:user_with_aspect).person end
sm.aspects << sm.author.owner.aspects.first after(:build) do |sm, ev|
ev.participants.each do |participant|
person = participant.is_a?(User) ? participant.person : participant
sm.participations.build(author: person)
end
end
end end
end end
......
require "spec_helper"
describe PostInteractionPresenter do
let(:status_message_without_participation) {
FactoryGirl.create(:status_message_without_participation)
}
let(:status_message_with_participations) {
FactoryGirl.create(:status_message_with_participations, participants: [alice, bob])
}
context "with an user" do
context "without a participation" do
let(:presenter) { PostInteractionPresenter.new(status_message_without_participation, alice) }
it "returns an empty array for participations" do
expect(presenter.as_json[:participations]).to be_empty
end
end
context "with a participation" do
let(:presenter) { PostInteractionPresenter.new(status_message_with_participations, alice) }
it "returns the users own participation only" do
expect(presenter.as_json[:participations]).to eq [alice.participations.first.as_api_response(:backbone)]
end
end
end
context "without an user" do
let(:presenter) { PostInteractionPresenter.new(status_message_with_participations, nil) }
it "returns an empty array" do
expect(presenter.as_json[:participations]).to be_empty
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment