Skip to content

Commit

Permalink
fix: Update email alerts for Slack integration (#7739)
Browse files Browse the repository at this point in the history
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
  • Loading branch information
3 people committed Aug 17, 2023
1 parent ab039e1 commit 178bc80
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/javascript/dashboard/i18n/locale/en/integrations.json
Expand Up @@ -85,7 +85,8 @@
"UPDATE": "Update",
"BUTTON_TEXT": "Connect channel",
"DESCRIPTION": "Your Slack workspace is now linked with Chatwoot. However, the integration is currently inactive. To activate the integration and connect a channel to Chatwoot, please click the button below.\n\n**Note:** If you are attempting to connect a private channel, add the Chatwoot app to the Slack channel before proceeding with this step.",
"ATTENTION_REQUIRED": "Attention required"
"ATTENTION_REQUIRED": "Attention required",
"EXPIRED": "Your Slack integration has expired. To continue receiving messages on Slack, please delete the integration and connect your workspace again."
},
"UPDATE_ERROR": "There was an error updating the integration, please try again",
"UPDATE_SUCCESS": "The channel is connected successfully",
Expand Down
Expand Up @@ -21,7 +21,10 @@
/>
</div>
<div v-if="areHooksAvailable" class="p-6 flex-1">
<select-channel-warning v-if="!isIntegrationHookEnabled" />
<select-channel-warning
v-if="!isIntegrationHookEnabled"
:has-connected-a-channel="hasConnectedAChannel"
/>
<slack-integration-help-text
:selected-channel-name="selectedChannelName"
/>
Expand Down Expand Up @@ -61,16 +64,20 @@ export default {
const { hooks = [] } = this.integration || {};
return !!hooks.length;
},
isIntegrationHookEnabled() {
hook() {
const { hooks = [] } = this.integration || {};
const [hook = {}] = hooks;
return hook.status || false;
const [hook] = hooks;
return hook || {};
},
isIntegrationHookEnabled() {
return this.hook.status || false;
},
hasConnectedAChannel() {
return !!this.hook.reference_id;
},
selectedChannelName() {
const { hooks = [] } = this.integration || {};
const [hook = {}] = hooks;
if (hook.status) {
const { settings: { channel_name: channelName = '' } = {} } = hook;
if (this.hook.status) {
const { settings: { channel_name: channelName = '' } = {} } = this.hook;
return channelName || 'customer-conversations';
}
return this.$t('INTEGRATION_SETTINGS.SLACK.HELP_TEXT.SELECTED');
Expand Down
Expand Up @@ -23,7 +23,7 @@
v-dompurify-html="
formatMessage(
useInstallationName(
$t('INTEGRATION_SETTINGS.SLACK.SELECT_CHANNEL.DESCRIPTION'),
errorDescription,
globalConfig.installationName
),
false
Expand All @@ -33,7 +33,7 @@
</div>
</div>
</div>
<div class="ml-8 mt-2">
<div v-if="!hasConnectedAChannel" class="ml-8 mt-2">
<woot-submit-button
v-if="!availableChannels.length"
button-class="smooth small warning"
Expand Down Expand Up @@ -79,6 +79,12 @@ import alertMixin from 'shared/mixins/alertMixin';
export default {
mixins: [alertMixin, globalConfigMixin, messageFormatterMixin],
props: {
hasConnectedAChannel: {
type: Boolean,
default: true,
},
},
data() {
return { selectedChannelId: '', availableChannels: [] };
},
Expand All @@ -87,6 +93,11 @@ export default {
globalConfig: 'globalConfig/get',
uiFlags: 'integrations/getUIFlags',
}),
errorDescription() {
return !this.hasConnectedAChannel
? this.$t('INTEGRATION_SETTINGS.SLACK.SELECT_CHANNEL.DESCRIPTION')
: this.$t('INTEGRATION_SETTINGS.SLACK.SELECT_CHANNEL.EXPIRED');
},
},
methods: {
async fetchChannels() {
Expand Down
1 change: 1 addition & 0 deletions app/views/api/v1/models/_hook.json.jbuilder
Expand Up @@ -6,3 +6,4 @@ json.account_id resource.account_id
json.hook_type resource.hook_type

json.settings resource.settings if Current.account_user&.administrator?
json.reference_id resource.reference_id if Current.account_user&.administrator?
@@ -1,7 +1,6 @@
<p>Hello,</p>

<p>Your Slack integration has expired. </p>
<p>Please reconnect Slack to continue receiving messages on Slack</p>
<p>Your Slack integration has expired. To continue receiving messages on Slack, please delete the integration and connect your workspace again. </p>

<p>
Click <a href="{{action_url}}">here</a> to re-connect.
Expand Down
6 changes: 3 additions & 3 deletions lib/integrations/slack/send_on_slack_service.rb
Expand Up @@ -80,10 +80,10 @@ def avatar_url(sender)
def send_message
post_message if message_content.present?
upload_file if message.attachments.any?
rescue Slack::Web::Api::Errors::AccountInactive => e
rescue Slack::Web::Api::Errors::AccountInactive, Slack::Web::Api::Errors::MissingScope => e
Rails.logger.error e
hook.authorization_error!
hook.disable if hook.enabled?
hook.prompt_reauthorization!
hook.disable
end

def post_message
Expand Down
21 changes: 19 additions & 2 deletions spec/lib/integrations/slack/send_on_slack_service_spec.rb
Expand Up @@ -194,11 +194,28 @@
unfurl_links: true
).and_raise(Slack::Web::Api::Errors::AccountInactive.new('Account disconnected'))

allow(hook).to receive(:authorization_error!)
allow(hook).to receive(:prompt_reauthorization!)

builder.perform
expect(hook).to be_disabled
expect(hook).to have_received(:authorization_error!)
expect(hook).to have_received(:prompt_reauthorization!)
end

it 'disables hook on Slack MissingScope error' do
expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id,
text: message.content,
username: "#{message.sender.name} (Contact)",
thread_ts: conversation.identifier,
icon_url: anything,
unfurl_links: true
).and_raise(Slack::Web::Api::Errors::MissingScope.new('Account disconnected'))

allow(hook).to receive(:prompt_reauthorization!)

builder.perform
expect(hook).to be_disabled
expect(hook).to have_received(:prompt_reauthorization!)
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/models/integrations/hook_spec.rb
@@ -1,6 +1,9 @@
require 'rails_helper'
require Rails.root.join 'spec/models/concerns/reauthorizable_shared.rb'

RSpec.describe Integrations::Hook do
it_behaves_like 'reauthorizable'

context 'with validations' do
it { is_expected.to validate_presence_of(:app_id) }
it { is_expected.to validate_presence_of(:account_id) }
Expand Down

0 comments on commit 178bc80

Please sign in to comment.