Unverified Commit 436934e8 authored by Steffen van Bergerem's avatar Steffen van Bergerem Committed by Benjamin Neff
Browse files

Refactor reshares controller using reshare service

parent 44b616ed
Loading
Loading
Loading
Loading
+10 −18
Original line number Diff line number Diff line
@@ -3,30 +3,22 @@ class ResharesController < ApplicationController
  respond_to :json

  def create
    post = Post.where(:guid => params[:root_guid]).first
    if post.is_a? Reshare
      @reshare = current_user.build_post(:reshare, :root_guid => post.absolute_root.guid)
    else
      @reshare = current_user.build_post(:reshare, :root_guid => params[:root_guid])
    end

    if @reshare.save
      current_user.dispatch_post(@reshare)
      render :json => ExtremePostPresenter.new(@reshare, current_user), :status => 201
    else
    reshare = reshare_service.create(params[:root_guid])
  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
    render text: I18n.t("reshares.create.error"), status: 422
    end
  else
    render json: ExtremePostPresenter.new(reshare, current_user), status: 201
  end

  def index
    @reshares = target.reshares.includes(author: :profile)
    render json: @reshares.as_api_response(:backbone)
    render json: reshare_service.find_for_post(params[:post_id])
      .includes(author: :profile)
      .as_api_response(:backbone)
  end

  private

  def target
    @target ||= current_user.find_visible_shareable_by_id(Post, params[:post_id]) ||
      raise(ActiveRecord::RecordNotFound.new)
  def reshare_service
    @reshare_service ||= ReshareService.new(current_user)
  end
end
+3 −2
Original line number Diff line number Diff line
@@ -31,8 +31,9 @@ describe ResharesController, :type => :controller do
        }.to change(Reshare, :count).by(1)
      end

      it 'calls dispatch' do
        expect(bob).to receive(:dispatch_post)
      it "federates" do
        allow_any_instance_of(Participation::Generator).to receive(:create!)
        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
        post_request!
      end