2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseController
|
2021-01-24 19:29:44 +00:00
|
|
|
RESULTS_PER_PAGE = 15
|
2020-05-01 09:23:43 +00:00
|
|
|
|
|
|
|
before_action :fetch_notification, only: [:update]
|
2020-06-15 08:06:56 +00:00
|
|
|
before_action :set_primary_actor, only: [:read_all]
|
2021-01-13 11:34:27 +00:00
|
|
|
before_action :set_current_page, only: [:index]
|
2020-05-01 09:23:43 +00:00
|
|
|
|
|
|
|
def index
|
2020-06-09 12:12:18 +00:00
|
|
|
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
|
2021-01-13 11:34:27 +00:00
|
|
|
@count = notifications.count
|
2021-01-24 19:29:44 +00:00
|
|
|
@notifications = notifications.page(@current_page).per(RESULTS_PER_PAGE)
|
2020-06-09 12:12:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_all
|
2021-01-24 19:29:44 +00:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2020-06-15 08:06:56 +00:00
|
|
|
if @primary_actor
|
|
|
|
current_user.notifications.where(account_id: current_account.id, primary_actor: @primary_actor, read_at: nil)
|
2021-01-24 19:29:44 +00:00
|
|
|
.update_all(read_at: DateTime.now.utc)
|
2020-06-15 08:06:56 +00:00
|
|
|
else
|
2021-01-24 19:29:44 +00:00
|
|
|
current_user.notifications.where(account_id: current_account.id, read_at: nil).update_all(read_at: DateTime.now.utc)
|
2020-06-15 08:06:56 +00:00
|
|
|
end
|
2021-01-24 19:29:44 +00:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2020-06-09 12:12:18 +00:00
|
|
|
head :ok
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification.update(read_at: DateTime.now.utc)
|
|
|
|
render json: @notification
|
|
|
|
end
|
|
|
|
|
2021-01-17 17:15:31 +00:00
|
|
|
def unread_count
|
|
|
|
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
|
|
|
|
render json: @unread_count
|
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
private
|
|
|
|
|
2020-06-15 08:06:56 +00:00
|
|
|
def set_primary_actor
|
|
|
|
return unless params[:primary_actor_type]
|
|
|
|
return unless Notification::PRIMARY_ACTORS.include?(params[:primary_actor_type])
|
|
|
|
|
|
|
|
@primary_actor = params[:primary_actor_type].safe_constantize.find_by(id: params[:primary_actor_id])
|
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
def fetch_notification
|
|
|
|
@notification = current_user.notifications.find(params[:id])
|
|
|
|
end
|
2021-01-13 11:34:27 +00:00
|
|
|
|
|
|
|
def set_current_page
|
|
|
|
@current_page = params[:page] || 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def notifications
|
|
|
|
@notifications ||= current_user.notifications.where(account_id: current_account.id)
|
|
|
|
end
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|