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 = 40
  8. def index
  9. with_read_replica do
  10. @notifications = load_notifications
  11. @relationships = StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
  12. end
  13. render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
  14. end
  15. def show
  16. @notification = current_account.notifications.without_suspended.find(params[:id])
  17. render json: @notification, serializer: REST::NotificationSerializer
  18. end
  19. def clear
  20. current_account.notifications.delete_all
  21. render_empty
  22. end
  23. def dismiss
  24. current_account.notifications.find(params[:id]).destroy!
  25. render_empty
  26. end
  27. private
  28. def load_notifications
  29. notifications = browserable_account_notifications.includes(from_account: [:account_stat, :user]).to_a_paginated_by_id(
  30. limit_param(DEFAULT_NOTIFICATIONS_LIMIT),
  31. params_slice(:max_id, :since_id, :min_id)
  32. )
  33. Notification.preload_cache_collection_target_statuses(notifications) do |target_statuses|
  34. cache_collection(target_statuses, Status)
  35. end
  36. end
  37. def browserable_account_notifications
  38. current_account.notifications.without_suspended.browserable(
  39. types: Array(browserable_params[:types]),
  40. exclude_types: Array(browserable_params[:exclude_types]),
  41. from_account_id: browserable_params[:account_id]
  42. )
  43. end
  44. def target_statuses_from_notifications
  45. @notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
  46. end
  47. def insert_pagination_headers
  48. set_pagination_headers(next_path, prev_path)
  49. end
  50. def next_path
  51. api_v1_notifications_url pagination_params(max_id: pagination_max_id) unless @notifications.empty?
  52. end
  53. def prev_path
  54. api_v1_notifications_url pagination_params(min_id: pagination_since_id) unless @notifications.empty?
  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 browserable_params
  63. params.permit(:account_id, types: [], exclude_types: [])
  64. end
  65. def pagination_params(core_params)
  66. params.slice(:limit, :account_id, :types, :exclude_types).permit(:limit, :account_id, types: [], exclude_types: []).merge(core_params)
  67. end
  68. end