2024-03-07 14:53:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Notifications::RequestsController < Api::BaseController
|
2024-08-19 15:59:06 +00:00
|
|
|
include Redisable
|
|
|
|
|
|
|
|
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, only: [:index, :show, :merged?]
|
|
|
|
before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, except: [:index, :show, :merged?]
|
2024-03-07 14:53:37 +00:00
|
|
|
|
|
|
|
before_action :require_user!
|
2024-08-01 12:36:54 +00:00
|
|
|
before_action :set_request, only: [:show, :accept, :dismiss]
|
|
|
|
before_action :set_requests, only: [:accept_bulk, :dismiss_bulk]
|
2024-03-07 14:53:37 +00:00
|
|
|
|
|
|
|
after_action :insert_pagination_headers, only: :index
|
|
|
|
|
|
|
|
def index
|
|
|
|
with_read_replica do
|
|
|
|
@requests = load_requests
|
|
|
|
@relationships = relationships
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: @requests, each_serializer: REST::NotificationRequestSerializer, relationships: @relationships
|
|
|
|
end
|
|
|
|
|
2024-08-19 15:59:06 +00:00
|
|
|
def merged?
|
|
|
|
render json: { merged: redis.get("notification_unfilter_jobs:#{current_account.id}").to_i <= 0 }
|
|
|
|
end
|
|
|
|
|
2024-03-11 15:02:21 +00:00
|
|
|
def show
|
|
|
|
render json: @request, serializer: REST::NotificationRequestSerializer
|
|
|
|
end
|
|
|
|
|
2024-03-07 14:53:37 +00:00
|
|
|
def accept
|
|
|
|
AcceptNotificationRequestService.new.call(@request)
|
|
|
|
render_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def dismiss
|
2024-08-07 15:14:37 +00:00
|
|
|
DismissNotificationRequestService.new.call(@request)
|
2024-03-07 14:53:37 +00:00
|
|
|
render_empty
|
|
|
|
end
|
|
|
|
|
2024-08-01 12:36:54 +00:00
|
|
|
def accept_bulk
|
|
|
|
@requests.each { |request| AcceptNotificationRequestService.new.call(request) }
|
|
|
|
render_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
def dismiss_bulk
|
|
|
|
@requests.each(&:destroy!)
|
|
|
|
render_empty
|
|
|
|
end
|
|
|
|
|
2024-03-07 14:53:37 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_requests
|
2024-07-12 12:09:52 +00:00
|
|
|
requests = NotificationRequest.where(account: current_account).includes(:last_status, from_account: [:account_stat, :user]).to_a_paginated_by_id(
|
2024-03-07 14:53:37 +00:00
|
|
|
limit_param(DEFAULT_ACCOUNTS_LIMIT),
|
|
|
|
params_slice(:max_id, :since_id, :min_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
NotificationRequest.preload_cache_collection(requests) do |statuses|
|
2024-05-16 08:03:46 +00:00
|
|
|
preload_collection(statuses, Status)
|
2024-03-07 14:53:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def relationships
|
|
|
|
StatusRelationshipsPresenter.new(@requests.map(&:last_status), current_user&.account_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_request
|
|
|
|
@request = NotificationRequest.where(account: current_account).find(params[:id])
|
|
|
|
end
|
|
|
|
|
2024-08-01 12:36:54 +00:00
|
|
|
def set_requests
|
|
|
|
@requests = NotificationRequest.where(account: current_account, id: Array(params[:id]).uniq.map(&:to_i))
|
|
|
|
end
|
|
|
|
|
2024-03-07 14:53:37 +00:00
|
|
|
def next_path
|
2024-08-22 13:42:04 +00:00
|
|
|
api_v1_notifications_requests_url pagination_params(max_id: pagination_max_id) if records_continue?
|
2024-03-07 14:53:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def prev_path
|
|
|
|
api_v1_notifications_requests_url pagination_params(min_id: pagination_since_id) unless @requests.empty?
|
|
|
|
end
|
|
|
|
|
2024-08-22 13:42:04 +00:00
|
|
|
def records_continue?
|
|
|
|
@requests.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
|
|
|
|
end
|
|
|
|
|
2024-03-07 14:53:37 +00:00
|
|
|
def pagination_max_id
|
|
|
|
@requests.last.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_since_id
|
|
|
|
@requests.first.id
|
|
|
|
end
|
|
|
|
end
|