Refactor reshares controller using reshare service

This commit is contained in:
Steffen van Bergerem 2017-02-18 01:50:10 +01:00 committed by Benjamin Neff
parent 44b616eda8
commit 436934e890
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 13 additions and 20 deletions

View file

@ -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

View file

@ -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