Commit 2ec45317 authored by Steffen van Bergerem's avatar Steffen van Bergerem Committed by Benjamin Neff
Browse files

Add new JSON endpoint for reshares

parent 636c69c0
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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
+8 −0
Original line number Diff line number Diff line
@@ -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
+1 −0
Original line number Diff line number Diff line
@@ -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'
+40 −0
Original line number Diff line number Diff line
@@ -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