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

124 lines
4.5 KiB

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