reblogs_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::ReblogsController < Api::BaseController
  3. include Authorization
  4. include Redisable
  5. include Lockable
  6. before_action -> { doorkeeper_authorize! :write, :'write:statuses' }
  7. before_action :require_user!
  8. before_action :set_reblog, only: [:create]
  9. override_rate_limit_headers :create, family: :statuses
  10. def create
  11. with_lock("reblog:#{current_account.id}:#{@reblog.id}") do
  12. @status = ReblogService.new.call(current_account, @reblog, reblog_params)
  13. end
  14. render json: @status, serializer: REST::StatusSerializer
  15. end
  16. def destroy
  17. @status = current_account.statuses.find_by(reblog_of_id: params[:status_id])
  18. if @status
  19. authorize @status, :unreblog?
  20. @status.discard
  21. RemovalWorker.perform_async(@status.id)
  22. @reblog = @status.reblog
  23. else
  24. @reblog = Status.find(params[:status_id])
  25. authorize @reblog, :show?
  26. end
  27. render json: @reblog, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, reblogs_map: { @reblog.id => false })
  28. rescue Mastodon::NotPermittedError
  29. not_found
  30. end
  31. private
  32. def set_reblog
  33. @reblog = Status.find(params[:status_id])
  34. authorize @reblog, :show?
  35. rescue Mastodon::NotPermittedError
  36. not_found
  37. end
  38. def reblog_params
  39. params.permit(:visibility)
  40. end
  41. end