reblogs_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_redis_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. @reblog = @status.reblog
  21. count = [@reblog.reblogs_count - 1, 0].max
  22. @status.discard
  23. RemovalWorker.perform_async(@status.id)
  24. else
  25. @reblog = Status.find(params[:status_id])
  26. count = @reblog.reblogs_count
  27. authorize @reblog, :show?
  28. end
  29. relationships = StatusRelationshipsPresenter.new([@status], current_account.id, reblogs_map: { @reblog.id => false }, attributes_map: { @reblog.id => { reblogs_count: count } })
  30. render json: @reblog, serializer: REST::StatusSerializer, relationships: relationships
  31. rescue Mastodon::NotPermittedError
  32. not_found
  33. end
  34. private
  35. def set_reblog
  36. @reblog = Status.find(params[:status_id])
  37. authorize @reblog, :show?
  38. rescue Mastodon::NotPermittedError
  39. not_found
  40. end
  41. def reblog_params
  42. params.permit(:visibility)
  43. end
  44. end