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: [:create, :update, :destroy]
|
2018-02-13 14:38:26 +08:00
|
|
|
before_action :set_notification, only: [:update, :destroy]
|
2013-02-06 03:16:51 +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
|
|
|
|
2016-09-16 10:02:19 +08:00
|
|
|
if params[:recent].present?
|
2015-09-03 03:48:41 +08:00
|
|
|
limit = (params[:limit] || 15).to_i
|
2015-09-03 02:29:53 +08:00
|
|
|
limit = 50 if limit > 50
|
|
|
|
|
|
|
|
notifications = Notification.recent_report(current_user, limit)
|
2016-09-16 10:02:19 +08:00
|
|
|
changed = false
|
2015-05-06 01:44:19 +08:00
|
|
|
|
2020-06-25 10:14:07 +08:00
|
|
|
if notifications.present? && !(params.has_key?(:slient) || @readonly_mode)
|
2015-05-06 01:44:19 +08:00
|
|
|
# ordering can be off due to PMs
|
|
|
|
max_id = notifications.map(&:id).max
|
2020-06-25 10:14:07 +08:00
|
|
|
changed = current_user.saw_notification_id(max_id)
|
2015-05-06 01:44:19 +08:00
|
|
|
end
|
2020-06-25 10:14:07 +08:00
|
|
|
|
2016-09-16 10:02:19 +08:00
|
|
|
user.reload
|
|
|
|
user.publish_notifications_state if changed
|
2015-05-06 01:44:19 +08:00
|
|
|
|
2016-09-16 10:02:19 +08:00
|
|
|
render_json_dump(notifications: serialize_data(notifications, NotificationSerializer),
|
|
|
|
seen_notification_id: current_user.seen_notification_id)
|
2015-05-06 01:44:19 +08:00
|
|
|
else
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
|
|
|
notifications = Notification.where(user_id: user.id)
|
2017-07-28 09:20:09 +08:00
|
|
|
.visible
|
|
|
|
.includes(:topic)
|
|
|
|
.order(created_at: :desc)
|
2015-05-06 01:44:19 +08:00
|
|
|
|
|
|
|
total_rows = notifications.dup.count
|
|
|
|
notifications = notifications.offset(offset).limit(60)
|
|
|
|
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,
|
2015-05-06 01:44:19 +08:00
|
|
|
load_more_notifications: notifications_path(username: user.username, offset: offset + 60))
|
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
|
|
|
|
Notification.where(user_id: current_user.id).includes(:topic).where(read: false).update_all(read: true)
|
|
|
|
current_user.saw_notification_id(Notification.recent_report(current_user, 1).max.try(:id))
|
|
|
|
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
|
|
|
|
@notification = Notification.create!(notification_params)
|
|
|
|
render_notification
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification.update!(notification_params)
|
|
|
|
render_notification
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@notification.destroy!
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def set_notification
|
|
|
|
@notification = Notification.find(params[:id])
|
|
|
|
end
|
2018-02-13 14:38:26 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def notification_params
|
|
|
|
params.permit(:notification_type, :user_id, :data, :read, :topic_id, :post_number, :post_action_id)
|
|
|
|
end
|
2018-02-13 14:38:26 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def render_notification
|
|
|
|
render_json_dump(NotificationSerializer.new(@notification, scope: guardian, root: false))
|
|
|
|
end
|
2018-02-13 14:38:26 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|