mirror of
https://github.com/discourse/discourse.git
synced 2025-02-22 17:53:10 +08:00
FEATURE: clean API method for reading a single notification
This commit is contained in:
parent
0290619669
commit
75f3f7fcbd
@ -172,10 +172,7 @@ class ApplicationController < ActionController::Base
|
|||||||
|
|
||||||
if notifications.present?
|
if notifications.present?
|
||||||
notification_ids = notifications.split(",").map(&:to_i)
|
notification_ids = notifications.split(",").map(&:to_i)
|
||||||
count = Notification.where(user_id: current_user.id, id: notification_ids, read: false).update_all(read: true)
|
Notification.read(current_user, notification_ids)
|
||||||
if count > 0
|
|
||||||
current_user.publish_notifications_state
|
|
||||||
end
|
|
||||||
cookies.delete('cn')
|
cookies.delete('cn')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -46,11 +46,14 @@ class NotificationsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def mark_read
|
def mark_read
|
||||||
Notification.where(user_id: current_user.id).includes(:topic).where(read: false).update_all(read: true)
|
if params[:id]
|
||||||
|
Notification.read(current_user, [params[:id].to_i])
|
||||||
current_user.saw_notification_id(Notification.recent_report(current_user, 1).max.try(:id))
|
else
|
||||||
current_user.reload
|
Notification.where(user_id: current_user.id).includes(:topic).where(read: false).update_all(read: true)
|
||||||
current_user.publish_notifications_state
|
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
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
@ -60,6 +60,15 @@ class Notification < ActiveRecord::Base
|
|||||||
count
|
count
|
||||||
end
|
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)
|
def self.interesting_after(min_date)
|
||||||
result = where("created_at > ?", min_date)
|
result = where("created_at > ?", min_date)
|
||||||
.includes(:topic)
|
.includes(:topic)
|
||||||
|
@ -426,6 +426,9 @@ Discourse::Application.routes.draw do
|
|||||||
|
|
||||||
get 'notifications' => 'notifications#index'
|
get 'notifications' => 'notifications#index'
|
||||||
put 'notifications/mark-read' => 'notifications#mark_read'
|
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/:provider/callback", to: "users/omniauth_callbacks#complete", via: [:get, :post]
|
||||||
match "/auth/failure", to: "users/omniauth_callbacks#failure", via: [:get, :post]
|
match "/auth/failure", to: "users/omniauth_callbacks#failure", via: [:get, :post]
|
||||||
|
@ -38,6 +38,19 @@ describe NotificationsController do
|
|||||||
expect(user.reload.total_unread_notifications).to eq(1)
|
expect(user.reload.total_unread_notifications).to eq(1)
|
||||||
end
|
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
|
it "updates the `read` status" do
|
||||||
notification = Fabricate(:notification, user: user)
|
notification = Fabricate(:notification, user: user)
|
||||||
expect(user.reload.unread_notifications).to eq(1)
|
expect(user.reload.unread_notifications).to eq(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user