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.

77 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::ScheduledStatusesController < Api::BaseController
  3. include Authorization
  4. before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, except: [:update, :destroy]
  5. before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:update, :destroy]
  6. before_action :set_statuses, only: :index
  7. before_action :set_status, except: :index
  8. after_action :insert_pagination_headers, only: :index
  9. def index
  10. render json: @statuses, each_serializer: REST::ScheduledStatusSerializer
  11. end
  12. def show
  13. render json: @status, serializer: REST::ScheduledStatusSerializer
  14. end
  15. def update
  16. @status.update!(scheduled_status_params)
  17. render json: @status, serializer: REST::ScheduledStatusSerializer
  18. end
  19. def destroy
  20. @status.destroy!
  21. render_empty
  22. end
  23. private
  24. def set_statuses
  25. @statuses = current_account.scheduled_statuses.to_a_paginated_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
  26. end
  27. def set_status
  28. @status = current_account.scheduled_statuses.find(params[:id])
  29. end
  30. def scheduled_status_params
  31. params.permit(:scheduled_at)
  32. end
  33. def pagination_params(core_params)
  34. params.slice(:limit).permit(:limit).merge(core_params)
  35. end
  36. def insert_pagination_headers
  37. set_pagination_headers(next_path, prev_path)
  38. end
  39. def next_path
  40. if records_continue?
  41. api_v1_scheduled_statuses_url pagination_params(max_id: pagination_max_id)
  42. end
  43. end
  44. def prev_path
  45. unless @statuses.empty?
  46. api_v1_scheduled_statuses_url pagination_params(min_id: pagination_since_id)
  47. end
  48. end
  49. def records_continue?
  50. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  51. end
  52. def pagination_max_id
  53. @statuses.last.id
  54. end
  55. def pagination_since_id
  56. @statuses.first.id
  57. end
  58. end