mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 15:21:24 +08:00
FEATURE: Notification API Endpoints for Admins
* create/update/delete notification api with external url * remove external url feature * Fix Travis CI build error (add new line) * Fix Travis CI build error
This commit is contained in:
@ -3,6 +3,8 @@ require_dependency 'notification_serializer'
|
|||||||
class NotificationsController < ApplicationController
|
class NotificationsController < ApplicationController
|
||||||
|
|
||||||
requires_login
|
requires_login
|
||||||
|
before_action :ensure_admin, only: [:create, :update, :destroy] # only admin can create/edit/delete notification
|
||||||
|
before_action :set_notification, only: [:update, :destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
user =
|
user =
|
||||||
@ -64,4 +66,33 @@ class NotificationsController < ApplicationController
|
|||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@notification = Notification.create!(notification_params)
|
||||||
|
render_notification
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@notification.update!(notification_params)
|
||||||
|
render_notification
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@notification.destroy!
|
||||||
|
render json: success_json
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_notification
|
||||||
|
@notification = Notification.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def notification_params
|
||||||
|
params.permit(:notification_type, :user_id, :data, :read, :topic_id, :post_number, :post_action_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_notification
|
||||||
|
render_json_dump(NotificationSerializer.new(@notification, scope: guardian, root: false))
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -489,11 +489,14 @@ Discourse::Application.routes.draw do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get 'notifications' => 'notifications#index'
|
resources :notifications, except: :show do
|
||||||
put 'notifications/mark-read' => 'notifications#mark_read'
|
collection do
|
||||||
|
put 'mark-read' => 'notifications#mark_read'
|
||||||
# creating an alias cause the api was extended to mark a single notification
|
# creating an alias cause the api was extended to mark a single notification
|
||||||
# this allows us to cleanly target it
|
# this allows us to cleanly target it
|
||||||
put 'notifications/read' => 'notifications#mark_read'
|
put 'read' => 'notifications#mark_read'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
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]
|
||||||
|
@ -1,8 +1,34 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
|
def create_notification(user_id, resp_code, matcher)
|
||||||
|
notification_count = Notification.count
|
||||||
|
post :create, params: { notification_type: Notification.types[:mentioned], user_id: user_id, data: { message: 'tada' }.to_json }, format: :json
|
||||||
|
expect(response.status).to eq(resp_code)
|
||||||
|
expect(Notification.count).send(matcher, eq(notification_count))
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_notification(topic_id, resp_code, matcher)
|
||||||
|
notification = Fabricate(:notification)
|
||||||
|
post :update, params: { id: notification.id, topic_id: topic_id }, format: :json
|
||||||
|
expect(response.status).to eq(resp_code)
|
||||||
|
notification.reload
|
||||||
|
expect(notification.topic_id).send(matcher, eq(topic_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_notification(resp_code, matcher)
|
||||||
|
notification = Fabricate(:notification)
|
||||||
|
notification_count = Notification.count
|
||||||
|
delete :destroy, params: { id: notification.id }, format: :json
|
||||||
|
expect(response.status).to eq(resp_code)
|
||||||
|
expect(Notification.count).send(matcher, eq(notification_count))
|
||||||
|
end
|
||||||
|
|
||||||
describe NotificationsController do
|
describe NotificationsController do
|
||||||
|
|
||||||
context 'when logged in' do
|
context 'when logged in' do
|
||||||
|
|
||||||
|
context 'as normal user' do
|
||||||
|
|
||||||
let!(:user) { log_in }
|
let!(:user) { log_in }
|
||||||
|
|
||||||
describe '#index' do
|
describe '#index' do
|
||||||
@ -71,13 +97,83 @@ describe NotificationsController do
|
|||||||
expect(user.reload.unread_notifications).to eq(0)
|
expect(user.reload.unread_notifications).to eq(0)
|
||||||
expect(user.reload.total_unread_notifications).to eq(0)
|
expect(user.reload.total_unread_notifications).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#create' do
|
||||||
|
it "can't create notification" do
|
||||||
|
create_notification(user.id, 403, :to)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#update' do
|
||||||
|
it "can't update notification" do
|
||||||
|
update_notification(8, 403, :to_not)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#destroy' do
|
||||||
|
it "can't delete notification" do
|
||||||
|
delete_notification(403, :to)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'as admin' do
|
||||||
|
|
||||||
|
let!(:admin) { log_in(:admin) }
|
||||||
|
|
||||||
|
describe '#create' do
|
||||||
|
it "can create notification" do
|
||||||
|
create_notification(admin.id, 200, :to_not)
|
||||||
|
expect(::JSON.parse(response.body)["id"]).to_not eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#update' do
|
||||||
|
it "can update notification" do
|
||||||
|
update_notification(8, 200, :to)
|
||||||
|
expect(::JSON.parse(response.body)["topic_id"]).to eq(8)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#destroy' do
|
||||||
|
it "can delete notification" do
|
||||||
|
delete_notification(200, :to_not)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when not logged in' do
|
context 'when not logged in' do
|
||||||
|
|
||||||
|
describe '#index' do
|
||||||
it 'should raise an error' do
|
it 'should raise an error' do
|
||||||
get :index, params: { recent: true }, format: :json
|
get :index, params: { recent: true }, format: :json
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#create' do
|
||||||
|
it "can't create notification" do
|
||||||
|
user = Fabricate(:user)
|
||||||
|
create_notification(user.id, 403, :to)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#update' do
|
||||||
|
it "can't update notification" do
|
||||||
|
update_notification(8, 403, :to_not)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#destroy' do
|
||||||
|
it "can't delete notification" do
|
||||||
|
delete_notification(403, :to)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user