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.

100 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < ApiController
  3. before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
  4. before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
  5. before_action :require_user!, except: [:show, :context, :reblogged_by, :favourited_by]
  6. before_action :set_status, only: [:show, :context, :reblogged_by, :favourited_by]
  7. respond_to :json
  8. def show
  9. cached = Rails.cache.read(@status.cache_key)
  10. @status = cached unless cached.nil?
  11. end
  12. def context
  13. @context = OpenStruct.new(ancestors: @status.ancestors(current_account), descendants: @status.descendants(current_account))
  14. statuses = [@status] + @context[:ancestors] + @context[:descendants]
  15. set_maps(statuses)
  16. set_counters_maps(statuses)
  17. end
  18. def reblogged_by
  19. results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  20. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  21. @accounts = results.map { |r| accounts[r.account_id] }
  22. set_account_counters_maps(@accounts)
  23. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  24. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  25. set_pagination_headers(next_path, prev_path)
  26. render action: :accounts
  27. end
  28. def favourited_by
  29. results = @status.favourites.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  30. accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h
  31. @accounts = results.map { |f| accounts[f.account_id] }
  32. set_account_counters_maps(@accounts)
  33. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  34. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  35. set_pagination_headers(next_path, prev_path)
  36. render action: :accounts
  37. end
  38. def create
  39. @status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), media_ids: params[:media_ids], sensitive: params[:sensitive], visibility: params[:visibility])
  40. render action: :show
  41. end
  42. def destroy
  43. @status = Status.where(account_id: current_user.account).find(params[:id])
  44. RemovalWorker.perform_async(@status.id)
  45. render_empty
  46. end
  47. def reblog
  48. @status = ReblogService.new.call(current_user.account, Status.find(params[:id]))
  49. render action: :show
  50. end
  51. def unreblog
  52. reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!
  53. @status = reblog.reblog
  54. @reblogged_map = { @status.id => false }
  55. RemovalWorker.perform_async(reblog.id)
  56. render action: :show
  57. end
  58. def favourite
  59. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  60. render action: :show
  61. end
  62. def unfavourite
  63. @status = Status.find(params[:id])
  64. @favourited_map = { @status.id => false }
  65. UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
  66. render action: :show
  67. end
  68. private
  69. def set_status
  70. @status = Status.find(params[:id])
  71. raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account)
  72. end
  73. end