2015-05-05 13:44:19 -04:00
|
|
|
require_dependency 'notification_serializer'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class NotificationsController < ApplicationController
|
|
|
|
|
|
|
|
before_filter :ensure_logged_in
|
|
|
|
|
2015-05-05 13:44:19 -04:00
|
|
|
def index
|
2017-04-07 05:32:13 -04:00
|
|
|
user =
|
|
|
|
if params[:username] && !params[:recent]
|
|
|
|
user_record = User.find_by(username: params[:username].to_s)
|
2017-04-24 21:56:26 -04:00
|
|
|
raise Discourse::NotFound if !user_record
|
2017-04-07 05:32:13 -04:00
|
|
|
user_record
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
2016-09-15 22:02:19 -04:00
|
|
|
|
|
|
|
guardian.ensure_can_see_notifications!(user)
|
2015-09-02 14:29:53 -04:00
|
|
|
|
2016-09-15 22:02:19 -04:00
|
|
|
if params[:recent].present?
|
2015-09-02 15:48:41 -04:00
|
|
|
limit = (params[:limit] || 15).to_i
|
2015-09-02 14:29:53 -04:00
|
|
|
limit = 50 if limit > 50
|
|
|
|
|
|
|
|
notifications = Notification.recent_report(current_user, limit)
|
2016-09-15 22:02:19 -04:00
|
|
|
changed = false
|
2015-05-05 13:44:19 -04:00
|
|
|
|
|
|
|
if notifications.present?
|
|
|
|
# ordering can be off due to PMs
|
|
|
|
max_id = notifications.map(&:id).max
|
2016-09-15 22:02:19 -04:00
|
|
|
changed = current_user.saw_notification_id(max_id) unless params.has_key?(:silent)
|
2015-05-05 13:44:19 -04:00
|
|
|
end
|
2016-09-15 22:02:19 -04:00
|
|
|
user.reload
|
|
|
|
user.publish_notifications_state if changed
|
2015-05-05 13:44:19 -04:00
|
|
|
|
2016-09-15 22:02:19 -04:00
|
|
|
render_json_dump(notifications: serialize_data(notifications, NotificationSerializer),
|
|
|
|
seen_notification_id: current_user.seen_notification_id)
|
2015-05-05 13:44:19 -04:00
|
|
|
else
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
|
|
|
notifications = Notification.where(user_id: user.id)
|
2017-07-27 21:20:09 -04:00
|
|
|
.visible
|
|
|
|
.includes(:topic)
|
|
|
|
.order(created_at: :desc)
|
2015-05-05 13:44:19 -04: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-15 22:02:19 -04:00
|
|
|
seen_notification_id: user.seen_notification_id,
|
2015-05-05 13:44:19 -04:00
|
|
|
load_more_notifications: notifications_path(username: user.username, offset: offset + 60))
|
2014-09-02 21:32:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-10-13 06:26:30 -04:00
|
|
|
|
2015-05-05 13:44:19 -04:00
|
|
|
def mark_read
|
2016-09-16 02:14:00 -04: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))
|
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
end
|
2014-10-13 06:26:30 -04:00
|
|
|
|
2015-05-05 13:44:19 -04:00
|
|
|
render json: success_json
|
2014-10-13 06:26:30 -04:00
|
|
|
end
|
2015-05-05 13:44:19 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|