diff --git a/app/controllers/reshares_controller.rb b/app/controllers/reshares_controller.rb index 22915891a895c711e37c1614e29971160a6c2846..385e6acfc6aef9bcbc24a7cbafaf8f76f31ea6d6 100644 --- a/app/controllers/reshares_controller.rb +++ b/app/controllers/reshares_controller.rb @@ -17,4 +17,16 @@ class ResharesController < ApplicationController render :nothing => true, :status => 422 end end + + def index + @reshares = target.reshares.includes(author: :profile) + render json: @reshares.as_api_response(:backbone) + end + + private + + def target + @target ||= current_user.find_visible_shareable_by_id(Post, params[:post_id]) || + raise(ActiveRecord::RecordNotFound.new) + end end diff --git a/app/models/reshare.rb b/app/models/reshare.rb index bb914d40146ee966423bae6f9887fa5a37db3145..a88208248e4b8dfd0d9d88f940a2047905ce01c6 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -21,6 +21,14 @@ class Reshare < Post self.root.update_reshares_counter if self.root.present? end + acts_as_api + api_accessible :backbone do |t| + t.add :id + t.add :guid + t.add :author + t.add :created_at + end + def root_diaspora_id root.try(:author).try(:diaspora_handle) end diff --git a/config/routes.rb b/config/routes.rb index 02b15089c3dbc1f81b1f750f31a43160b2ab5b68..190ad6b985bfeeb56a5a1be76d5f51cd1c2e2830 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,6 +38,7 @@ Diaspora::Application.routes.draw do resources :poll_participations, only: :create resources :likes, only: %i(create destroy index) resources :comments, only: %i(new create destroy index) + resources :reshares, only: :index end get 'p/:id' => 'posts#show', :as => 'short_post' diff --git a/spec/controllers/reshares_controller_spec.rb b/spec/controllers/reshares_controller_spec.rb index cc4b7e9ecbf7a9311840b86b75d79724be4dfc50..d33c9353643cdce96a54a611c046fe2c00960475 100644 --- a/spec/controllers/reshares_controller_spec.rb +++ b/spec/controllers/reshares_controller_spec.rb @@ -64,4 +64,44 @@ describe ResharesController, :type => :controller do end end end + + describe "#index" do + context "with a private post" do + before do + @alices_aspect = alice.aspects.where(name: "generic").first + @post = alice.post(:status_message, text: "hey", to: @alices_aspect.id) + end + + it "returns a 404 for a post not visible to the user" do + sign_in(eve, scope: :user) + expect { + get :index, post_id: @post.id, format: :json + }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "returns an empty array for a post visible to the user" do + sign_in(bob, scope: :user) + get :index, post_id: @post.id, format: :json + expect(JSON.parse(response.body)).to eq([]) + end + end + + context "with a public post" do + before do + sign_in(alice, scope: :user) + @post = alice.post(:status_message, text: "hey", public: true) + end + + it "returns an array of reshares for a post" do + bob.reshare!(@post) + get :index, post_id: @post.id, format: :json + expect(JSON.parse(response.body).map {|h| h["id"] }).to eq(@post.reshares.map(&:id)) + end + + it "returns an empty array for a post with no likes" do + get :index, post_id: @post.id, format: :json + expect(JSON.parse(response.body)).to eq([]) + end + end + end end