Refactor reshares controller using reshare service
This commit is contained in:
parent
44b616eda8
commit
436934e890
2 changed files with 13 additions and 20 deletions
|
|
@ -3,30 +3,22 @@ class ResharesController < ApplicationController
|
||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
||||||
def create
|
def create
|
||||||
post = Post.where(:guid => params[:root_guid]).first
|
reshare = reshare_service.create(params[:root_guid])
|
||||||
if post.is_a? Reshare
|
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
||||||
@reshare = current_user.build_post(:reshare, :root_guid => post.absolute_root.guid)
|
render text: I18n.t("reshares.create.error"), status: 422
|
||||||
else
|
else
|
||||||
@reshare = current_user.build_post(:reshare, :root_guid => params[:root_guid])
|
render json: ExtremePostPresenter.new(reshare, current_user), status: 201
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@reshares = target.reshares.includes(author: :profile)
|
render json: reshare_service.find_for_post(params[:post_id])
|
||||||
render json: @reshares.as_api_response(:backbone)
|
.includes(author: :profile)
|
||||||
|
.as_api_response(:backbone)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def target
|
def reshare_service
|
||||||
@target ||= current_user.find_visible_shareable_by_id(Post, params[:post_id]) ||
|
@reshare_service ||= ReshareService.new(current_user)
|
||||||
raise(ActiveRecord::RecordNotFound.new)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,9 @@ describe ResharesController, :type => :controller do
|
||||||
}.to change(Reshare, :count).by(1)
|
}.to change(Reshare, :count).by(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'calls dispatch' do
|
it "federates" do
|
||||||
expect(bob).to receive(:dispatch_post)
|
allow_any_instance_of(Participation::Generator).to receive(:create!)
|
||||||
|
expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
|
||||||
post_request!
|
post_request!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue