FEATURE: clean API method for reading a single notification

This commit is contained in:
Sam 2016-09-16 16:14:00 +10:00
parent 0290619669
commit 75f3f7fcbd
5 changed files with 34 additions and 9 deletions

View file

@ -172,10 +172,7 @@ class ApplicationController < ActionController::Base
if notifications.present?
notification_ids = notifications.split(",").map(&:to_i)
count = Notification.where(user_id: current_user.id, id: notification_ids, read: false).update_all(read: true)
if count > 0
current_user.publish_notifications_state
end
Notification.read(current_user, notification_ids)
cookies.delete('cn')
end
end

View file

@ -46,11 +46,14 @@ class NotificationsController < ApplicationController
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
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
render json: success_json
end

View file

@ -60,6 +60,15 @@ class Notification < ActiveRecord::Base
count
end
def self.read(user, notification_ids)
count = Notification.where(user_id: user.id,
id: notification_ids,
read: false).update_all(read: true)
if count > 0
user.publish_notifications_state
end
end
def self.interesting_after(min_date)
result = where("created_at > ?", min_date)
.includes(:topic)

View file

@ -426,6 +426,9 @@ Discourse::Application.routes.draw do
get 'notifications' => 'notifications#index'
put 'notifications/mark-read' => 'notifications#mark_read'
# creating an alias cause the api was extended to mark a single notification
# this allows us to cleanly target it
put 'notifications/read' => 'notifications#mark_read'
match "/auth/:provider/callback", to: "users/omniauth_callbacks#complete", via: [:get, :post]
match "/auth/failure", to: "users/omniauth_callbacks#failure", via: [:get, :post]

View file

@ -38,6 +38,19 @@ describe NotificationsController do
expect(user.reload.total_unread_notifications).to eq(1)
end
it "can update a single notification" do
notification = Fabricate(:notification, user: user)
notification2 = Fabricate(:notification, user: user)
xhr :put, :mark_read, id: notification.id
expect(response).to be_success
notification.reload
notification2.reload
expect(notification.read).to eq(true)
expect(notification2.read).to eq(false)
end
it "updates the `read` status" do
notification = Fabricate(:notification, user: user)
expect(user.reload.unread_notifications).to eq(1)