2015-05-05 12:44:19 -05:00
|
|
|
require_dependency 'notification_serializer'
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class NotificationsController < ApplicationController
|
|
|
|
|
|
|
|
before_filter :ensure_logged_in
|
|
|
|
|
2015-05-05 12:44:19 -05:00
|
|
|
def index
|
2014-09-02 20:32:27 -05:00
|
|
|
user = current_user
|
2015-05-05 12:44:19 -05:00
|
|
|
if params[:recent].present?
|
|
|
|
notifications = Notification.recent_report(current_user, 10)
|
|
|
|
|
|
|
|
if notifications.present?
|
|
|
|
# ordering can be off due to PMs
|
|
|
|
max_id = notifications.map(&:id).max
|
|
|
|
current_user.saw_notification_id(max_id) unless params.has_key?(:silent)
|
|
|
|
end
|
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
|
|
|
|
render_serialized(notifications, NotificationSerializer, root: :notifications)
|
|
|
|
else
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
user = User.find_by_username(params[:username].to_s) if params[:username]
|
|
|
|
|
|
|
|
guardian.ensure_can_see_notifications!(user)
|
|
|
|
|
|
|
|
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,
|
|
|
|
load_more_notifications: notifications_path(username: user.username, offset: offset + 60))
|
2014-09-02 20:32:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-10-13 05:26:30 -05:00
|
|
|
|
2015-05-05 12:44:19 -05:00
|
|
|
def mark_read
|
|
|
|
Notification.where(user_id: current_user.id).includes(:topic).where(read: false).update_all(read: true)
|
2014-10-13 05:26:30 -05:00
|
|
|
|
|
|
|
current_user.saw_notification_id(Notification.recent_report(current_user, 1).max)
|
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
|
2015-05-05 12:44:19 -05:00
|
|
|
render json: success_json
|
2014-10-13 05:26:30 -05:00
|
|
|
end
|
2015-05-05 12:44:19 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|