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.

120 lines
4.6 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 to_cn(n)
  31. case Time.new.wday
  32. when 0, 3
  33. "秦汉魏晋隋唐宋元明清"[n % 10] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  34. when 1, 4, 6
  35. "甲乙丙丁戊己庚辛壬癸"[n % 10] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  36. else
  37. "鼠牛虎兔龙蛇马羊猴鸡狗猪" [n % 12] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  38. end
  39. end
  40. def create
  41. p Rails.configuration.x.anon_tag
  42. anon = Rails.configuration.x.anon_acc && status_params[:status].end_with?(Rails.configuration.x.anon_tag)
  43. sender = anon ? Account.find(Rails.configuration.x.anon_acc) : current_user.account
  44. st_text = anon ? ("$#{to_cn(7919**(current_account.id + 1000 * Time.new.day) % 1000000007)}:\n" + status_params[:status][0..4990]) : status_params[:status]
  45. @status = PostStatusService.new.call(sender,
  46. text: st_text,
  47. thread: @thread,
  48. media_ids: status_params[:media_ids],
  49. sensitive: status_params[:sensitive],
  50. spoiler_text: status_params[:spoiler_text],
  51. visibility: status_params[:visibility],
  52. scheduled_at: status_params[:scheduled_at],
  53. application: doorkeeper_token.application,
  54. poll: status_params[:poll],
  55. idempotency: request.headers['Idempotency-Key'],
  56. with_rate_limit: true)
  57. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  58. end
  59. def destroy
  60. @status = Status.where(account_id: current_user.account).find(params[:id])
  61. authorize @status, :destroy?
  62. @status.discard
  63. RemovalWorker.perform_async(@status.id, redraft: true)
  64. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  65. end
  66. private
  67. def set_status
  68. @status = Status.find(params[:id])
  69. authorize @status, :show?
  70. rescue Mastodon::NotPermittedError
  71. not_found
  72. end
  73. def set_thread
  74. @thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  75. rescue ActiveRecord::RecordNotFound
  76. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  77. end
  78. def status_params
  79. params.permit(
  80. :status,
  81. :in_reply_to_id,
  82. :sensitive,
  83. :spoiler_text,
  84. :visibility,
  85. :scheduled_at,
  86. media_ids: [],
  87. poll: [
  88. :multiple,
  89. :hide_totals,
  90. :expires_in,
  91. options: [],
  92. ]
  93. )
  94. end
  95. def pagination_params(core_params)
  96. params.slice(:limit).permit(:limit).merge(core_params)
  97. end
  98. end