Add ‘send test message’ functionality to the admin interface

This commit is contained in:
David Taylor 2017-07-04 23:35:45 +01:00
parent d97d35fd0d
commit aaaffdf371
8 changed files with 155 additions and 4 deletions

View File

@ -37,6 +37,11 @@ export default Ember.Controller.extend({
}, },
showError(error_key){ showError(error_key){
bootbox.alert(I18n.t(error_key)); bootbox.alert(I18n.t(error_key));
},
test(){
this.set('modalShowing', true);
var model = {provider:this.get('model.provider'), channel:''}
showModal('admin-plugins-chat-test', { model: model, admin: true });
} }
} }

View File

@ -0,0 +1,43 @@
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { ajax } from 'discourse/lib/ajax';
export default Ember.Controller.extend(ModalFunctionality, {
sendDisabled: function(){
if(this.get('model').topic_id && this.get('model').channel){
return false
}
return true
}.property('model.topic_id', 'model.channel'),
actions: {
send: function(){
self = this;
this.set('loading', true);
ajax("/admin/plugins/chat/test", {
data: { provider: this.get('model.provider.name'),
channel: this.get('model.channel'),
topic_id: this.get('model.topic_id')
},
type: 'POST'
}).then(function (result) {
self.set('loading', false)
self.flash(I18n.t('chat_integration.test_modal.success'), 'success');
}, function(e) {
self.set('loading', false);
var response = e.jqXHR.responseJSON
var error_key = 'chat_integration.test_modal.error'
debugger;
if(response['error_key']){
error_key = response['error_key']
}
self.flash(I18n.t(error_key), 'error');
});
}
}
});

View File

@ -0,0 +1,54 @@
{{#d-modal-body id="chat_integration_test_modal" title="chat_integration.test_modal.title"}}
<div>
<form>
<table>
<tr class="input">
<td class="label"><label for='provider'>{{i18n "chat_integration.test_modal.provider"}}</label></td>
<td>
{{i18n (concat 'chat_integration.provider.' model.provider.id '.title')}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td></td>
</tr>
<tr class="input">
<td class="label"><label for='channel'>{{i18n "chat_integration.test_modal.channel"}}</label></td>
<td>
{{text-field
name="channel"
value=model.channel
autofocus="autofocus"
id="channel-field"}}
{{!-- &nbsp;{{input-tip validation=channelValidation}} --}}
</td>
</tr>
<tr class="instructions">
<td></td>
<td><label>{{i18n (concat 'chat_integration.provider.' model.provider.id '.channel_instructions')}}</label></td>
</tr>
<tr class="input">
<td class="label"><label for='channel'>{{i18n "chat_integration.test_modal.topic"}}</label></td>
<td>
{{choose-topic selectedTopicId=model.topic_id}}
</td>
</tr>
</table>
</form>
</div>
{{/d-modal-body}}
<div class="modal-footer">
{{#conditional-loading-spinner condition=loading}}
{{d-button class='btn-primary btn-large' action="send" title="chat_integration.test_modal.send" label="chat_integration.test_modal.send" disabled=sendDisabled}}
{{d-button class="btn-large" action="closeModal" title="chat_integration.test_modal.close" label="chat_integration.test_modal.close"}}
{{/conditional-loading-spinner}}
</div>

View File

@ -67,6 +67,8 @@
</table> </table>
<div class="table-footer"> <div class="table-footer">
{{d-button action="test" actionParam=model.provider icon="rocket" title="chat_integration.test_provider" label="chat_integration.test_provider"}}
<div class="pull-right"> <div class="pull-right">
{{d-button action="create" actionParam=model.provider icon="plus" title="chat_integration.create_rule" label="chat_integration.create_rule"}} {{d-button action="create" actionParam=model.provider icon="plus" title="chat_integration.create_rule" label="chat_integration.create_rule"}}
</div> </div>

View File

@ -28,7 +28,7 @@
} }
#chat_integration_edit_rule_modal{ #chat_integration_edit_rule_modal, #chat_integration_test_modal{
table{ table{
width:100%; width:100%;
@ -51,7 +51,7 @@
} }
input{ #channel-field{
width: 200px; width: 200px;
margin-bottom: 0px; margin-bottom: 0px;
box-shadow: none; box-shadow: none;

View File

@ -9,6 +9,16 @@ en:
all_categories: "(all categories)" all_categories: "(all categories)"
all_tags: "(all tags)" all_tags: "(all tags)"
create_rule: "Create Rule" create_rule: "Create Rule"
test_provider: "Test Provider"
test_modal:
title: "Send a test message"
provider: "Provider"
channel: "Channel"
topic: "Topic"
send: "Send Test Message"
close: "Close"
error: "An unknown error occured while sending the message. Check the site logs for more information."
success: "Message sent successfully"
filter: filter:
mute: 'Mute' mute: 'Mute'
follow: 'First post only' follow: 'First post only'

View File

@ -26,7 +26,10 @@ module DiscourseChat::Provider::SlackProvider
topic = post.topic topic = post.topic
category = ''
if topic.category
category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]" category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]"
end
icon_url = icon_url =
if !SiteSetting.chat_integration_slack_icon_url.blank? if !SiteSetting.chat_integration_slack_icon_url.blank?
@ -46,7 +49,7 @@ module DiscourseChat::Provider::SlackProvider
fallback: "#{topic.title} - #{display_name}", fallback: "#{topic.title} - #{display_name}",
author_name: display_name, author_name: display_name,
author_icon: post.user.small_avatar_url, author_icon: post.user.small_avatar_url,
color: "##{topic.category.color}", color: topic.category ? "##{topic.category.color}" : nil,
text: ::DiscourseSlack::Slack.excerpt(post), text: ::DiscourseSlack::Slack.excerpt(post),
mrkdwn_in: ["text"] mrkdwn_in: ["text"]
} }

View File

@ -79,6 +79,39 @@ after_initialize do
render json:providers, root: 'providers' render json:providers, root: 'providers'
end end
def test_provider
begin
requested_provider = params[:provider]
channel = params[:channel]
topic_id = params[:topic_id]
provider = ::DiscourseChat::Provider.get_by_name(requested_provider)
if provider.nil? or not ::DiscourseChat::Provider.is_enabled(provider)
raise Discourse::NotFound
end
if defined? provider::PROVIDER_CHANNEL_REGEX
channel_regex = Regexp.new provider::PROVIDER_CHANNEL_REGEX
raise Discourse::InvalidParameters, 'Channel is not valid' if not channel_regex.match?(channel)
end
post = Topic.find(topic_id.to_i).posts.first
provider.trigger_notification(post, channel)
render json:success_json
rescue Discourse::InvalidParameters, ActiveRecord::RecordNotFound => e
render json: {errors: [e.message]}, status: 422
rescue DiscourseChat::ProviderError => e
if e.info.key?(:error_key) and !e.info[:error_key].nil?
render json: {error_key: e.info[:error_key]}, status: 422
else
render json: {errors: [e.message]}, status: 422
end
end
end
def list_rules def list_rules
providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME} providers = ::DiscourseChat::Provider.enabled_providers.map {|x| x::PROVIDER_NAME}
@ -148,6 +181,7 @@ after_initialize do
DiscourseChat::Engine.routes.draw do DiscourseChat::Engine.routes.draw do
get "" => "chat#respond" get "" => "chat#respond"
get '/providers' => "chat#list_providers" get '/providers' => "chat#list_providers"
post '/test' => "chat#test_provider"
get '/rules' => "chat#list_rules" get '/rules' => "chat#list_rules"
put '/rules' => "chat#create_rule" put '/rules' => "chat#create_rule"