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.

112 lines
4.3 KiB

4 years ago
  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. respond_to :json
  9. # This API was originally unlimited, pagination cannot be introduced without
  10. # breaking backwards-compatibility. Arbitrarily high number to cover most
  11. # conversations as quasi-unlimited, it would be too much work to render more
  12. # than this anyway
  13. CONTEXT_LIMIT = 4_096
  14. def show
  15. @status = cache_collection([@status], Status).first
  16. render json: @status, serializer: REST::StatusSerializer
  17. end
  18. def context
  19. ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(CONTEXT_LIMIT, current_account)
  20. treeId = ENV['TREE_ADDRESS'].split('/')[-1].to_i
  21. depth = @status.id == treeId ? 1 : ((!ancestors_results.empty? && ancestors_results[0].id == treeId) ? 2 : nil)
  22. descendants_results = @status.descendants(CONTEXT_LIMIT, current_account, nil, nil, depth)
  23. loaded_ancestors = cache_collection(ancestors_results, Status)
  24. loaded_descendants = cache_collection(descendants_results, Status)
  25. @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
  26. statuses = [@status] + @context.ancestors + @context.descendants
  27. render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id)
  28. end
  29. def to_cn(n)
  30. case Time.new.wday
  31. when 0, 3
  32. "秦汉魏晋隋唐宋元明清"[n % 10] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  33. when 1, 4, 6
  34. "甲乙丙丁戊己庚辛壬癸"[n % 10] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  35. else
  36. "鼠牛虎兔龙蛇马羊猴鸡狗猪" [n % 12] + [n % 20873, n % 20899].map{|i| i+0x4e00}.pack('U*')
  37. end
  38. end
  39. def create
  40. thread = status_params[:in_reply_to_id].blank? ? nil : Status.find(status_params[:in_reply_to_id])
  41. masked = status_params[:status].end_with?('[mask]')
  42. sender = masked ? Account.find_local('mask_bot') : current_account
  43. st_text = masked ? ("$#{to_cn(7919**(current_account.id + 1000 * Time.new.day) % 1000000007)}:\n" + status_params[:status][0..4900]) : status_params[:status]
  44. @status = PostStatusService.new.call(sender,
  45. text: st_text,
  46. thread: thread,
  47. media_ids: status_params[:media_ids],
  48. sensitive: status_params[:sensitive],
  49. spoiler_text: status_params[:spoiler_text],
  50. visibility: status_params[:visibility],
  51. scheduled_at: status_params[:scheduled_at],
  52. application: doorkeeper_token.application,
  53. poll: status_params[:poll],
  54. idempotency: request.headers['Idempotency-Key'])
  55. render json: @status, serializer: @status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer
  56. end
  57. def destroy
  58. @status = Status.where(account_id: current_user.account).find(params[:id])
  59. authorize @status, :destroy?
  60. @status.discard
  61. RemovalWorker.perform_async(@status.id, redraft: true)
  62. render json: @status, serializer: REST::StatusSerializer, source_requested: true
  63. end
  64. private
  65. def set_status
  66. @status = Status.find(params[:id])
  67. authorize @status, :show?
  68. rescue Mastodon::NotPermittedError
  69. raise ActiveRecord::RecordNotFound
  70. end
  71. def status_params
  72. params.permit(
  73. :status,
  74. :in_reply_to_id,
  75. :sensitive,
  76. :spoiler_text,
  77. :visibility,
  78. :scheduled_at,
  79. media_ids: [],
  80. poll: [
  81. :multiple,
  82. :hide_totals,
  83. :expires_in,
  84. options: [],
  85. ]
  86. )
  87. end
  88. def pagination_params(core_params)
  89. params.slice(:limit).permit(:limit).merge(core_params)
  90. end
  91. end