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.

73 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. api_v1_scheduled_statuses_url pagination_params(max_id: pagination_max_id) if records_continue?
  41. end
  42. def prev_path
  43. api_v1_scheduled_statuses_url pagination_params(min_id: pagination_since_id) unless @statuses.empty?
  44. end
  45. def records_continue?
  46. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  47. end
  48. def pagination_max_id
  49. @statuses.last.id
  50. end
  51. def pagination_since_id
  52. @statuses.first.id
  53. end
  54. end