The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

56 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::ConversationsController < Api::BaseController
  3. LIMIT = 20
  4. before_action -> { doorkeeper_authorize! :read, :'read:statuses' }
  5. before_action :require_user!
  6. after_action :insert_pagination_headers
  7. respond_to :json
  8. def index
  9. @conversations = paginated_conversations
  10. render json: @conversations, each_serializer: REST::ConversationSerializer
  11. end
  12. private
  13. def paginated_conversations
  14. AccountConversation.where(account: current_account)
  15. .paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  16. end
  17. def insert_pagination_headers
  18. set_pagination_headers(next_path, prev_path)
  19. end
  20. def next_path
  21. if records_continue?
  22. api_v1_conversations_url pagination_params(max_id: pagination_max_id)
  23. end
  24. end
  25. def prev_path
  26. unless @conversations.empty?
  27. api_v1_conversations_url pagination_params(min_id: pagination_since_id)
  28. end
  29. end
  30. def pagination_max_id
  31. @conversations.last.last_status_id
  32. end
  33. def pagination_since_id
  34. @conversations.first.last_status_id
  35. end
  36. def records_continue?
  37. @conversations.size == limit_param(LIMIT)
  38. end
  39. def pagination_params(core_params)
  40. params.slice(:limit).permit(:limit).merge(core_params)
  41. end
  42. end