REFACTOR: Clean up code in plugin.

This commit is contained in:
Guo Xiang Tan 2017-10-03 17:42:07 +08:00
parent aa4d169c0e
commit f836047f90
40 changed files with 457 additions and 465 deletions

View File

@ -26,7 +26,7 @@ class DiscourseChat::ChatController < ApplicationController
provider = ::DiscourseChat::Provider.get_by_name(channel.provider)
if not ::DiscourseChat::Provider.is_enabled(provider)
if !DiscourseChat::Provider.is_enabled(provider)
raise Discourse::NotFound
end
@ -48,16 +48,12 @@ class DiscourseChat::ChatController < ApplicationController
end
def list_channels
providers = ::DiscourseChat::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
providers = ::DiscourseChat::Provider.enabled_provider_names
requested_provider = params[:provider]
if not providers.include? requested_provider
raise Discourse::NotFound
end
raise Discourse::InvalidParameters if !providers.include?(requested_provider)
channels = DiscourseChat::Channel.with_provider(requested_provider)
render_serialized channels, DiscourseChat::ChannelSerializer, root: 'channels'
end
@ -65,13 +61,13 @@ class DiscourseChat::ChatController < ApplicationController
begin
providers = ::DiscourseChat::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
if (not defined? params[:channel]) && defined? params[:channel][:provider]
if !defined?(params[:channel]) && defined?(params[:channel][:provider])
raise Discourse::InvalidParameters, 'Provider is not valid'
end
requested_provider = params[:channel][:provider]
if not providers.include? requested_provider
if !providers.include?(requested_provider)
raise Discourse::InvalidParameters, 'Provider is not valid'
end
@ -81,7 +77,7 @@ class DiscourseChat::ChatController < ApplicationController
channel = DiscourseChat::Channel.new(hash)
if not channel.save(hash)
if !channel.save(hash)
raise Discourse::InvalidParameters, 'Channel is not valid'
end
@ -100,7 +96,7 @@ class DiscourseChat::ChatController < ApplicationController
hash = params.require(:channel).permit(data: allowed_keys)
if not channel.update(hash)
if !channel.update(hash)
raise Discourse::InvalidParameters, 'Channel is not valid'
end
@ -111,9 +107,9 @@ class DiscourseChat::ChatController < ApplicationController
end
def destroy_channel
rule = DiscourseChat::Channel.find(params[:id].to_i)
rule.destroy
rule = DiscourseChat::Channel.find_by(id: params[:id])
raise Discourse::InvalidParameters unless rule
rule.destroy!
render json: success_json
end
@ -121,10 +117,9 @@ class DiscourseChat::ChatController < ApplicationController
def create_rule
begin
hash = params.require(:rule).permit(:channel_id, :type, :filter, :group_id, :category_id, tags: [])
rule = DiscourseChat::Rule.new(hash)
if not rule.save(hash)
if !rule.save(hash)
raise Discourse::InvalidParameters, 'Rule is not valid'
end
@ -139,7 +134,7 @@ class DiscourseChat::ChatController < ApplicationController
rule = DiscourseChat::Rule.find(params[:id].to_i)
hash = params.require(:rule).permit(:type, :filter, :group_id, :category_id, tags: [])
if not rule.update(hash)
if !rule.update(hash)
raise Discourse::InvalidParameters, 'Rule is not valid'
end
@ -150,9 +145,9 @@ class DiscourseChat::ChatController < ApplicationController
end
def destroy_rule
rule = DiscourseChat::Rule.find(params[:id].to_i)
rule.destroy
rule = DiscourseChat::Rule.find_by(id: params[:id])
raise Discourse::InvalidParameters.new unless rule
rule.destroy!
render json: success_json
end

View File

@ -4,15 +4,13 @@ class DiscourseChat::PublicController < ApplicationController
def post_transcript
params.require(:secret)
redis_key = "chat_integration:transcript:" + params[:secret]
redis_key = "chat_integration:transcript:#{params[:secret]}"
content = $redis.get(redis_key)
if content
render json: { content: content }
return
else
raise Discourse::NotFound
end
raise Discourse::NotFound
end
end

View File

@ -33,11 +33,12 @@ module DiscourseChat
if token.start_with?('tag:')
tag_name = token.sub(/^tag:/, '')
else
return error_text # Abort and send help text
return error_text
end
tag = Tag.find_by(name: tag_name)
unless tag # If tag doesn't exist, abort
unless tag
return I18n.t("chat_integration.provider.#{provider}.not_found.tag", name: tag_name)
end
tags.push(tag.name)
@ -46,11 +47,11 @@ module DiscourseChat
category_id = category.nil? ? nil : category.id
case DiscourseChat::Helper.smart_create_rule(channel: channel, filter: cmd, category_id: category_id, tags: tags)
when :created
return I18n.t("chat_integration.provider.#{provider}.create.created")
I18n.t("chat_integration.provider.#{provider}.create.created")
when :updated
return I18n.t("chat_integration.provider.#{provider}.create.updated")
I18n.t("chat_integration.provider.#{provider}.create.updated")
else
return I18n.t("chat_integration.provider.#{provider}.create.error")
I18n.t("chat_integration.provider.#{provider}.create.error")
end
when "remove"
return error_text unless tokens.size == 1
@ -59,16 +60,16 @@ module DiscourseChat
return error_text unless rule_number.to_s == tokens[0] # Check we were given a number
if DiscourseChat::Helper.delete_by_index(channel, rule_number)
return I18n.t("chat_integration.provider.#{provider}.delete.success")
I18n.t("chat_integration.provider.#{provider}.delete.success")
else
return I18n.t("chat_integration.provider.#{provider}.delete.error")
I18n.t("chat_integration.provider.#{provider}.delete.error")
end
when "status"
return DiscourseChat::Helper.status_for_channel(channel)
when "help"
return I18n.t("chat_integration.provider.#{provider}.help")
I18n.t("chat_integration.provider.#{provider}.help")
else
return error_text
error_text
end
end
@ -105,12 +106,12 @@ module DiscourseChat
end
text << I18n.t("chat_integration.provider.#{provider}.status.rule_string",
index: i,
filter: rule.filter,
category: category_name
)
index: i,
filter: rule.filter,
category: category_name
)
if SiteSetting.tagging_enabled && (not rule.tags.nil?)
if SiteSetting.tagging_enabled && (!rule.tags.nil?)
text << I18n.t("chat_integration.provider.#{provider}.status.rule_string_tags_suffix", tags: rule.tags.join(', '))
end
@ -121,16 +122,15 @@ module DiscourseChat
if rules.size == 0
text << I18n.t("chat_integration.provider.#{provider}.status.no_rules")
end
return text
text
end
# Delete a rule based on its (1 based) index as seen in the
# status_for_channel function
def self.delete_by_index(channel, index)
rules = channel.rules.order_by_precedence
return false if index < (1) || index > (rules.size)
return :deleted if rules[index - 1].destroy
end
@ -184,17 +184,14 @@ module DiscourseChat
# This rule is unique! Create a new one:
return :created if Rule.new(channel: channel, filter: filter, category_id: category_id, tags: tags).save
return false # Error
false
end
def self.save_transcript(transcript)
secret = SecureRandom.hex
redis_key = "chat_integration:transcript:" + secret
$redis.set(redis_key, transcript, ex: 3600) # Expire in 1 hour
return secret
redis_key = "chat_integration:transcript:#{secret}"
$redis.setex(redis_key, 3600, transcript)
secret
end
end

View File

@ -2,12 +2,12 @@ module ::DiscourseChat
PLUGIN_NAME = "discourse-chat-integration".freeze
class AdminEngine < ::Rails::Engine
engine_name DiscourseChat::PLUGIN_NAME + "-admin"
engine_name "#{DiscourseChat::PLUGIN_NAME}-admin"
isolate_namespace DiscourseChat
end
class PublicEngine < ::Rails::Engine
engine_name DiscourseChat::PLUGIN_NAME + "-public"
engine_name "#{DiscourseChat::PLUGIN_NAME}-public"
isolate_namespace DiscourseChat
end

View File

@ -1,9 +1,9 @@
module Jobs
class NotifyChats < Jobs::Base
sidekiq_options retry: false # Don't retry, could result in duplicate notifications for some providers
def execute(args)
return if not SiteSetting.chat_integration_enabled? # Plugin may have been disabled since job triggered
sidekiq_options retry: false
def execute(args)
return if !SiteSetting.chat_integration_enabled?
::DiscourseChat::Manager.trigger_notifications(args[:post_id])
end
end

View File

@ -29,10 +29,8 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
end
def provider_valid?
# Validate provider
if not ::DiscourseChat::Provider.provider_names.include? provider
if !DiscourseChat::Provider.provider_names.include?(provider)
errors.add(:provider, "#{provider} is not a valid provider")
return
end
end
@ -66,6 +64,5 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
if check_unique && matching_channels.exists?
errors.add(:data, "matches an existing channel")
end
end
end

View File

@ -17,8 +17,6 @@ module DiscourseChat
return if post.blank? || post.post_type != Post.types[:regular]
topic = post.topic
# Abort if topic is blank... this should never be the case
return if topic.blank?
# If it's a private message, filter rules by groups, otherwise filter rules by category
@ -66,7 +64,7 @@ module DiscourseChat
matching_rules = matching_rules.select { |rule| rule.filter != "mute" }
# If this is not the first post, discard all "follow" rules
if not post.is_first_post?
if !post.is_first_post?
matching_rules = matching_rules.select { |rule| rule.filter != "follow" }
end

View File

@ -1,6 +1,3 @@
import buildPluginAdapter from 'admin/adapters/build-plugin';
export default buildPluginAdapter('chat').extend({
});
export default buildPluginAdapter('chat');

View File

@ -1,5 +1,3 @@
import buildPluginAdapter from 'admin/adapters/build-plugin';
export default buildPluginAdapter('chat').extend({
});
export default buildPluginAdapter('chat');

View File

@ -1,5 +1,3 @@
import buildPluginAdapter from 'admin/adapters/build-plugin';
export default buildPluginAdapter('chat').extend({
});
export default buildPluginAdapter('chat');

View File

@ -33,8 +33,8 @@ export default Ember.Component.extend({
this.sendAction('editRule', rule, this.get('channel'));
},
showError(error_key){
bootbox.alert(I18n.t(error_key));
showError(errorKey){
bootbox.alert(I18n.t(errorKey));
},
}

View File

@ -1,32 +1,37 @@
import { popupAjaxError } from 'discourse/lib/ajax-error';
import computed from 'ember-addons/ember-computed-decorators';
export default Ember.Component.extend({
tagName: 'tr',
isCategory: function(){
return this.get('rule.type') === 'normal';
}.property('rule.type'),
@computed('rule.type')
isCategory(type) {
return type === 'normal';
},
isMessage: function(){
return this.get('rule.type') === 'group_message';
}.property('rule.type'),
@computed('rule.type')
isMessage(type) {
return type === 'group_message';
},
isMention: function(){
return this.get('rule.type') === 'group_mention';
}.property('rule.type'),
@computed('rule.type')
isMention(type) {
return type === 'group_mention';
},
actions: {
edit: function(){
edit() {
this.sendAction('edit', this.get('rule'));
},
delete(rule){
delete(rule) {
rule.destroyRecord().then(() => {
this.send('refresh');
}).catch(popupAjaxError);
},
refresh: function(){
refresh() {
this.sendAction('refresh');
}
}
});

View File

@ -1,46 +1,73 @@
import showModal from 'discourse/lib/show-modal';
import computed from "ember-addons/ember-computed-decorators";
export default Ember.Controller.extend({
modalShowing: false,
anyErrors: function(){
var anyErrors = false;
this.get('model.channels').forEach(function(channel){
if(channel.error_key){
@computed('model.channels')
anyErrors(channels) {
let anyErrors = false;
channels.forEach((channel) => {
if (channel.error_key) {
anyErrors = true;
}
});
return anyErrors;
}.property('model.channels'),
},
actions:{
createChannel(){
createChannel() {
this.set('modalShowing', true);
var model = {channel: this.store.createRecord('channel',{provider: this.get('model.provider').id, data:{}},), provider:this.get('model.provider')};
const model = {
channel: this.store.createRecord('channel', { provider: this.get('model.provider.id'), data:{} }),
provider: this.get('model.provider')
};
showModal('admin-plugins-chat-edit-channel', { model: model, admin: true });
},
editChannel(channel){
editChannel(channel) {
this.set('modalShowing', true);
var model = {channel: channel, provider: this.get('model.provider')};
const model = {
channel: channel,
provider: this.get('model.provider')
};
showModal('admin-plugins-chat-edit-channel', { model: model, admin: true });
},
testChannel(channel){
testChannel(channel) {
this.set('modalShowing', true);
var model = {channel:channel};
showModal('admin-plugins-chat-test', { model: model, admin: true });
showModal('admin-plugins-chat-test', { model: { channel: channel }, admin: true });
},
createRule(channel){
this.set('modalShowing', true);
var model = {rule: this.store.createRecord('rule',{channel_id: channel.id}), channel:channel, provider:this.get('model.provider'), groups:this.get('model.groups')};
const model = {
rule: this.store.createRecord('rule', { channel_id: channel.id }),
channel: channel,
provider: this.get('model.provider'),
groups: this.get('model.groups')
};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
},
editRule(rule, channel){
this.set('modalShowing', true);
var model = {rule: rule, channel:channel, provider:this.get('model.provider'), groups:this.get('model.groups')};
const model = {
rule: rule,
channel: channel,
provider: this.get('model.provider'),
groups: this.get('model.groups')
};
showModal('admin-plugins-chat-edit-rule', { model: model, admin: true });
},
}
});

View File

@ -1,10 +1,11 @@
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { extractError } from 'discourse/lib/ajax-error';
import { popupAjaxError } from 'discourse/lib/ajax-error';
import InputValidation from 'discourse/models/input-validation';
import { default as computed, observes } from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend(ModalFunctionality, {
setupKeydown: function() {
setupKeydown() {
Ember.run.schedule('afterRender', () => {
$('#chat_integration_edit_channel_modal').keydown(e => {
if (e.keyCode === 13) {
@ -15,36 +16,37 @@ export default Ember.Controller.extend(ModalFunctionality, {
}.on('init'),
// The validation property must be defined at runtime since the possible parameters vary by provider
setupValidations: function(){
@observes('model')
setupValidations() {
if(this.get('model.provider')){
var theKeys = this.get('model.provider.channel_parameters').map( ( param ) => param['key'] );
Ember.defineProperty(this,'paramValidation',Ember.computed('model.channel.data.{' + theKeys.join(',') + '}',this._paramValidation));
const theKeys = this.get('model.provider.channel_parameters').map( ( param ) => param['key'] );
Ember.defineProperty(this,'paramValidation', Ember.computed(`model.channel.data.{${theKeys.join(',')}},this._paramValidation`));
}
}.observes('model'),
},
validate(parameter){
var regString = parameter.regex;
var regex = new RegExp(regString);
var val = this.get('model.channel.data.'+parameter.key);
validate(parameter) {
const regString = parameter.regex;
const regex = new RegExp(regString);
let val = this.get(`model.channel.data.${parameter.key}`);
if(val===undefined){
if (val === undefined) {
val = "";
}
if(val === ""){ // Fail silently if field blank
if (val === "") { // Fail silently if field blank
return InputValidation.create({
failed: true,
});
}else if(!regString){ // Pass silently if no regex available for provider
} else if (!regString) { // Pass silently if no regex available for provider
return InputValidation.create({
ok: true,
});
}else if(regex.test(val)){ // Test against regex
} else if (regex.test(val)) { // Test against regex
return InputValidation.create({
ok: true,
reason: I18n.t('chat_integration.edit_channel_modal.channel_validation.ok')
});
}else{ // Failed regex
} else { // Failed regex
return InputValidation.create({
failed: true,
reason: I18n.t('chat_integration.edit_channel_modal.channel_validation.fail')
@ -53,50 +55,47 @@ export default Ember.Controller.extend(ModalFunctionality, {
},
_paramValidation: function(){
var response = {};
var parameters = this.get('model.provider.channel_parameters');
_paramValidation() {
const response = {};
const parameters = this.get('model.provider.channel_parameters');
parameters.forEach(parameter => {
response[parameter.key] = this.validate(parameter);
});
return response;
},
saveDisabled: function(){
var validations = this.get('paramValidation');
@computed('paramValidation')
saveDisabled(paramValidation) {
if (!paramValidation) return true;
if(!validations){ return true; }
let invalid = false;
var invalid = false;
Object.keys(validations).forEach(key =>{
if(!validations[key]){
Object.keys(paramValidation).forEach(key =>{
if (!validations[key]) {
invalid = true;
}
if(!validations[key]['ok']){
if (!validations[key]['ok']) {
invalid = true;
}
});
return invalid;
}.property('paramValidation'),
},
actions: {
cancel: function(){
cancel() {
this.send('closeModal');
},
save: function(){
if(this.get('saveDisabled')){return;};
const self = this;
this.get('model.channel').save().then(function() {
self.send('closeModal');
}).catch(function(error) {
self.flash(extractError(error), 'error');
});
save() {
if (this.get('saveDisabled')) return;
this.get('model.channel').save().then(() => {
this.send('closeModal');
}).catch(popupAjaxError);
}
}
});

View File

@ -1,9 +1,11 @@
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { extractError } 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(ModalFunctionality, {
setupKeydown: function() {
saveDisabled: false,
setupKeydown() {
Ember.run.schedule('afterRender', () => {
$('#chat_integration_edit_channel_modal').keydown(e => {
if (e.keyCode === 13) {
@ -13,31 +15,22 @@ export default Ember.Controller.extend(ModalFunctionality, {
});
}.on('init'),
saveDisabled: function(){
return false;
}.property(),
@computed('model.rule.type')
showCategory: function(type){
return (type === "normal");
showCategory(type) {
return type === "normal";
},
actions: {
cancel: function(){
cancel() {
this.send('closeModal');
},
save: function(){
if(this.get('saveDisabled')){return;};
const self = this;
this.get('model.rule').save().then(function() {
self.send('closeModal');
}).catch(function(error) {
self.flash(extractError(error), 'error');
});
save() {
if (this.get('saveDisabled')) return;
this.get('model.rule').save().then(() => {
this.send('closeModal');
}).catch(popupAjaxError);
}
}
});

View File

@ -1,8 +1,9 @@
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { ajax } from 'discourse/lib/ajax';
import computed from "ember-addons/ember-computed-decorators";
export default Ember.Controller.extend(ModalFunctionality, {
setupKeydown: function() {
setupKeydown() {
Ember.run.schedule('afterRender', () => {
$('#chat_integration_test_modal').keydown(e => {
if (e.keyCode === 13) {
@ -12,42 +13,26 @@ export default Ember.Controller.extend(ModalFunctionality, {
});
}.on('init'),
sendDisabled: function(){
if(this.get('model').topic_id){
return false;
}
return true;
}.property('model.topic_id'),
@computed('model.topic_id')
sendDisabled(topicId) {
return !topicId;
},
actions: {
send() {
if (this.get('sendDisabled')) return;
send: function(){
if(this.get('sendDisabled')){return;};
self = this;
this.set('loading', true);
ajax("/admin/plugins/chat/test", {
data: { channel_id: this.get('model.channel.id'),
topic_id: this.get('model.topic_id')
},
data: {
channel_id: this.get('model.channel.id'),
topic_id: this.get('model.topic_id')
},
type: 'POST'
}).then(function () {
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';
if(response['error_key']){
error_key = response['error_key'];
}
self.flash(I18n.t(error_key), 'error');
});
}).then(() => {
this.set('loading', false);
this.flash(I18n.t('chat_integration.test_modal.success'), 'success');
}.catch(popupAjaxError);
}
}
});

View File

@ -1,14 +1,11 @@
import RestModel from 'discourse/models/rest';
export default RestModel.extend({
updateProperties() {
var prop_names = ['data'];
return this.getProperties(prop_names);
return this.getProperties(['data']);
},
createProperties() {
var prop_names = ['provider','data'];
return this.getProperties(prop_names);
return this.getProperties(['provider','data']);
}
});

View File

@ -1,6 +1,6 @@
import RestModel from 'discourse/models/rest';
import Category from 'discourse/models/category';
import computed from "ember-addons/ember-computed-decorators";
import { default as computed, observes } from "ember-addons/ember-computed-decorators";
export default RestModel.extend({
available_filters: [
@ -22,21 +22,22 @@ export default RestModel.extend({
type: 'normal',
error_key: null,
@observes('type')
removeUnneededInfo() {
const type = this.get('type');
removeUnneededInfo: function(){
const type=this.get('type');
if(type==='normal'){
if (type === 'normal') {
this.set('group_id', null);
}else{
} else {
this.set('category_id', null);
}
}.observes('type'),
},
@computed('category_id')
category(categoryId) {
if (categoryId){
return Category.findById(categoryId);
}else {
} else {
return false;
}
},
@ -47,13 +48,10 @@ export default RestModel.extend({
},
updateProperties() {
var prop_names = ['type','category_id','group_id','tags','filter'];
return this.getProperties(prop_names);
return this.getProperties(['type','category_id','group_id','tags','filter']);
},
createProperties() {
var prop_names = ['type','channel_id', 'category_id','group_id','tags','filter'];
return this.getProperties(prop_names);
return this.getProperties(['type','channel_id', 'category_id','group_id','tags','filter']);
}
});

View File

@ -1,8 +1,7 @@
export default Discourse.Route.extend({
afterModel(model) {
if(model.totalRows > 0){
this.transitionTo('adminPlugins.chat.provider', model.get('firstObject').name);
}
afterModel(model) {
if (model.totalRows > 0) {
this.transitionTo('adminPlugins.chat.provider', model.get('firstObject').name);
}
}
});

View File

@ -17,17 +17,18 @@ export default Discourse.Route.extend({
return rule;
}));
});
return value;
});
},
serialize: function(model) {
return { provider: model['provider'].get('id')};
serialize(model) {
return { provider: model['provider'].get('id') };
},
actions: {
closeModal: function(){
if(this.get('controller.modalShowing')){
closeModal() {
if (this.get('controller.modalShowing')) {
this.refresh();
this.set('controller.modalShowing', false);
}
@ -35,9 +36,8 @@ export default Discourse.Route.extend({
return true; // Continue bubbling up, so the modal actually closes
},
refresh: function(){
refresh() {
this.refresh();
}
}
});

View File

@ -1,13 +1,13 @@
export default Discourse.Route.extend({
model() {
model() {
return this.store.findAll('provider');
},
actions: {
showSettings: function(){
this.transitionTo('adminSiteSettingsCategory', 'plugins', {
queryParams: { filter: 'chat_integration'}
});
}
}
showSettings() {
this.transitionTo('adminSiteSettingsCategory', 'plugins', {
queryParams: { filter: 'chat_integration'}
});
}
}
});

View File

@ -3,10 +3,10 @@
<form {{action "save" on="submit"}}>
<table>
<tr class="input">
<tr class="input">
<td class="label"><label for='provider'>{{i18n "chat_integration.edit_channel_modal.provider"}}</label></td>
<td>
{{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}}
{{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}}
</td>
</tr>
<tr class="instructions">
@ -14,13 +14,13 @@
<td></td>
</tr>
{{# each model.provider.channel_parameters as |param|}}
{{#each model.provider.channel_parameters as |param|}}
<tr class="input">
<td class="label"><label for='param-{{param.key}}'>{{i18n (concat 'chat_integration.provider.' model.channel.provider '.param.' param.key '.title')}}</label></td>
<td>
{{text-field
name=(concat 'param-' param.key)
value=(mut (get model.channel.data param.key))
{{text-field
name=(concat 'param-' param.key)
value=(mut (get model.channel.data param.key))
}}
&nbsp;
@ -41,9 +41,15 @@
{{/d-modal-body}}
<div class="modal-footer">
{{d-button id="save-channel"
class='btn-primary btn-large'
action="save"
title="chat_integration.edit_channel_modal.save"
label="chat_integration.edit_channel_modal.save"
disabled=saveDisabled}}
{{d-button id="save_channel" class='btn-primary btn-large' action="save" title="chat_integration.edit_channel_modal.save" label="chat_integration.edit_channel_modal.save" disabled=saveDisabled}}
{{d-button class="btn-large" action="cancel" title="chat_integration.edit_channel_modal.cancel" label="chat_integration.edit_channel_modal.cancel"}}
{{d-button class="btn-large"
action="cancel"
title="chat_integration.edit_channel_modal.cancel"
label="chat_integration.edit_channel_modal.cancel"}}
</div>

View File

@ -3,10 +3,10 @@
<form {{action "save" on="submit"}}>
<table>
<tr class="input">
<tr class="input">
<td class="label"><label for='provider'>{{i18n "chat_integration.edit_rule_modal.provider"}}</label></td>
<td>
{{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}}
{{i18n (concat 'chat_integration.provider.' model.channel.provider '.title')}}
</td>
</tr>
<tr class="instructions">
@ -99,9 +99,15 @@
{{/d-modal-body}}
<div class="modal-footer">
{{d-button id="save-rule"
class='btn-primary btn-large'
action="save"
title="chat_integration.edit_rule_modal.save"
label="chat_integration.edit_rule_modal.save"
disabled=saveDisabled}}
{{d-button id="save_rule" class='btn-primary btn-large' action="save" title="chat_integration.edit_rule_modal.save" label="chat_integration.edit_rule_modal.save" disabled=saveDisabled}}
{{d-button class="btn-large" action="cancel" title="chat_integration.edit_rule_modal.cancel" label="chat_integration.edit_rule_modal.cancel"}}
{{d-button class="btn-large"
action="cancel"
title="chat_integration.edit_rule_modal.cancel"
label="chat_integration.edit_rule_modal.cancel"}}
</div>

View File

@ -1,3 +1,3 @@
export default function() {
this.route('transcript', {path: '/chat-transcript/:secret'});
this.route('transcript', { path: '/chat-transcript/:secret' });
};

View File

@ -2,23 +2,20 @@ import { ajax } from 'discourse/lib/ajax';
import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Discourse.Route.extend({
beforeModel: function(transition) {
beforeModel(transition) {
if (this.currentUser) {
const secret = transition.params.transcript.secret;
if (Discourse.User.current()) {
var secret = transition.params.transcript.secret;
// User is logged in
this.replaceWith('discovery.latest').then(e => {
if (this.controllerFor('navigation/default').get('canCreateTopic')) {
// User can create topic
Ember.run.next(() => {
ajax("/chat-transcript/"+secret).then(result => {
ajax(`chat-transcript/${secret}`).then(result => {
e.send('createNewTopicViaParams', null, result['content'], null, null, null);
}, popupAjaxError);
});
}
});
} else {
// User is not logged in
this.session.set("shouldRedirectToUrl", window.location.href);
this.replaceWith('login');
}

View File

@ -1,27 +1,29 @@
{{#if anyErrors}}
<div class="error">
<i class="fa fa-exclamation-triangle"></i>
<span class="error-message">{{i18n "chat_integration.channels_with_errors"}}</span>
</div>
{{/if}}
{{# each model.channels as |channel|}}
{{channel-details
channel=channel
provider=model.provider
store=store
refresh='refresh'
edit='editChannel'
test='testChannel'
createRule='createRule'
editRule='editRule'
}}
{{/each}}
<div class="table-footer">
<div class="pull-right">
{{d-button id="create_channel" action="createChannel" actionParam=model.provider icon="plus" title="chat_integration.create_channel" label="chat_integration.create_channel"}}
</div>
{{#if anyErrors}}
<div class="error">
<i class="fa fa-exclamation-triangle"></i>
<span class="error-message">{{i18n "chat_integration.channels_with_errors"}}</span>
</div>
{{/if}}
{{#each model.channels as |channel|}}
{{channel-details
channel=channel
provider=model.provider
store=store
refresh='refresh'
edit='editChannel'
test='testChannel'
createRule='createRule'
editRule='editRule'}}
{{/each}}
<div class="table-footer">
<div class="pull-right">
{{d-button id="create-channel"
action="createChannel"
actionParam=model.provider
icon="plus"
title="chat_integration.create_channel"
label="chat_integration.create_channel"}}
</div>
</div>

View File

@ -1,27 +1,25 @@
<div id="admin-plugin-chat">
<div class="admin-controls">
<div class="span15">
<ul class="nav nav-pills">
{{#each model as |provider|}}
{{nav-item route='adminPlugins.chat.provider' routeParam=provider.name label=(concat 'chat_integration.provider.' provider.name '.title')}}
{{/each}}
</ul>
</div>
<div class="admin-controls">
<div class="span15">
<ul class="nav nav-pills">
{{#each model as |provider|}}
{{nav-item route='adminPlugins.chat.provider' routeParam=provider.name label=(concat 'chat_integration.provider.' provider.name '.title')}}
{{/each}}
</ul>
</div>
<div class="pull-right">
{{#d-button
action="showSettings"
icon="gear"
title="chat_integration.settings"
label="chat_integration.settings"}}
{{/d-button}}
</div>
</div>
{{#if model.totalRows}}
{{else}}
{{i18n "chat_integration.no_providers"}}
{{/if}}
<div class="pull-right">
{{#d-button
action="showSettings"
icon="gear"
title="chat_integration.settings"
label="chat_integration.settings"}}
</div>
</div>
{{outlet}}
{{#unless model.totalRows}}
{{i18n "chat_integration.no_providers"}}
{{/unless}}
{{outlet}}
</div>

View File

@ -1,7 +1,7 @@
{{# each provider.channel_parameters as |param|}}
{{#if param.hidden}}{{else}}
{{#each provider.channel_parameters as |param|}}
{{#unless param.hidden}}
<span class='field-name'>{{i18n (concat 'chat_integration.provider.' channel.provider '.param.' param.key '.title')}}:</span>
<span class='field-value'>{{get channel.data param.key}}</span>
<br/>
{{/if}}
{{/unless}}
{{/each}}

View File

@ -6,44 +6,41 @@
{{d-button class='cancel' action="delete" actionParam=channel icon="trash" title="chat_integration.delete_channel" label="chat_integration.delete_channel"}}
</div>
<span class='channel-title'>
{{#if channel.error_key}}
{{d-button action="showError" actionParam=channel.error_key class="delete btn-danger" icon="exclamation-triangle"}}
{{/if}}
{{channel-data provider=provider channel=channel}}
</span>
</div>
<div class='channel-body'>
<table>
<tr>
{{!-- <th></th> --}}
<th>{{i18n "chat_integration.rule_table.filter"}}</th>
<table>
<tr>
<th>{{i18n "chat_integration.rule_table.filter"}}</th>
<th>{{i18n "chat_integration.rule_table.category"}}</th>
<th>{{i18n "chat_integration.rule_table.category"}}</th>
{{#if siteSettings.tagging_enabled}}
<th>{{i18n "chat_integration.rule_table.tags"}}</th>
{{/if}}
{{#if siteSettings.tagging_enabled}}
<th>{{i18n "chat_integration.rule_table.tags"}}</th>
{{/if}}
<th></th>
</tr>
<th></th>
</tr>
{{#each channel.rules as |rule|}}
{{rule-row rule=rule edit='editRule' refresh='refresh'}}
{{/each}}
</table>
{{#each channel.rules as |rule|}}
{{rule-row rule=rule edit='editRule' refresh='refresh'}}
{{/each}}
</table>
</div>
<div class='channel-footer'>
<div class='pull-right'>
{{d-button action="createRule" actionParam=channel icon="plus" title="chat_integration.create_rule" label="chat_integration.create_rule"}}
{{d-button action="createRule"
actionParam=channel
icon="plus"
title="chat_integration.create_rule"
label="chat_integration.create_rule"}}
</div>
</div>

View File

@ -1,4 +1,3 @@
<td>
{{rule.filterName}}
</td>
@ -31,5 +30,4 @@
<td>
{{d-button action="edit" actionParam=rule icon="pencil" class="edit" title="chat_integration.rule_table.edit_rule"}}
{{d-button action="delete" actionParam=rule icon="trash-o" class="delete" title="chat_integration.rule_table.delete_rule"}}
</td>

View File

@ -1,97 +1,96 @@
#admin-plugin-chat{
#admin-plugin-chat {
table{
margin-top:0;
td:last-child{
white-space:nowrap;
}
td:not(:last-child){
width: 30%;
}
}
table {
margin-top:0;
div.table-footer{
margin: 10px;
}
td:last-child {
white-space:nowrap;
}
div.error {
font-size: 1.1em;
font-weight:bold;
max-width: 100%;
td:not(:last-child) {
width: 30%;
}
}
div.table-footer {
margin: 10px;
}
div.error {
font-size: 1.1em;
font-weight:bold;
max-width: 100%;
margin-top: 10px;
margin-bottom: 10px;
background-color: $danger-low;
padding: 15px;
}
div.channel-details{
margin: 20px 10px;
div.channel-details {
margin: 20px 10px;
border: 1px solid $primary-low;
border: 1px solid $primary-low;
div.channel-header {
background: $primary-low;
padding: 10px;
overflow:auto;
div.channel-header{
background: $primary-low;
padding: 10px;
overflow:auto;
.channel-title{
.channel-title {
font-size: 1.3em;
.field-name{
font-weight: bold;
.field-name {
font-weight: bold;
}
}
}
div.channel-footer{
overflow:auto;
}
}
}
#chat_integration_edit_channel_modal, #chat_integration_test_modal, #chat_integration_edit_rule_modal{
table{
width:100%;
tr.input td{
padding-top: 10px;
&.label{
width: 100px;
label{
margin-bottom: 0px;
font-weight: bold;
}
}
}
tr.instructions label{
color: $primary-medium;
}
}
#channel-field{
width: 200px;
margin-bottom: 0px;
box-shadow: none;
}
.tag-chooser{
ul.select2-choices{
border: none;
background:none;
}
margin-bottom: 0px;
margin-top: 0px;
}
.field-name{
font-weight: bold;
}
}
}
div.channel-footer {
overflow:auto;
}
}
}
#chat_integration_edit_channel_modal,
#chat_integration_test_modal,
#chat_integration_edit_rule_modal {
table {
width:100%;
tr.input td {
padding-top: 10px;
&.label {
width: 100px;
label {
margin-bottom: 0px;
font-weight: bold;
}
}
}
tr.instructions label {
color: $primary-medium;
}
}
#channel-field {
width: 200px;
margin-bottom: 0px;
box-shadow: none;
}
.tag-chooser {
ul.select2-choices {
border: none;
background:none;
}
margin-bottom: 0px;
margin-top: 0px;
}
.field-name {
font-weight: bold;
}
}

View File

@ -22,11 +22,11 @@ module DiscourseChat
end
def self.provider_names
self.providers.map { |x| x::PROVIDER_NAME }
self.providers.map! { |x| x::PROVIDER_NAME }
end
def self.enabled_provider_names
self.enabled_providers.map { |x| x::PROVIDER_NAME }
self.enabled_providers.map! { |x| x::PROVIDER_NAME }
end
def self.get_by_name(name)
@ -71,7 +71,7 @@ module DiscourseChat
engines = []
DiscourseChat::Provider.providers.each do |provider|
engine = provider.constants.select do |constant|
constant.to_s =~ (/Engine$/) && (not constant.to_s == "HookEngine")
constant.to_s =~ (/Engine$/) && (constant.to_s != "HookEngine")
end.map(&provider.method(:const_get)).first
if engine

View File

@ -3,10 +3,11 @@ module DiscourseChat
module DiscordProvider
PROVIDER_NAME = "discord".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_discord_enabled
CHANNEL_PARAMETERS = [
{ key: "name", regex: '^\S+' },
{ key: "webhook_url", regex: '^https:\/\/discordapp\.com\/api\/webhooks\/', unique: true, hidden: true }
]
{ key: "name", regex: '^\S+' },
{ key: "webhook_url", regex: '^https:\/\/discordapp\.com\/api\/webhooks\/', unique: true, hidden: true }
].freeze
def self.send_message(url, message)
http = Net::HTTP.new("discordapp.com", 443)
@ -18,12 +19,12 @@ module DiscourseChat
req.body = message.to_json
response = http.request(req)
return response
response
end
def self.ensure_protocol(url)
return url if not url.start_with?('//')
return 'http:' + url
return url if !url.start_with?('//')
"http:#{url}"
end
def self.generate_discord_message(post)
@ -49,22 +50,20 @@ module DiscourseChat
}]
}
return message
message
end
def self.trigger_notification(post, channel)
# Adding ?wait=true means that we actually get a success/failure response, rather than returning asynchronously
webhook_url = channel.data['webhook_url'] + '?wait=true'
webhook_url = "#{channel.data['webhook_url']}?wait=true"
message = generate_discord_message(post)
response = send_message(webhook_url, message)
if not response.kind_of? Net::HTTPSuccess
error_key = nil
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body }
if !response.kind_of?(Net::HTTPSuccess)
raise ::DiscourseChat::ProviderError.new(info: {
error_key: nil, message: message, response_body: response.body
})
end
end
end

View File

@ -81,7 +81,7 @@ module DiscourseChat
response = send_message(webhook_url, message)
if not response.kind_of? Net::HTTPSuccess
if !response.kind_of?(Net::HTTPSuccess)
error_key = nil
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body }
end

View File

@ -59,7 +59,7 @@ module DiscourseChat
response = send_message(channel.data['room_id'], message)
if not response.kind_of? Net::HTTPSuccess
if !response.kind_of?(Net::HTTPSuccess)
error_key = nil
begin
responseData = JSON.parse(response.body)

View File

@ -18,7 +18,7 @@ module DiscourseChat
response = self.do_api_request('setWebhook', message)
if not response['ok'] == true
if response['ok'] != true
# If setting up webhook failed, disable provider
SiteSetting.chat_integration_telegram_enabled = false
Rails.logger.error("Failed to setup telegram webhook. Message data= " + message.to_json + " response=" + response.to_json)
@ -89,7 +89,7 @@ module DiscourseChat
response = sendMessage(message)
if not response['ok'] == true
if response['ok'] != true
error_key = nil
if response['description'].include? 'chat not found'
error_key = 'chat_integration.provider.telegram.errors.channel_not_found'

View File

@ -53,7 +53,7 @@ module DiscourseChat
response = send_message(message)
if not response.kind_of? Net::HTTPSuccess
if !response.kind_of?(Net::HTTPSuccess)
error_key = nil
error_key = 'chat_integration.provider.zulip.errors.does_not_exist' if response.body.include?('does not exist')
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_code: response.code, response_body: response.body }

View File

@ -114,9 +114,9 @@ describe 'Chat Controller', type: :request do
end
it 'should fail for invalid provider' do
get '/admin/plugins/chat/channels.json', params: { provider: 'someprovider' }
expect(response).not_to be_success
expect do
get '/admin/plugins/chat/channels.json', params: { provider: 'someprovider' }
end.to raise_error(Discourse::InvalidParameters)
end
end

View File

@ -13,6 +13,7 @@ RSpec.shared_context "dummy provider" do
if @@raise_exception
raise @@raise_exception
end
@@sent_messages.push(post: post.id, channel: channel)
end

View File

@ -10,41 +10,49 @@ acceptance("Chat Integration", {
object
];
};
server.get('/admin/plugins/chat/providers', () => { // eslint-disable-line no-undef
return response({ providers: [{name: 'dummy', id:'dummy',channel_parameters:[{key:'somekey', regex:"^\\S+$"}]}] });
});
server.get('/admin/plugins/chat/channels', () => { // eslint-disable-line no-undef
return response({"channels":[{"id":97,"provider":"dummy","data":{somekey:"#general"},"rules":[
{"id":98,"channel_id":97,"category_id":null,"team_id":null,"type":"normal","tags":[],"filter":"watch","error_key":null}
]}]});
});
server.post('/admin/plugins/chat/channels', () => { // eslint-disable-line no-undef
return response({ });
});
server.put('/admin/plugins/chat/channels/:id', () => { // eslint-disable-line no-undef
return response({ });
});
server.delete('/admin/plugins/chat/channels/:id', () => { // eslint-disable-line no-undef
return response({ });
});
server.post('/admin/plugins/chat/rules', () => { // eslint-disable-line no-undef
return response({ });
});
server.put('/admin/plugins/chat/rules/:id', () => { // eslint-disable-line no-undef
return response({ });
});
server.delete('/admin/plugins/chat/rules/:id', () => { // eslint-disable-line no-undef
return response({ });
});
server.post('/admin/plugins/chat/test', () => { // eslint-disable-line no-undef
return response({ });
});
server.get('/groups/search.json', () => { // eslint-disable-line no-undef
return response([]);
});
}
});
test("Rules load successfully", assert => {
@ -60,21 +68,21 @@ test("Create channel works", assert => {
visit("/admin/plugins/chat");
andThen(() => {
click('#create_channel');
click('#create-channel');
});
andThen(() => {
assert.ok(exists('#chat_integration_edit_channel_modal'), 'it displays the modal');
assert.ok(find('#save_channel').prop('disabled'), 'it disables the save button');
assert.ok(find('#save-channel').prop('disabled'), 'it disables the save button');
fillIn('#chat_integration_edit_channel_modal input', '#general');
});
andThen(() => {
assert.ok(find('#save_channel').prop('disabled') === false, 'it enables the save button');
assert.ok(find('#save-channel').prop('disabled') === false, 'it enables the save button');
});
andThen(() => {
click('#save_channel');
click('#save-channel');
});
andThen(() => {
@ -92,12 +100,12 @@ test("Edit channel works", assert => {
andThen(() => {
assert.ok(exists('#chat_integration_edit_channel_modal'), 'it displays the modal');
assert.ok(!find('#save_channel').prop('disabled'), 'save is enabled');
assert.ok(!find('#save-channel').prop('disabled'), 'save is enabled');
fillIn('#chat_integration_edit_channel_modal input', ' general');
});
andThen(() => {
assert.ok(find('#save_channel').prop('disabled'), 'it disables the save button');
assert.ok(find('#save-channel').prop('disabled'), 'it disables the save button');
});
andThen(() => {
@ -125,10 +133,10 @@ test("Create rule works", assert => {
andThen(() => {
assert.ok(exists('#chat_integration_edit_rule_modal'), 'modal opens on edit');
assert.ok(find('#save_rule').prop('disabled') === false, 'save is enabled');
assert.ok(find('#save-rule').prop('disabled') === false, 'save is enabled');
});
click('#save_rule');
click('#save-rule');
andThen(() => {
assert.ok(!exists('#chat_integration_edit_rule_modal'), 'modal closes on save');
@ -146,10 +154,10 @@ test("Edit rule works", assert => {
andThen(() => {
assert.ok(exists('#chat_integration_edit_rule_modal'), 'modal opens on edit');
assert.ok(find('#save_rule').prop('disabled') === false, 'it enables the save button');
assert.ok(find('#save-rule').prop('disabled') === false, 'it enables the save button');
});
click('#save_rule');
click('#save-rule');
andThen(() => {
assert.ok(!exists('#chat_integration_edit_rule_modal'), 'modal closes on save');