Skip to content

Commit

Permalink
Merge branch 'develop' into feat/campaigns-content-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsivin committed Aug 2, 2021
2 parents 0926435 + 9f3f238 commit 18358ab
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 47 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Metrics/ClassLength:
- 'app/models/conversation.rb'
- 'app/mailers/conversation_reply_mailer.rb'
- 'app/models/message.rb'
- 'app/builders/messages/facebook/message_builder.rb'
RSpec/ExampleLength:
Max: 25
Style/Documentation:
Expand Down
8 changes: 8 additions & 0 deletions app/builders/messages/facebook/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ def initialize(response, inbox, outgoing_echo: false)
end

def perform
# This channel might require reauthorization, may be owner might have changed the fb password
return if @inbox.channel.reauthorization_required?

ActiveRecord::Base.transaction do
build_contact
build_message
end
rescue Koala::Facebook::AuthenticationError
Rails.logger.info "Facebook Authorization expired for Inbox #{@inbox.id}"
rescue StandardError => e
Raven.capture_exception(e)
true
Expand Down Expand Up @@ -136,6 +141,9 @@ def contact_params
begin
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
result = k.get_object(@sender_id) || {}
rescue Koala::Facebook::AuthenticationError
@inbox.channel.authorization_error!
raise
rescue StandardError => e
result = {}
Raven.capture_exception(e)
Expand Down
4 changes: 2 additions & 2 deletions app/finders/conversation_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def set_count_for_all_conversations
end

def current_page
params[:page]
params[:page] || 1
end

def conversations
@conversations = @conversations.includes(
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team
)
current_page ? @conversations.latest.page(current_page) : @conversations.latest
@conversations.latest.page(current_page)
end
end
27 changes: 27 additions & 0 deletions app/javascript/dashboard/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
<transition name="fade" mode="out-in">
<router-view></router-view>
</transition>
<add-account-modal
:show="showAddAccountModal"
:has-accounts="hasAccounts"
/>
<woot-snackbar-box />
</div>
</template>

<script>
import { mapGetters } from 'vuex';
import AddAccountModal from '../dashboard/components/layout/sidebarComponents/AddAccountModal';
import WootSnackbarBox from './components/SnackbarContainer';
import { accountIdFromPathname } from './helper/URLHelper';
Expand All @@ -17,14 +22,36 @@ export default {
components: {
WootSnackbarBox,
AddAccountModal,
},
data() {
return {
showAddAccountModal: false,
};
},
computed: {
...mapGetters({
getAccount: 'accounts/getAccount',
currentUser: 'getCurrentUser',
}),
hasAccounts() {
return (
this.currentUser &&
this.currentUser.accounts &&
this.currentUser.accounts.length !== 0
);
},
},
watch: {
currentUser() {
if (!this.hasAccounts) {
this.showAddAccountModal = true;
}
},
},
mounted() {
this.$store.dispatch('setUser');
this.setLocale(window.chatwootConfig.selectedLocale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
:header-title="$t('CREATE_ACCOUNT.NEW_ACCOUNT')"
:header-content="$t('CREATE_ACCOUNT.SELECTOR_SUBTITLE')"
/>
<div v-if="!hasAccounts" class="alert-wrap">
<div class="callout alert">
<div class="icon-wrap">
<i class="ion-alert-circled"></i>
</div>
{{ $t('CREATE_ACCOUNT.NO_ACCOUNT_WARNING') }}
</div>
</div>

<form class="row" @submit.prevent="addAccount">
<div class="medium-12 columns">
Expand Down Expand Up @@ -53,6 +61,10 @@ export default {
type: Boolean,
default: false,
},
hasAccounts: {
type: Boolean,
default: true,
},
},
data() {
return {
Expand Down Expand Up @@ -90,3 +102,19 @@ export default {
},
};
</script>
<style lang="scss">
.alert-wrap {
margin: var(--space-zero) var(--space-large);
margin-top: var(--space-medium);
.callout {
display: flex;
justify-content: center;
}
}
.icon-wrap {
font-size: var(--font-size-big);
margin-left: var(--space-smaller);
margin-right: var(--space-slab);
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export default {
text_content: { full: fullTextContent, reply: replyTextContent } = {},
} = {},
} = this.contentAttributes;
let contentToBeParsed =
replyHTMLContent ||
replyTextContent ||
Expand All @@ -154,7 +153,7 @@ export default {
if (contentToBeParsed && this.isIncoming) {
const parsedContent = this.stripStyleCharacters(contentToBeParsed);
if (parsedContent) {
return parsedContent;
return parsedContent.replace(/\n/g, '<br />');
}
}
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default {
created() {
bus.$on('scrollToMessage', () => {
setTimeout(() => this.scrollToBottom(), 0);
this.$nextTick(() => this.scrollToBottom());
this.makeMessagesRead();
});
Expand All @@ -255,7 +255,7 @@ export default {
this.conversationPanel = this.$el.querySelector('.conversation-panel');
this.setScrollParams();
this.conversationPanel.addEventListener('scroll', this.handleScroll);
this.scrollToBottom();
this.$nextTick(() => this.scrollToBottom());
this.isLoadingPrevious = false;
},
removeScrollListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
export default {
props: {
emailAttributes: {
type: Array,
type: Object,
default: () => ({}),
},
isIncoming: {
Expand Down
1 change: 1 addition & 0 deletions app/javascript/dashboard/i18n/locale/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"CSAT": "CSAT"
},
"CREATE_ACCOUNT": {
"NO_ACCOUNT_WARNING": "Uh oh! We could not find any Chatwoot accounts. Please create a new account to continue.",
"NEW_ACCOUNT": "New Account",
"SELECTOR_SUBTITLE": "Create a new account",
"API": {
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/shared/helpers/MessageFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MessageFormatter {
const markedDownOutput = marked(withHash);
return markedDownOutput;
}
return marked(this.message);
return marked(this.message, { breaks: true, gfm: true });
}

get formattedMessage() {
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/widget/components/UserMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,9 @@ export default {
margin-top: $space-normal;
}
}
p:not(:last-child) {
margin-bottom: $space-normal;
}
}
</style>
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
def slack_disconnect(account)
def slack_disconnect
return unless smtp_config_set_or_development?

emails = account.administrators.pluck(:email)
subject = 'Your Slack integration has expired'
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{account.id}/settings/integrations/slack"
send_mail_with_liquid(to: emails, subject: subject) and return
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{Current.account.id}/settings/integrations/slack"
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end

def facebook_disconnect(inbox)
return unless smtp_config_set_or_development?

subject = 'Your Facebook page connection has expired'
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{Current.account.id}/settings/inboxes/#{inbox.id}"
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end

private

def admin_emails
Current.account.administrators.pluck(:email)
end
end
8 changes: 6 additions & 2 deletions app/models/concerns/reauthorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ def authorization_error!
# could used to manually prompt reauthorization if auth scope changes
def prompt_reauthorization!
::Redis::Alfred.set(reauthorization_required_key, true)
return unless (is_a? Integrations::Hook) && slack?

AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect(account)&.deliver_later
if (is_a? Integrations::Hook) && slack?
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect.deliver_later
end
return unless is_a? Channel::FacebookPage

AdministratorNotifications::ChannelNotificationsMailer.with(account: account).facebook_disconnect(inbox).deliver_later
end

# call this after you successfully Reauthorized the object in UI
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Hello,</p>

<p>Your Facebook Inbox Access has expired. </p>
<p>Please reconnect Facebook Page to continue receiving messages in Chatwoot</p>

<p>
Click <a href="{{action_url}}">here</a> to re-connect.
</p>
2 changes: 1 addition & 1 deletion spec/actions/contact_merge_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
it 'does not delete contact' do
mergee_contact = base_contact
contact_merge
expect { mergee_contact.reload }.not_to raise_error(ActiveRecord::RecordNotFound)
expect(mergee_contact.reload).not_to eq nil
end
end

Expand Down
29 changes: 17 additions & 12 deletions spec/builders/messages/facebook/message_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
let!(:incoming_fb_text_message) { Integrations::Facebook::MessageParser.new(message_object) }
let(:fb_object) { double }

before do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return(
{
first_name: 'Jane',
last_name: 'Dae',
account_id: facebook_channel.inbox.account_id,
profile_pic: 'https://via.placeholder.com/250x250.png'
}.with_indifferent_access
)
end

describe '#perform' do
it 'creates contact and message for the facebook inbox' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return(
{
first_name: 'Jane',
last_name: 'Dae',
account_id: facebook_channel.inbox.account_id,
profile_pic: 'https://via.placeholder.com/250x250.png'
}.with_indifferent_access
)
message_builder

contact = facebook_channel.inbox.contacts.first
Expand All @@ -30,5 +27,13 @@
expect(contact.name).to eq('Jane Dae')
expect(message.content).to eq('facebook message')
end

it 'increments channel authorization_error_count when error is thrown' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::AuthenticationError.new(500, 'Error validating access token'))
message_builder

expect(facebook_channel.authorization_error_count).to eq(1)
end
end
end
4 changes: 2 additions & 2 deletions spec/controllers/api/v1/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@

context 'when ENABLE_ACCOUNT_SIGNUP env variable is set to api_only' do
it 'does not respond 404 on requests' do
params = { account_name: 'test', email: email, user_full_name: user_full_name }
params = { account_name: 'test', email: email, user_full_name: user_full_name, password: 'Password1!' }
ENV['ENABLE_ACCOUNT_SIGNUP'] = 'api_only'

post api_v1_accounts_url,
params: params,
as: :json

expect(response).not_to have_http_status(:not_found)
expect(response).to have_http_status(:success)
ENV['ENABLE_ACCOUNT_SIGNUP'] = nil
end
end
Expand Down
Loading

0 comments on commit 18358ab

Please sign in to comment.