From 2f63ebb8a60d2e313ff5b2b80219dd751522b157 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Wed, 8 Dec 2021 22:20:28 -0800 Subject: [PATCH] fix: Add fixes for sentry errors (#3522) - Add fixes for sentry errors --- .../api/v1/widget/contacts_controller.rb | 15 +- app/drops/conversation_drop.rb | 2 +- app/drops/message_drop.rb | 4 +- app/helpers/message_format_helper.rb | 7 + .../widgets/conversation/MoreActions.vue | 1 - app/javascript/sdk/IFrameHelper.js | 4 + .../v1/widget/contacts/update.json.jbuilder | 3 + app/views/layouts/mailer/base.liquid | 178 ++++++++---------- .../conversation_mention.liquid | 8 +- .../api/v1/widget/contacts_controller_spec.rb | 4 +- spec/helpers/message_format_helper_spec.rb | 8 + 11 files changed, 124 insertions(+), 110 deletions(-) create mode 100644 app/views/api/v1/widget/contacts/update.json.jbuilder diff --git a/app/controllers/api/v1/widget/contacts_controller.rb b/app/controllers/api/v1/widget/contacts_controller.rb index eafc220a3..d745c4153 100644 --- a/app/controllers/api/v1/widget/contacts_controller.rb +++ b/app/controllers/api/v1/widget/contacts_controller.rb @@ -1,5 +1,5 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController - before_action :process_hmac + before_action :process_hmac, only: [:update] def show; end @@ -8,7 +8,7 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController contact: @contact, params: permitted_params.to_h.deep_symbolize_keys ) - render json: contact_identify_action.perform + @contact = contact_identify_action.perform end # TODO : clean up this with proper routes delete contacts/custom_attributes @@ -21,13 +21,22 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController private def process_hmac - return if params[:identifier_hash].blank? && !@web_widget.hmac_mandatory + return unless should_verify_hmac? render json: { error: 'HMAC failed: Invalid Identifier Hash Provided' }, status: :unauthorized unless valid_hmac? @contact_inbox.update(hmac_verified: true) end + def should_verify_hmac? + return false if params[:identifier_hash].blank? && !@web_widget.hmac_mandatory + + # Taking an extra caution that the hmac is triggered whenever identifier is present + return false if params[:custom_attributes].present? && params[:identifier].blank? + + true + end + def valid_hmac? params[:identifier_hash] == OpenSSL::HMAC.hexdigest( 'sha256', diff --git a/app/drops/conversation_drop.rb b/app/drops/conversation_drop.rb index b24586c30..ffd664659 100644 --- a/app/drops/conversation_drop.rb +++ b/app/drops/conversation_drop.rb @@ -13,7 +13,7 @@ class ConversationDrop < BaseDrop @obj.try(:recent_messages).map do |message| { 'sender' => message_sender_name(message.sender), - 'content' => transform_user_mention_content(message.content), + 'content' => render_message_content(transform_user_mention_content(message.content)), 'attachments' => message.attachments.map(&:file_url) } end diff --git a/app/drops/message_drop.rb b/app/drops/message_drop.rb index cbb3f1782..3423f0623 100644 --- a/app/drops/message_drop.rb +++ b/app/drops/message_drop.rb @@ -6,7 +6,7 @@ class MessageDrop < BaseDrop end def text_content - content = @obj.try(:content) - transform_user_mention_content content + content = @obj.try(:content) || '' + render_message_content(transform_user_mention_content(content)) end end diff --git a/app/helpers/message_format_helper.rb b/app/helpers/message_format_helper.rb index e27e61fce..3dd8d8f23 100644 --- a/app/helpers/message_format_helper.rb +++ b/app/helpers/message_format_helper.rb @@ -1,6 +1,13 @@ module MessageFormatHelper include RegexHelper + def transform_user_mention_content(message_content) message_content.gsub(MENTION_REGEX, '\1') end + + def render_message_content(message_content) + # rubocop:disable Rails/OutputSafety + CommonMarker.render_html(message_content).html_safe + # rubocop:enable Rails/OutputSafety + end end diff --git a/app/javascript/dashboard/components/widgets/conversation/MoreActions.vue b/app/javascript/dashboard/components/widgets/conversation/MoreActions.vue index 8882f7919..a166f7b5d 100644 --- a/app/javascript/dashboard/components/widgets/conversation/MoreActions.vue +++ b/app/javascript/dashboard/components/widgets/conversation/MoreActions.vue @@ -85,7 +85,6 @@ export default { }, toggleEmailActionsModal() { this.showEmailActionsModal = !this.showEmailActionsModal; - this.hideConversationActions(); }, }, }; diff --git a/app/javascript/sdk/IFrameHelper.js b/app/javascript/sdk/IFrameHelper.js index 21cd923ff..ff9bcca91 100644 --- a/app/javascript/sdk/IFrameHelper.js +++ b/app/javascript/sdk/IFrameHelper.js @@ -199,6 +199,10 @@ export const IFrameHelper = { }, handleNotificationDot: event => { + if (window.$chatwoot.hideMessageBubble) { + return; + } + const bubbleElement = document.querySelector('.woot-widget-bubble'); if ( event.unreadMessageCount > 0 && diff --git a/app/views/api/v1/widget/contacts/update.json.jbuilder b/app/views/api/v1/widget/contacts/update.json.jbuilder new file mode 100644 index 000000000..9853ccbee --- /dev/null +++ b/app/views/api/v1/widget/contacts/update.json.jbuilder @@ -0,0 +1,3 @@ +json.id @contact.id +json.name @contact.name +json.email @contact.email diff --git a/app/views/layouts/mailer/base.liquid b/app/views/layouts/mailer/base.liquid index e836cdbb6..5fa07e139 100644 --- a/app/views/layouts/mailer/base.liquid +++ b/app/views/layouts/mailer/base.liquid @@ -1,112 +1,94 @@ - - - + + - - + - - - - - - - - + +
-
- - - - -
- - - {{ content_for_layout }} -
-
- -
-
+ + +
+
+ + + + +
+ + {{ content_for_layout }} +
+
+
+ +
- - + diff --git a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid index 9a30e6e09..00cb13e1e 100644 --- a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid +++ b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid @@ -5,6 +5,7 @@ {{message.text_content}} +

Previous messages:

{% for chat_message in conversation.recent_messages %}
{% if chat_message.sender == user.available_name %} @@ -15,7 +16,7 @@
-

+

{% if chat_message.content %} {{chat_message.content}} {% endif %} @@ -28,5 +29,6 @@

{% endfor %} - -

View Message

+

+ View Message +

diff --git a/spec/controllers/api/v1/widget/contacts_controller_spec.rb b/spec/controllers/api/v1/widget/contacts_controller_spec.rb index 854f2ab24..9364dbb3a 100644 --- a/spec/controllers/api/v1/widget/contacts_controller_spec.rb +++ b/spec/controllers/api/v1/widget/contacts_controller_spec.rb @@ -23,7 +23,7 @@ RSpec.describe '/api/v1/widget/contacts', type: :request do before do allow(ContactIdentifyAction).to receive(:new).and_return(identify_action) - allow(identify_action).to receive(:perform) + allow(identify_action).to receive(:perform).and_return(contact) end it 'calls contact identify' do @@ -47,7 +47,7 @@ RSpec.describe '/api/v1/widget/contacts', type: :request do before do allow(ContactIdentifyAction).to receive(:new).and_return(identify_action) - allow(identify_action).to receive(:perform) + allow(identify_action).to receive(:perform).and_return(contact) end it 'returns success when correct identifier hash is provided' do diff --git a/spec/helpers/message_format_helper_spec.rb b/spec/helpers/message_format_helper_spec.rb index a2edc38c0..a2e0c5f45 100644 --- a/spec/helpers/message_format_helper_spec.rb +++ b/spec/helpers/message_format_helper_spec.rb @@ -8,4 +8,12 @@ describe MessageFormatHelper, type: :helper do end end end + + describe '#render_message_content' do + context 'when render_message_content called' do + it 'render text correctly' do + expect(helper.render_message_content('Hi *there*, I am mostly text!')).to eq "

Hi there, I am mostly text!

\n" + end + end + end end