mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	Chore: Send browser language in webwidget events (#952)
Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
		| @@ -2,12 +2,19 @@ class Api::V1::Widget::EventsController < Api::V1::Widget::BaseController | |||||||
|   include Events::Types |   include Events::Types | ||||||
|  |  | ||||||
|   def create |   def create | ||||||
|     Rails.configuration.dispatcher.dispatch(permitted_params[:name], Time.zone.now, contact_inbox: @contact_inbox) |     Rails.configuration.dispatcher.dispatch(permitted_params[:name], Time.zone.now, contact_inbox: @contact_inbox, event_info: event_info) | ||||||
|     head :no_content |     head :no_content | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   private |   private | ||||||
|  |  | ||||||
|  |   def event_info | ||||||
|  |     { | ||||||
|  |       widget_language: params[:locale], | ||||||
|  |       browser_language: browser.accept_language.first&.code | ||||||
|  |     } | ||||||
|  |   end | ||||||
|  |  | ||||||
|   def permitted_params |   def permitted_params | ||||||
|     params.permit(:name, :website_token) |     params.permit(:name, :website_token) | ||||||
|   end |   end | ||||||
|   | |||||||
| @@ -1,15 +1,8 @@ | |||||||
| import Vue from 'vue'; | import { buildSearchParamsWithLocale } from '../helpers/urlParamsHelper'; | ||||||
|  |  | ||||||
| const sendMessage = content => { | const sendMessage = content => { | ||||||
|   const locale = Vue.config.lang; |  | ||||||
|   const refererURL = window.refererURL || ''; |   const refererURL = window.refererURL || ''; | ||||||
|   let search = window.location.search; |   const search = buildSearchParamsWithLocale(window.location.search); | ||||||
|   if (search) { |  | ||||||
|     search = `${search}&locale=${locale}`; |  | ||||||
|   } else { |  | ||||||
|     search = `?locale=${locale}`; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   return { |   return { | ||||||
|     url: `/api/v1/widget/messages${search}`, |     url: `/api/v1/widget/messages${search}`, | ||||||
|     params: { |     params: { | ||||||
|   | |||||||
| @@ -1,7 +1,9 @@ | |||||||
| import { API } from 'widget/helpers/axios'; | import { API } from 'widget/helpers/axios'; | ||||||
|  | import { buildSearchParamsWithLocale } from '../helpers/urlParamsHelper'; | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   create(name) { |   create(name) { | ||||||
|     return API.post(`/api/v1/widget/events${window.location.search}`, { name }); |     const search = buildSearchParamsWithLocale(window.location.search); | ||||||
|  |     return API.post(`/api/v1/widget/events${search}`, { name }); | ||||||
|   }, |   }, | ||||||
| }; | }; | ||||||
|   | |||||||
							
								
								
									
										16
									
								
								app/javascript/widget/helpers/specs/urlParamsHelper.spec.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								app/javascript/widget/helpers/specs/urlParamsHelper.spec.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | import { buildSearchParamsWithLocale } from '../urlParamsHelper'; | ||||||
|  |  | ||||||
|  | jest.mock('vue', () => ({ | ||||||
|  |   config: { | ||||||
|  |     lang: 'el', | ||||||
|  |   }, | ||||||
|  | })); | ||||||
|  |  | ||||||
|  | describe('#buildSearchParamsWithLocale', () => { | ||||||
|  |   it('returns correct search params', () => { | ||||||
|  |     expect(buildSearchParamsWithLocale('?test=1234')).toEqual( | ||||||
|  |       '?test=1234&locale=el' | ||||||
|  |     ); | ||||||
|  |     expect(buildSearchParamsWithLocale('')).toEqual('?locale=el'); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
							
								
								
									
										10
									
								
								app/javascript/widget/helpers/urlParamsHelper.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								app/javascript/widget/helpers/urlParamsHelper.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | import Vue from 'vue'; | ||||||
|  | export const buildSearchParamsWithLocale = search => { | ||||||
|  |   const locale = Vue.config.lang; | ||||||
|  |   if (search) { | ||||||
|  |     search = `${search}&locale=${locale}`; | ||||||
|  |   } else { | ||||||
|  |     search = `?locale=${locale}`; | ||||||
|  |   } | ||||||
|  |   return search; | ||||||
|  | }; | ||||||
| @@ -56,6 +56,7 @@ class AgentBotListener < BaseListener | |||||||
|     agent_bot = inbox.agent_bot_inbox.agent_bot |     agent_bot = inbox.agent_bot_inbox.agent_bot | ||||||
|  |  | ||||||
|     payload = contact_inbox.webhook_data.merge(event: __method__.to_s) |     payload = contact_inbox.webhook_data.merge(event: __method__.to_s) | ||||||
|  |     payload[:event_info] = event.data[:event_info] | ||||||
|     AgentBotJob.perform_later(agent_bot.outgoing_url, payload) |     AgentBotJob.perform_later(agent_bot.outgoing_url, payload) | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
| @@ -38,6 +38,7 @@ class WebhookListener < BaseListener | |||||||
|     inbox = contact_inbox.inbox |     inbox = contact_inbox.inbox | ||||||
|  |  | ||||||
|     payload = contact_inbox.webhook_data.merge(event: __method__.to_s) |     payload = contact_inbox.webhook_data.merge(event: __method__.to_s) | ||||||
|  |     payload[:event_info] = event.data[:event_info] | ||||||
|     deliver_webhook_payloads(payload, inbox) |     deliver_webhook_payloads(payload, inbox) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   | |||||||
| @@ -107,10 +107,7 @@ class Conversation < ApplicationRecord | |||||||
|   end |   end | ||||||
|  |  | ||||||
|   def webhook_data |   def webhook_data | ||||||
|     { |     Conversations::EventDataPresenter.new(self).push_data | ||||||
|       display_id: display_id, |  | ||||||
|       additional_attributes: additional_attributes |  | ||||||
|     } |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def notifiable_assignee_change? |   def notifiable_assignee_change? | ||||||
|   | |||||||
							
								
								
									
										16
									
								
								db/schema.rb
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								db/schema.rb
									
									
									
									
									
								
							| @@ -13,6 +13,7 @@ | |||||||
| ActiveRecord::Schema.define(version: 2020_06_10_143132) do | ActiveRecord::Schema.define(version: 2020_06_10_143132) do | ||||||
|  |  | ||||||
|   # These are extensions that must be enabled in order to support this database |   # These are extensions that must be enabled in order to support this database | ||||||
|  |   enable_extension "pg_stat_statements" | ||||||
|   enable_extension "pgcrypto" |   enable_extension "pgcrypto" | ||||||
|   enable_extension "plpgsql" |   enable_extension "plpgsql" | ||||||
|  |  | ||||||
| @@ -205,8 +206,8 @@ ActiveRecord::Schema.define(version: 2020_06_10_143132) do | |||||||
|     t.boolean "locked", default: false |     t.boolean "locked", default: false | ||||||
|     t.jsonb "additional_attributes" |     t.jsonb "additional_attributes" | ||||||
|     t.bigint "contact_inbox_id" |     t.bigint "contact_inbox_id" | ||||||
|     t.string "identifier" |  | ||||||
|     t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false |     t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false | ||||||
|  |     t.string "identifier" | ||||||
|     t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true |     t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true | ||||||
|     t.index ["account_id"], name: "index_conversations_on_account_id" |     t.index ["account_id"], name: "index_conversations_on_account_id" | ||||||
|     t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" |     t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" | ||||||
| @@ -228,17 +229,6 @@ ActiveRecord::Schema.define(version: 2020_06_10_143132) do | |||||||
|     t.index ["user_id"], name: "index_events_on_user_id" |     t.index ["user_id"], name: "index_events_on_user_id" | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   create_table "hooks_inbox_apps", force: :cascade do |t| |  | ||||||
|     t.integer "inbox_id" |  | ||||||
|     t.integer "agent_id" |  | ||||||
|     t.integer "account_id" |  | ||||||
|     t.string "app_slug" |  | ||||||
|     t.string "status" |  | ||||||
|     t.text "settings" |  | ||||||
|     t.datetime "created_at", precision: 6, null: false |  | ||||||
|     t.datetime "updated_at", precision: 6, null: false |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   create_table "inbox_members", id: :serial, force: :cascade do |t| |   create_table "inbox_members", id: :serial, force: :cascade do |t| | ||||||
|     t.integer "user_id", null: false |     t.integer "user_id", null: false | ||||||
|     t.integer "inbox_id", null: false |     t.integer "inbox_id", null: false | ||||||
| @@ -381,11 +371,9 @@ ActiveRecord::Schema.define(version: 2020_06_10_143132) do | |||||||
|     t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" |     t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" | ||||||
|     t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy" |     t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy" | ||||||
|     t.index ["taggable_id"], name: "index_taggings_on_taggable_id" |     t.index ["taggable_id"], name: "index_taggings_on_taggable_id" | ||||||
|     t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id" |  | ||||||
|     t.index ["taggable_type"], name: "index_taggings_on_taggable_type" |     t.index ["taggable_type"], name: "index_taggings_on_taggable_type" | ||||||
|     t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type" |     t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type" | ||||||
|     t.index ["tagger_id"], name: "index_taggings_on_tagger_id" |     t.index ["tagger_id"], name: "index_taggings_on_tagger_id" | ||||||
|     t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id" |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   create_table "tags", id: :serial, force: :cascade do |t| |   create_table "tags", id: :serial, force: :cascade do |t| | ||||||
|   | |||||||
| @@ -30,7 +30,9 @@ RSpec.describe '/api/v1/widget/events', type: :request do | |||||||
|              as: :json |              as: :json | ||||||
|  |  | ||||||
|         expect(response).to have_http_status(:success) |         expect(response).to have_http_status(:success) | ||||||
|         expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(params[:name], anything, contact_inbox: contact_inbox) |         expect(Rails.configuration.dispatcher).to have_received(:dispatch) | ||||||
|  |           .with(params[:name], anything, contact_inbox: contact_inbox, | ||||||
|  |                                          event_info: { browser_language: nil, widget_language: nil }) | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sojan Jose
					Sojan Jose