FEATURE: prompts new webhook events

This commit is contained in:
Erick Guan 2016-09-14 07:54:53 +08:00 committed by Guo Xiang Tan
parent 115461b395
commit 00d5facf36
9 changed files with 86 additions and 6 deletions

View file

@ -1,14 +1,65 @@
import { ajax } from 'discourse/lib/ajax'; import { ajax } from 'discourse/lib/ajax';
import { popupAjaxError } from 'discourse/lib/ajax-error'; import { popupAjaxError } from 'discourse/lib/ajax-error';
import computed from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
pingDisabled: false,
incomingEventIds: [],
incomingCount: Ember.computed.alias("incomingEventIds.length"),
@computed('incomingCount')
hasIncoming(incomingCount) {
return incomingCount > 0;
},
subscribe() {
this.messageBus.subscribe(`/web_hook_events/${this.get('model.extras.web_hook_id')}`, data => {
if (data.event_type === 'ping') {
this.set('pingDisabled', false);
}
this._addIncoming(data.web_hook_event_id);
});
},
unsubscribe() {
this.messageBus.unsubscribe('/web_hook_events/*');
},
_addIncoming(eventId) {
const incomingEventIds = this.get("incomingEventIds");
if (incomingEventIds.indexOf(eventId) === -1) {
incomingEventIds.pushObject(eventId);
}
},
actions: { actions: {
loadMore() { loadMore() {
this.get('model').loadMore(); this.get('model').loadMore();
}, },
ping() { ping() {
ajax(`/admin/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, {type: 'POST'}).catch(popupAjaxError); this.set('pingDisabled', true);
ajax(`/admin/web_hooks/${this.get('model.extras.web_hook_id')}/ping`, {
type: 'POST'
}).catch(error => {
this.set('pingDisabled', false);
popupAjaxError(error);
});
},
showInserted() {
const webHookId = this.get('model.extras.web_hook_id');
ajax(`/admin/web_hooks/${webHookId}/events/bulk`, {
type: 'GET',
data: { ids: this.get('incomingEventIds') }
}).then(data => {
const objects = data.map(event => this.store.createRecord('web-hook-event', event));
this.get("model").unshiftObjects(objects);
this.set("incomingEventIds", []);
});
} }
} }
}); });

View file

@ -5,6 +5,11 @@ export default Discourse.Route.extend({
setupController(controller, model) { setupController(controller, model) {
controller.set('model', model); controller.set('model', model);
controller.subscribe();
},
deactivate() {
this.controllerFor('adminWebHooks.showEvents').unsubscribe();
}, },
renderTemplate() { renderTemplate() {

View file

@ -2,7 +2,7 @@
{{#link-to 'adminWebHooks' tagName='button' classNames='btn'}} {{#link-to 'adminWebHooks' tagName='button' classNames='btn'}}
{{fa-icon 'list'}} {{i18n 'admin.web_hooks.events.go_list'}} {{fa-icon 'list'}} {{i18n 'admin.web_hooks.events.go_list'}}
{{/link-to}} {{/link-to}}
{{d-button icon="send" label="admin.web_hooks.events.ping" action="ping"}} {{d-button icon="send" label="admin.web_hooks.events.ping" action="ping" disabled=pingDisabled}}
{{#link-to 'adminWebHooks.show' model.extras.web_hook_id tagName='button' classNames='btn'}} {{#link-to 'adminWebHooks.show' model.extras.web_hook_id tagName='button' classNames='btn'}}
{{fa-icon 'edit'}} {{i18n 'admin.web_hooks.events.go_details'}} {{fa-icon 'edit'}} {{i18n 'admin.web_hooks.events.go_details'}}
{{/link-to}} {{/link-to}}
@ -10,6 +10,12 @@
<div class='web-hook-events-listing'> <div class='web-hook-events-listing'>
{{#if model}} {{#if model}}
{{#if hasIncoming}}
<div class='alert alert-info clickable' {{action "showInserted"}}>
{{count-i18n key="admin.web_hooks.events.incoming" count=incomingCount}}
{{i18n 'click_to_show'}}
</div>
{{/if}}
{{#load-more selector=".web-hook-events li" action="loadMore"}} {{#load-more selector=".web-hook-events li" action="loadMore"}}
<div class='web-hook-events content-list'> <div class='web-hook-events content-list'>
<ul> <ul>

View file

@ -1859,8 +1859,6 @@ table#user-badges {
} }
.web-hook-events { .web-hook-events {
margin-top: 15px;
li { li {
padding: 2px 0; padding: 2px 0;
} }
@ -1906,6 +1904,13 @@ table#user-badges {
} }
} }
.web-hook-events-listing {
margin-top: 15px;
.alert {
margin: 15px 0 0 0;
}
}
// Mobile specific styles // Mobile specific styles
// Mobile view text-inputs need some padding // Mobile view text-inputs need some padding
.mobile-view .admin-contents { .mobile-view .admin-contents {

View file

@ -1,5 +1,5 @@
class Admin::WebHooksController < Admin::AdminController class Admin::WebHooksController < Admin::AdminController
before_filter :fetch_web_hook, only: %i(show update destroy list_events ping) before_filter :fetch_web_hook, only: %i(show update destroy list_events bulk_events ping)
def index def index
limit = 50 limit = 50
@ -72,6 +72,12 @@ class Admin::WebHooksController < Admin::AdminController
render json: MultiJson.dump(json), status: 200 render json: MultiJson.dump(json), status: 200
end end
def bulk_events
params.require(:ids)
web_hook_events = @web_hook.web_hook_events.where(id: params[:ids])
render_serialized(web_hook_events, AdminWebHookEventSerializer)
end
def redeliver_event def redeliver_event
web_hook_event = WebHookEvent.find(params[:event_id]) web_hook_event = WebHookEvent.find(params[:event_id])

View file

@ -67,6 +67,10 @@ module Jobs
response_headers: MultiJson.dump(response.headers), response_headers: MultiJson.dump(response.headers),
response_body: response.body, response_body: response.body,
duration: ((Time.zone.now - now) * 1000).to_i) duration: ((Time.zone.now - now) * 1000).to_i)
MessageBus.publish("/web_hook_events/#{@web_hook.id}", {
web_hook_event_id: web_hook_event.id,
event_type: @opts[:event_type]
}, user_ids: User.staff.pluck(:id))
end end
def build_web_hook_body def build_web_hook_body

View file

@ -10,7 +10,6 @@ class AdminWebHookEventSerializer < ApplicationSerializer
:duration, :duration,
:created_at :created_at
def request_url def request_url
object.web_hook.payload_url object.web_hook.payload_url
end end

View file

@ -2478,6 +2478,9 @@ en:
events: events:
none: "There are no related events." none: "There are no related events."
redeliver: "Redeliver" redeliver: "Redeliver"
incoming:
one: "There is a new event."
other: "There are {{count}} new events."
completion: "Completed in %{seconds} seconds." completion: "Completed in %{seconds} seconds."
request: "Request" request: "Request"
response: "Response" response: "Response"

View file

@ -209,6 +209,7 @@ Discourse::Application.routes.draw do
resources :web_hooks, constraints: AdminConstraint.new resources :web_hooks, constraints: AdminConstraint.new
get 'web_hook_events/:id' => 'web_hooks#list_events', constraints: AdminConstraint.new, as: :web_hook_events get 'web_hook_events/:id' => 'web_hooks#list_events', constraints: AdminConstraint.new, as: :web_hook_events
get 'web_hooks/:id/events' => 'web_hooks#list_events', constraints: AdminConstraint.new get 'web_hooks/:id/events' => 'web_hooks#list_events', constraints: AdminConstraint.new
get 'web_hooks/:id/events/bulk' => 'web_hooks#bulk_events', constraints: AdminConstraint.new
post 'web_hooks/:web_hook_id/events/:event_id/redeliver' => 'web_hooks#redeliver_event', constraints: AdminConstraint.new post 'web_hooks/:web_hook_id/events/:event_id/redeliver' => 'web_hooks#redeliver_event', constraints: AdminConstraint.new
post 'web_hooks/:id/ping' => 'web_hooks#ping', constraints: AdminConstraint.new post 'web_hooks/:id/ping' => 'web_hooks#ping', constraints: AdminConstraint.new