mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 18:13:39 +08:00
25a82e7d22
also publish seen_notification_id so we can tell what is new and what is old cleanup controller so it correctly checks user fix bug around clearing notification when people click mark read
59 lines
2.0 KiB
Ruby
59 lines
2.0 KiB
Ruby
require_dependency 'notification_serializer'
|
|
|
|
class NotificationsController < ApplicationController
|
|
|
|
before_filter :ensure_logged_in
|
|
|
|
def index
|
|
user = current_user
|
|
user = User.find_by_username(params[:username].to_s) if params[:username]
|
|
|
|
guardian.ensure_can_see_notifications!(user)
|
|
|
|
if params[:recent].present?
|
|
limit = (params[:limit] || 15).to_i
|
|
limit = 50 if limit > 50
|
|
|
|
notifications = Notification.recent_report(current_user, limit)
|
|
changed = false
|
|
|
|
if notifications.present?
|
|
# ordering can be off due to PMs
|
|
max_id = notifications.map(&:id).max
|
|
changed = current_user.saw_notification_id(max_id) unless params.has_key?(:silent)
|
|
end
|
|
user.reload
|
|
user.publish_notifications_state if changed
|
|
|
|
render_json_dump(notifications: serialize_data(notifications, NotificationSerializer),
|
|
seen_notification_id: current_user.seen_notification_id)
|
|
else
|
|
offset = params[:offset].to_i
|
|
|
|
notifications = Notification.where(user_id: user.id)
|
|
.visible
|
|
.includes(:topic)
|
|
.order(created_at: :desc)
|
|
|
|
total_rows = notifications.dup.count
|
|
notifications = notifications.offset(offset).limit(60)
|
|
render_json_dump(notifications: serialize_data(notifications, NotificationSerializer),
|
|
total_rows_notifications: total_rows,
|
|
seen_notification_id: user.seen_notification_id,
|
|
load_more_notifications: notifications_path(username: user.username, offset: offset + 60))
|
|
end
|
|
|
|
end
|
|
|
|
def mark_read
|
|
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
|
|
|
|
render json: success_json
|
|
end
|
|
|
|
end
|