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.
 
 
 
 

72 lines
1.8 KiB

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