2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class NotificationsController < ApplicationController
|
2018-02-01 12:17:59 +08:00
|
|
|
requires_login
|
2018-02-13 15:58:13 +08:00
|
|
|
before_action :ensure_admin, only: %i[create update destroy]
|
2018-02-13 14:38:26 +08:00
|
|
|
before_action :set_notification, only: %i[update destroy]
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2023-11-17 22:22:18 +08:00
|
|
|
INDEX_LIMIT = 60
|
2023-07-28 19:53:46 +08:00
|
|
|
|
2015-05-06 01:44:19 +08:00
|
|
|
def index
|
2017-04-07 17:32:13 +08:00
|
|
|
user =
|
|
|
|
if params[:username] && !params[:recent]
|
|
|
|
user_record = User.find_by(username: params[:username].to_s)
|
2017-04-25 09:56:26 +08:00
|
|
|
raise Discourse::NotFound if !user_record
|
2017-04-07 17:32:13 +08:00
|
|
|
user_record
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
2016-09-16 10:02:19 +08:00
|
|
|
|
|
|
|
guardian.ensure_can_see_notifications!(user)
|
2015-09-03 02:29:53 +08:00
|
|
|
|
2022-07-25 20:19:53 +08:00
|
|
|
if notification_types = params[:filter_by_types]&.split(",").presence
|
|
|
|
notification_types.map! do |type|
|
|
|
|
Notification.types[type.to_sym] ||
|
|
|
|
(raise Discourse::InvalidParameters.new("invalid notification type: #{type}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 10:02:19 +08:00
|
|
|
if params[:recent].present?
|
2023-07-28 19:53:46 +08:00
|
|
|
limit = fetch_limit_from_params(default: 15, max: INDEX_LIMIT)
|
2015-09-03 02:29:53 +08:00
|
|
|
|
2022-10-05 17:30:02 +08:00
|
|
|
include_reviewables = false
|
2022-12-08 09:44:29 +08:00
|
|
|
|
2023-05-18 00:16:42 +08:00
|
|
|
notifications =
|
|
|
|
Notification.prioritized_list(current_user, count: limit, types: notification_types)
|
|
|
|
# notification_types is blank for the "all notifications" user menu tab
|
|
|
|
include_reviewables = notification_types.blank? && guardian.can_see_review_queue?
|
2020-06-25 10:14:07 +08:00
|
|
|
|
2022-09-13 02:19:25 +08:00
|
|
|
if notifications.present? && !(params.has_key?(:silent) || @readonly_mode)
|
2022-11-28 08:41:57 +08:00
|
|
|
if current_user.bump_last_seen_notification!
|
2022-09-13 02:19:25 +08:00
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
end
|
2022-07-25 20:19:53 +08:00
|
|
|
end
|
2015-05-06 01:44:19 +08:00
|
|
|
|
2022-10-05 17:30:02 +08:00
|
|
|
if !params.has_key?(:silent) && params[:bump_last_seen_reviewable] && !@readonly_mode &&
|
|
|
|
include_reviewables
|
2022-08-03 13:57:59 +08:00
|
|
|
current_user_id = current_user.id
|
|
|
|
Scheduler::Defer.later "bump last seen reviewable for user" do
|
|
|
|
# we lookup current_user again in the background thread to avoid
|
2022-10-05 17:30:02 +08:00
|
|
|
# concurrency issues where the user object returned by the
|
|
|
|
# current_user controller method is changed by the time the deferred
|
|
|
|
# block is executed
|
|
|
|
User.find_by(id: current_user_id)&.bump_last_seen_reviewable!
|
2022-08-03 13:57:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-13 09:41:10 +08:00
|
|
|
notifications =
|
|
|
|
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
|
2023-12-08 01:30:44 +08:00
|
|
|
notifications =
|
|
|
|
Notification.populate_acting_user(notifications) if SiteSetting.show_user_menu_avatars
|
2022-11-28 08:41:57 +08:00
|
|
|
|
2022-10-05 17:30:02 +08:00
|
|
|
json = {
|
2022-07-25 20:19:53 +08:00
|
|
|
notifications: serialize_data(notifications, NotificationSerializer),
|
|
|
|
seen_notification_id: current_user.seen_notification_id,
|
2022-10-05 17:30:02 +08:00
|
|
|
}
|
2022-12-08 09:44:29 +08:00
|
|
|
|
2022-10-05 17:30:02 +08:00
|
|
|
if include_reviewables
|
|
|
|
json[:pending_reviewables] = Reviewable.basic_serializers_for_list(
|
|
|
|
Reviewable.user_menu_list_for(current_user),
|
|
|
|
current_user,
|
|
|
|
).as_json
|
|
|
|
end
|
2022-12-08 09:44:29 +08:00
|
|
|
|
2022-10-05 17:30:02 +08:00
|
|
|
render_json_dump(json)
|
2015-05-06 01:44:19 +08:00
|
|
|
else
|
2023-11-17 22:22:18 +08:00
|
|
|
limit = fetch_limit_from_params(default: INDEX_LIMIT, max: INDEX_LIMIT)
|
2015-05-06 01:44:19 +08:00
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
|
|
|
notifications =
|
|
|
|
Notification.where(user_id: user.id).visible.includes(:topic).order(created_at: :desc)
|
|
|
|
|
2020-07-02 18:06:00 +08:00
|
|
|
notifications = notifications.where(read: true) if params[:filter] == "read"
|
|
|
|
|
|
|
|
notifications = notifications.where(read: false) if params[:filter] == "unread"
|
|
|
|
|
2015-05-06 01:44:19 +08:00
|
|
|
total_rows = notifications.dup.count
|
2023-11-17 22:22:18 +08:00
|
|
|
notifications = notifications.offset(offset).limit(limit)
|
2023-10-13 09:41:10 +08:00
|
|
|
notifications =
|
|
|
|
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
|
2023-12-08 01:30:44 +08:00
|
|
|
notifications =
|
|
|
|
Notification.populate_acting_user(notifications) if SiteSetting.show_user_menu_avatars
|
2015-05-06 01:44:19 +08:00
|
|
|
render_json_dump(
|
|
|
|
notifications: serialize_data(notifications, NotificationSerializer),
|
|
|
|
total_rows_notifications: total_rows,
|
2016-09-16 10:02:19 +08:00
|
|
|
seen_notification_id: user.seen_notification_id,
|
2020-07-02 18:06:00 +08:00
|
|
|
load_more_notifications:
|
2023-11-17 22:22:18 +08:00
|
|
|
notifications_path(
|
|
|
|
username: user.username,
|
|
|
|
offset: offset + limit,
|
|
|
|
limit: limit,
|
|
|
|
filter: params[:filter],
|
|
|
|
),
|
2020-07-02 18:06:00 +08:00
|
|
|
)
|
2014-09-03 09:32:27 +08:00
|
|
|
end
|
|
|
|
end
|
2014-10-13 18:26:30 +08:00
|
|
|
|
2015-05-06 01:44:19 +08:00
|
|
|
def mark_read
|
2016-09-16 14:14:00 +08:00
|
|
|
if params[:id]
|
|
|
|
Notification.read(current_user, [params[:id].to_i])
|
|
|
|
else
|
2022-08-03 20:32:35 +08:00
|
|
|
if types = params[:dismiss_types]&.split(",").presence
|
|
|
|
invalid = []
|
|
|
|
types.map! do |type|
|
|
|
|
type_id = Notification.types[type.to_sym]
|
|
|
|
invalid << type if !type_id
|
|
|
|
type_id
|
|
|
|
end
|
|
|
|
if invalid.size > 0
|
|
|
|
raise Discourse::InvalidParameters.new("invalid notification types: #{invalid.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Notification.read_types(current_user, types)
|
2022-09-13 02:19:25 +08:00
|
|
|
current_user.bump_last_seen_notification!
|
2016-09-16 14:14:00 +08:00
|
|
|
end
|
2014-10-13 18:26:30 +08:00
|
|
|
|
2018-05-26 09:11:10 +08:00
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
|
2015-05-06 01:44:19 +08:00
|
|
|
render json: success_json
|
2014-10-13 18:26:30 +08:00
|
|
|
end
|
2015-05-06 01:44:19 +08:00
|
|
|
|
2018-02-13 14:38:26 +08:00
|
|
|
def create
|
REFACTOR: Improve support for consolidating notifications. (#14904)
* REFACTOR: Improve support for consolidating notifications.
Before this commit, we didn't have a single way of consolidating notifications. For notifications like group summaries, we manually removed old ones before creating a new one. On the other hand, we used an after_create callback for likes and group membership requests, which caused unnecessary work, as we need to delete the record we created to replace it with a consolidated one.
We now have all the consolidation rules centralized in a single place: the consolidation planner class. Other parts of the app looking to create a consolidable notification can do so by calling Notification#consolidate_or_save!, instead of the default Notification#create! method.
Finally, we added two more rules: one for re-using existing group summaries and another for deleting duplicated dashboard problems PMs notifications when the user is tracking the moderator's inbox. Setting the threshold to one forces the planner to apply this rule every time.
I plan to add plugin support for adding custom rules in another PR to keep this one relatively small.
* DEV: Introduces a plugin API for consolidating notifications.
This commit removes the `Notification#filter_by_consolidation_data` scope since plugins could have to define their criteria. The Plan class now receives two blocks, one to query for an already consolidated notification, which we'll try to update, and another to query for existing ones to consolidate.
It also receives a consolidation window, which accepts an ActiveSupport::Duration object, and filter notifications created since that value.
2021-12-01 00:36:14 +08:00
|
|
|
@notification = Notification.consolidate_or_create!(notification_params)
|
2018-02-13 14:38:26 +08:00
|
|
|
render_notification
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification.update!(notification_params)
|
|
|
|
render_notification
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@notification.destroy!
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2024-03-16 00:08:37 +08:00
|
|
|
def totals
|
|
|
|
render_serialized(current_user, UserNotificationTotalSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2018-02-13 14:38:26 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_notification
|
|
|
|
@notification = Notification.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_params
|
|
|
|
params.permit(
|
|
|
|
:notification_type,
|
|
|
|
:user_id,
|
|
|
|
:data,
|
|
|
|
:read,
|
|
|
|
:topic_id,
|
|
|
|
:post_number,
|
|
|
|
:post_action_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_notification
|
|
|
|
render_json_dump(NotificationSerializer.new(@notification, scope: guardian, root: false))
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|