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.

90 lines
2.0 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::NotificationsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers, only: :index
  6. respond_to :json
  7. DEFAULT_NOTIFICATIONS_LIMIT = 15
  8. def index
  9. @notifications = load_notifications
  10. set_maps_for_notification_target_statuses
  11. end
  12. def show
  13. @notification = current_account.notifications.find(params[:id])
  14. end
  15. def clear
  16. current_account.notifications.delete_all
  17. render_empty
  18. end
  19. def dismiss
  20. current_account.notifications.find_by!(id: params[:id]).destroy!
  21. render_empty
  22. end
  23. private
  24. def load_notifications
  25. cache_collection paginated_notifications, Notification
  26. end
  27. def paginated_notifications
  28. browserable_account_notifications.paginate_by_max_id(
  29. limit_param(DEFAULT_NOTIFICATIONS_LIMIT),
  30. params[:max_id],
  31. params[:since_id]
  32. )
  33. end
  34. def browserable_account_notifications
  35. current_account.notifications.browserable(exclude_types)
  36. end
  37. def set_maps_for_notification_target_statuses
  38. set_maps target_statuses_from_notifications
  39. end
  40. def target_statuses_from_notifications
  41. @notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
  42. end
  43. def insert_pagination_headers
  44. set_pagination_headers(next_path, prev_path)
  45. end
  46. def next_path
  47. unless @notifications.empty?
  48. api_v1_notifications_url pagination_params(max_id: pagination_max_id)
  49. end
  50. end
  51. def prev_path
  52. unless @notifications.empty?
  53. api_v1_notifications_url pagination_params(since_id: pagination_since_id)
  54. end
  55. end
  56. def pagination_max_id
  57. @notifications.last.id
  58. end
  59. def pagination_since_id
  60. @notifications.first.id
  61. end
  62. def exclude_types
  63. val = params.permit(exclude_types: [])[:exclude_types] || []
  64. val = [val] unless val.is_a?(Enumerable)
  65. val
  66. end
  67. def pagination_params(core_params)
  68. params.permit(:limit, exclude_types: []).merge(core_params)
  69. end
  70. end