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.

96 lines
2.3 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. render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
  11. end
  12. def show
  13. @notification = current_account.notifications.find(params[:id])
  14. render json: @notification, serializer: REST::NotificationSerializer
  15. end
  16. def clear
  17. current_account.notifications.delete_all
  18. render_empty
  19. end
  20. def destroy
  21. dismiss
  22. end
  23. def dismiss
  24. current_account.notifications.find_by!(id: params[:id]).destroy!
  25. render_empty
  26. end
  27. def destroy_multiple
  28. current_account.notifications.where(id: params[:ids]).destroy_all
  29. render_empty
  30. end
  31. private
  32. def load_notifications
  33. cache_collection paginated_notifications, Notification
  34. end
  35. def paginated_notifications
  36. browserable_account_notifications.paginate_by_max_id(
  37. limit_param(DEFAULT_NOTIFICATIONS_LIMIT),
  38. params[:max_id],
  39. params[:since_id]
  40. )
  41. end
  42. def browserable_account_notifications
  43. current_account.notifications.browserable(exclude_types)
  44. end
  45. def target_statuses_from_notifications
  46. @notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
  47. end
  48. def insert_pagination_headers
  49. set_pagination_headers(next_path, prev_path)
  50. end
  51. def next_path
  52. unless @notifications.empty?
  53. api_v1_notifications_url pagination_params(max_id: pagination_max_id)
  54. end
  55. end
  56. def prev_path
  57. unless @notifications.empty?
  58. api_v1_notifications_url pagination_params(since_id: pagination_since_id)
  59. end
  60. end
  61. def pagination_max_id
  62. @notifications.last.id
  63. end
  64. def pagination_since_id
  65. @notifications.first.id
  66. end
  67. def exclude_types
  68. val = params.permit(exclude_types: [])[:exclude_types] || []
  69. val = [val] unless val.is_a?(Enumerable)
  70. val
  71. end
  72. def pagination_params(core_params)
  73. params.permit(:limit, exclude_types: []).merge(core_params)
  74. end
  75. end