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

59 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Crypto::EncryptedMessagesController < Api::BaseController
  3. LIMIT = 80
  4. before_action -> { doorkeeper_authorize! :crypto }
  5. before_action :require_user!
  6. before_action :set_current_device
  7. before_action :set_encrypted_messages, only: :index
  8. after_action :insert_pagination_headers, only: :index
  9. def index
  10. render json: @encrypted_messages, each_serializer: REST::EncryptedMessageSerializer
  11. end
  12. def clear
  13. @current_device.encrypted_messages.up_to(params[:up_to_id]).delete_all
  14. render_empty
  15. end
  16. private
  17. def set_current_device
  18. @current_device = Device.find_by!(access_token: doorkeeper_token)
  19. end
  20. def set_encrypted_messages
  21. @encrypted_messages = @current_device.encrypted_messages.paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  22. end
  23. def insert_pagination_headers
  24. set_pagination_headers(next_path, prev_path)
  25. end
  26. def next_path
  27. api_v1_encrypted_messages_url pagination_params(max_id: pagination_max_id) if records_continue?
  28. end
  29. def prev_path
  30. api_v1_encrypted_messages_url pagination_params(min_id: pagination_since_id) unless @encrypted_messages.empty?
  31. end
  32. def pagination_max_id
  33. @encrypted_messages.last.id
  34. end
  35. def pagination_since_id
  36. @encrypted_messages.first.id
  37. end
  38. def records_continue?
  39. @encrypted_messages.size == limit_param(LIMIT)
  40. end
  41. def pagination_params(core_params)
  42. params.slice(:limit).permit(:limit).merge(core_params)
  43. end
  44. end