闭社主体 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.

115 lines
4.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::StatusesController < Api::BaseController
  3. include Authorization
  4. before_action -> { authorize_if_got_token! :read, :'read:statuses' }, except: [:create, :destroy]
  5. before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:create, :destroy]
  6. before_action :require_user!, except: [:show, :context]
  7. before_action :set_status, only: [:show, :context]
  8. before_action :set_thread, only: [:create]
  9. override_rate_limit_headers :create, family: :statuses
  10. # This API was originally unlimited, pagination cannot be introduced without
  11. # breaking backwards-compatibility. Arbitrarily high number to cover most
  12. # conversations as quasi-unlimited, it would be too much work to render more
  13. # than this anyway
  14. CONTEXT_LIMIT = 4_096
  15. def show
  16. @status = cache_collection([@status], Status).first
  17. render json: @status, serializer: REST::StatusSerializer
  18. end
  19. def context
  20. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(CONTEXT_LIMIT, current_account)
  21. treeId = Rails.configuration.x.tree_address.split('/')[-1].to_i
  22. depth = (@status.id == treeId || (!ancestors_results.empty? && ancestors_results[0].id == treeId)) ? 1 : nil
  23. descendants_results = @status.descendants(CONTEXT_LIMIT, current_account, nil, nil, depth)
  24. loaded_ancestors = cache_collection(ancestors_results, Status)
  25. loaded_descendants = cache_collection(descendants_results, Status)
  26. @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  27. statuses = [@status] + @context.ancestors + @context.descendants
  28. render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
  29. end
  30. def create
  31. anon = Rails.configuration.x.anon
  32. anon_name = anon.acc && status_params[:status].end_with?(anon.tag) && generate_anon_name(current_user.account.username + anon.salt + (Time.now - 5.hours).strftime("%D"), anon.namelist, Account.find(anon.acc).note)
  33. sender = anon_name ? Account.find(anon.acc) : current_user.account
  34. st_text = anon_name ? ("[#{anon_name}]:\n#{status_params[:status]}"[0..5000]) : status_params[:status]
  35. @status = PostStatusService.new.call(sender,
  36. text: st_text,
  37. thread: @thread,
  38. media_ids: status_params[:media_ids],
  39. sensitive: status_params[:sensitive],
  40. spoiler_text: status_params[:spoiler_text],
  41. visibility: status_params[:visibility],
  42. scheduled_at: status_params[:scheduled_at],
  43. application: doorkeeper_token.application,
  44. poll: status_params[:poll],
  45. idempotency: request.headers['Idempotency-Key'],
  46. with_rate_limit: true)
  47. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  48. end
  49. def destroy
  50. @status = Status.where(account_id: current_user.account).find(params[:id])
  51. authorize @status, :destroy?
  52. @status.discard
  53. RemovalWorker.perform_async(@status.id, redraft: true)
  54. @status.account.statuses_count = @status.account.statuses_count - 1
  55. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  56. end
  57. private
  58. def set_status
  59. @status = Status.find(params[:id])
  60. authorize @status, :show?
  61. rescue Mastodon::NotPermittedError
  62. not_found
  63. end
  64. def set_thread
  65. @thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  66. rescue ActiveRecord::RecordNotFound
  67. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  68. end
  69. def status_params
  70. params.permit(
  71. :status,
  72. :in_reply_to_id,
  73. :sensitive,
  74. :spoiler_text,
  75. :visibility,
  76. :scheduled_at,
  77. media_ids: [],
  78. poll: [
  79. :multiple,
  80. :hide_totals,
  81. :expires_in,
  82. options: [],
  83. ]
  84. )
  85. end
  86. def pagination_params(core_params)
  87. params.slice(:limit).permit(:limit).merge(core_params)
  88. end
  89. def generate_anon_name(k, namelist, note)
  90. name = namelist[Digest::SHA2.hexdigest(k).to_i(16) % namelist.size]
  91. name.in?(note) ? nil : name
  92. end
  93. end