diff --git a/app/controllers/reshares_controller.rb b/app/controllers/reshares_controller.rb index 7345e6343..4add93174 100644 --- a/app/controllers/reshares_controller.rb +++ b/app/controllers/reshares_controller.rb @@ -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 - render text: I18n.t("reshares.create.error"), status: 422 - end + reshare = reshare_service.create(params[:root_guid]) + rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid + render text: I18n.t("reshares.create.error"), status: 422 + 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 diff --git a/spec/controllers/reshares_controller_spec.rb b/spec/controllers/reshares_controller_spec.rb index 2dc48daca..38a65b94e 100644 --- a/spec/controllers/reshares_controller_spec.rb +++ b/spec/controllers/reshares_controller_spec.rb @@ -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