1
0

notifications_controller.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. class Api::V1::NotificationsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, except: [:clear, :dismiss]
  4. before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, only: [:clear, :dismiss]
  5. before_action :require_user!
  6. after_action :insert_pagination_headers, only: :index
  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.without_suspended.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 dismiss
  21. current_account.notifications.find_by!(id: params[:id]).destroy!
  22. render_empty
  23. end
  24. private
  25. def load_notifications
  26. notifications = browserable_account_notifications.includes(from_account: :account_stat).to_a_paginated_by_id(
  27. limit_param(DEFAULT_NOTIFICATIONS_LIMIT),
  28. params_slice(:max_id, :since_id, :min_id)
  29. )
  30. Notification.preload_cache_collection_target_statuses(notifications) do |target_statuses|
  31. cache_collection(target_statuses, Status)
  32. end
  33. end
  34. def browserable_account_notifications
  35. current_account.notifications.without_suspended.browserable(
  36. types: Array(browserable_params[:types]),
  37. exclude_types: Array(browserable_params[:exclude_types]),
  38. from_account_id: browserable_params[:account_id]
  39. )
  40. end
  41. def target_statuses_from_notifications
  42. @notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
  43. end
  44. def insert_pagination_headers
  45. set_pagination_headers(next_path, prev_path)
  46. end
  47. def next_path
  48. unless @notifications.empty?
  49. api_v1_notifications_url pagination_params(max_id: pagination_max_id)
  50. end
  51. end
  52. def prev_path
  53. unless @notifications.empty?
  54. api_v1_notifications_url pagination_params(min_id: pagination_since_id)
  55. end
  56. end
  57. def pagination_max_id
  58. @notifications.last.id
  59. end
  60. def pagination_since_id
  61. @notifications.first.id
  62. end
  63. def browserable_params
  64. params.permit(:account_id, types: [], exclude_types: [])
  65. end
  66. def pagination_params(core_params)
  67. params.slice(:limit, :account_id, :types, :exclude_types).permit(:limit, :account_id, types: [], exclude_types: []).merge(core_params)
  68. end
  69. end