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.

132 lines
4.8 KiB

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