You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

  1. class Api::V1::StatusesController < ApiController
  2. before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
  3. before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
  4. before_action :require_user!, except: [:show, :context, :reblogged_by, :favourited_by]
  5. before_action :set_status, only: [:show, :context, :reblogged_by, :favourited_by]
  6. respond_to :json
  7. def show
  8. end
  9. def context
  10. @context = OpenStruct.new({ ancestors: @status.ancestors, descendants: @status.descendants })
  11. set_maps([@status] + @context[:ancestors] + @context[:descendants])
  12. end
  13. def reblogged_by
  14. results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  15. accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h
  16. @accounts = results.map { |r| accounts[r.account_id] }
  17. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  18. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) if results.size > 0
  19. set_pagination_headers(next_path, prev_path)
  20. render action: :accounts
  21. end
  22. def favourited_by
  23. results = @status.favourites.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  24. accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h
  25. @accounts = results.map { |f| accounts[f.account_id] }
  26. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  27. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) if results.size > 0
  28. set_pagination_headers(next_path, prev_path)
  29. render action: :accounts
  30. end
  31. def create
  32. @status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), params[:media_ids])
  33. render action: :show
  34. end
  35. def destroy
  36. @status = Status.where(account_id: current_user.account).find(params[:id])
  37. RemoveStatusService.new.call(@status)
  38. render_empty
  39. end
  40. def reblog
  41. @status = ReblogService.new.call(current_user.account, Status.find(params[:id])).reload
  42. render action: :show
  43. end
  44. def unreblog
  45. RemoveStatusService.new.call(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!)
  46. @status = Status.find(params[:id])
  47. render action: :show
  48. end
  49. def favourite
  50. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  51. render action: :show
  52. end
  53. def unfavourite
  54. @status = UnfavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  55. render action: :show
  56. end
  57. private
  58. def set_status
  59. @status = Status.find(params[:id])
  60. end
  61. end