闭社主体 forked from https://github.com/tootsuite/mastodon
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.

82 lines
3.0 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. end
  10. def context
  11. @context = OpenStruct.new(ancestors: @status.ancestors(current_account), descendants: @status.descendants(current_account))
  12. set_maps([@status] + @context[:ancestors] + @context[:descendants])
  13. end
  14. def reblogged_by
  15. results = @status.reblogs.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  16. accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h
  17. @accounts = results.map { |r| accounts[r.account_id] }
  18. next_path = reblogged_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  19. prev_path = reblogged_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  20. set_pagination_headers(next_path, prev_path)
  21. render action: :accounts
  22. end
  23. def favourited_by
  24. results = @status.favourites.paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id])
  25. accounts = Account.where(id: results.map(&:account_id)).with_counters.map { |a| [a.id, a] }.to_h
  26. @accounts = results.map { |f| accounts[f.account_id] }
  27. next_path = favourited_by_api_v1_status_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT
  28. prev_path = favourited_by_api_v1_status_url(since_id: results.first.id) unless results.empty?
  29. set_pagination_headers(next_path, prev_path)
  30. render action: :accounts
  31. end
  32. def create
  33. @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])
  34. render action: :show
  35. end
  36. def destroy
  37. @status = Status.where(account_id: current_user.account).find(params[:id])
  38. RemoveStatusService.new.call(@status)
  39. render_empty
  40. end
  41. def reblog
  42. @status = ReblogService.new.call(current_user.account, Status.find(params[:id])).reload
  43. render action: :show
  44. end
  45. def unreblog
  46. RemoveStatusService.new.call(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!)
  47. @status = Status.find(params[:id])
  48. render action: :show
  49. end
  50. def favourite
  51. @status = FavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  52. render action: :show
  53. end
  54. def unfavourite
  55. @status = UnfavouriteService.new.call(current_user.account, Status.find(params[:id])).status.reload
  56. render action: :show
  57. end
  58. private
  59. def set_status
  60. @status = Status.find(params[:id])
  61. end
  62. end