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.

126 lines
4.8 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 = ENV['TREE_ADDRESS'].split('/')[-1].to_i
  22. depth = @status.id == treeId ? 1 : ((!ancestors_results.empty? && ancestors_results[0].id == treeId) ? 2 : 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. <<<<<<< HEAD
  42. thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  43. masked = status_params[:status].end_with?('[mask]')
  44. sender = masked ? Account.find_local('mask_bot') : current_account
  45. st_text = masked ? ("$#{to_cn(7919**(current_account.id + 1000 * Time.new.day) % 1000000007)}:\n" + status_params[:status][0..4900]) : status_params[:status]
  46. @status = PostStatusService.new.call(sender,
  47. text: st_text,
  48. thread: thread,
  49. =======
  50. @status = PostStatusService.new.call(current_user.account,
  51. text: status_params[:status],
  52. thread: @thread,
  53. >>>>>>> master
  54. media_ids: status_params[:media_ids],
  55. sensitive: status_params[:sensitive],
  56. spoiler_text: status_params[:spoiler_text],
  57. visibility: status_params[:visibility],
  58. scheduled_at: status_params[:scheduled_at],
  59. application: doorkeeper_token.application,
  60. poll: status_params[:poll],
  61. idempotency: request.headers['Idempotency-Key'],
  62. with_rate_limit: true)
  63. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  64. end
  65. def destroy
  66. @status = Status.where(account_id: current_user.account).find(params[:id])
  67. authorize @status, :destroy?
  68. @status.discard
  69. RemovalWorker.perform_async(@status.id, redraft: true)
  70. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  71. end
  72. private
  73. def set_status
  74. @status = Status.find(params[:id])
  75. authorize @status, :show?
  76. rescue Mastodon::NotPermittedError
  77. not_found
  78. end
  79. def set_thread
  80. @thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  81. rescue ActiveRecord::RecordNotFound
  82. render json: { error: I18n.t('statuses.errors.in_reply_not_found') }, status: 404
  83. end
  84. def status_params
  85. params.permit(
  86. :status,
  87. :in_reply_to_id,
  88. :sensitive,
  89. :spoiler_text,
  90. :visibility,
  91. :scheduled_at,
  92. media_ids: [],
  93. poll: [
  94. :multiple,
  95. :hide_totals,
  96. :expires_in,
  97. options: [],
  98. ]
  99. )
  100. end
  101. def pagination_params(core_params)
  102. params.slice(:limit).permit(:limit).merge(core_params)
  103. end
  104. end