Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
feat: configurable message search window (#16183)
Vishnu Narayananab70480485a6
diff --git a/.env.example b/.env.exampleindex d6b509a53f2..834e7a5277d 100644--- a/.env.example+++ b/.env.example@@ -78,6 +78,10 @@ REDIS_SENTINEL_MASTER_NAME=# OPENSEARCH_AWS_SECRET_ACCESS_KEY=# OPENSEARCH_AWS_REGION=us-east-1+# How many days back search looks. Defaults to 90.+# Covers message search and the date filter on conversation, contact and article search.+# SEARCH_TIME_RANGE_LIMIT_DAYS=90+# Postgres Database config variables# You can leave POSTGRES_DATABASE blank. The default name of# the database in the production environment is chatwoot_productiondiff --git a/app/javascript/dashboard/composables/spec/useConfig.spec.js b/app/javascript/dashboard/composables/spec/useConfig.spec.jsindex 92c65197edb..713e4a90eb2 100644--- a/app/javascript/dashboard/composables/spec/useConfig.spec.js+++ b/app/javascript/dashboard/composables/spec/useConfig.spec.js@@ -10,6 +10,7 @@ describe('useConfig', () => {enabledLanguages: ['en', 'fr'],isEnterprise: 'true',enterprisePlanName: 'enterprise',+ searchTimeRangeLimitDays: 180,};});@@ -25,6 +26,7 @@ describe('useConfig', () => {expect(config.enabledLanguages).toEqual(['en', 'fr']);expect(config.isEnterprise).toBe(true);expect(config.enterprisePlanName).toBe('enterprise');+ expect(config.searchTimeRangeLimitDays).toBe(180);});it('handles missing configuration values', () => {@@ -36,6 +38,7 @@ describe('useConfig', () => {expect(config.enabledLanguages).toBeUndefined();expect(config.isEnterprise).toBe(false);expect(config.enterprisePlanName).toBeUndefined();+ expect(config.searchTimeRangeLimitDays).toBeUndefined();});it('handles undefined window.chatwootConfig', () => {@@ -47,5 +50,6 @@ describe('useConfig', () => {expect(config.enabledLanguages).toBeUndefined();expect(config.isEnterprise).toBe(false);expect(config.enterprisePlanName).toBeUndefined();+ expect(config.searchTimeRangeLimitDays).toBeUndefined();});});diff --git a/app/javascript/dashboard/composables/useConfig.js b/app/javascript/dashboard/composables/useConfig.jsindex 4ffd05e6a05..adfa8579ae2 100644--- a/app/javascript/dashboard/composables/useConfig.js+++ b/app/javascript/dashboard/composables/useConfig.js@@ -42,6 +42,12 @@ export function useConfig() {*/const inboxEventsEnabled = config.inboxEventsEnabled === 'true';+ /**+ * How many days back search looks (SEARCH_TIME_RANGE_LIMIT_DAYS).+ * @type {number|undefined}+ */+ const searchTimeRangeLimitDays = config.searchTimeRangeLimitDays;+return {hostURL,vapidPublicKey,@@ -49,5 +55,6 @@ export function useConfig() {isEnterprise,enterprisePlanName,inboxEventsEnabled,+ searchTimeRangeLimitDays,};}diff --git a/app/javascript/dashboard/modules/search/components/SearchDateRangeSelector.vue b/app/javascript/dashboard/modules/search/components/SearchDateRangeSelector.vueindex 19aa3c8e659..2cf276833ca 100644--- a/app/javascript/dashboard/modules/search/components/SearchDateRangeSelector.vue+++ b/app/javascript/dashboard/modules/search/components/SearchDateRangeSelector.vue@@ -2,6 +2,7 @@import { computed, ref } from 'vue';import { useToggle } from '@vueuse/core';import { useI18n } from 'vue-i18n';+import { useConfig } from 'dashboard/composables/useConfig';import { vOnClickOutside } from '@vueuse/components';import {subDays,@@ -31,8 +32,12 @@ const customFrom = ref('');const customTo = ref('');const rangeType = ref(DATE_RANGE_TYPES.BETWEEN);-// Calculate min date (90 days ago) for date inputs-const minDate = computed(() => format(subDays(new Date(), 90), 'yyyy-MM-dd'));+const { searchTimeRangeLimitDays } = useConfig();++// Calculate min date (start of the search window) for date inputs+const minDate = computed(() =>+ format(subDays(new Date(), searchTimeRangeLimitDays), 'yyyy-MM-dd')+);const maxDate = computed(() => format(new Date(), 'yyyy-MM-dd'));// Check if both custom date inputs have values@@ -88,11 +93,13 @@ const selectedValue = computed(() => {});const menuItems = computed(() =>- PRESET_RANGES.value.map(item => ({- ...item,- action: DATE_FILTER_ACTIONS.PRESET,- isSelected: selectedValue.value === item.value,- }))+ PRESET_RANGES.value+ .filter(item => item.days <= searchTimeRangeLimitDays)+ .map(item => ({+ ...item,+ action: DATE_FILTER_ACTIONS.PRESET,+ isSelected: selectedValue.value === item.value,+ })));const applySelection = ({ type, from, to }) => {diff --git a/app/services/search_service.rb b/app/services/search_service.rbindex 312d8d0a4d6..439b3f115a7 100644--- a/app/services/search_service.rb+++ b/app/services/search_service.rb@@ -106,7 +106,7 @@ class SearchServiceenddef message_base_query- query = current_account.messages.where('created_at >= ?', 3.months.ago)+ query = current_account.messages.where('created_at >= ?', search_window_start)query = query.where(inbox_id: accessable_inbox_ids) unless should_skip_inbox_filtering?queryend@@ -190,12 +190,15 @@ class SearchServicequeryend+ def search_window_start+ Limits.search_time_range_limit_days.days.ago+ end+def cap_since_time(since_param)- max_lookback = 90.days.agorequested_time = Time.zone.at(since_param.to_i)- # Silently cap to max_lookback if requested time is too far back- [requested_time, max_lookback].max+ # Silently cap to the search window start if requested time is too far back+ [requested_time, search_window_start].maxenddef cap_until_time(until_param)diff --git a/app/views/layouts/vueapp.html.erb b/app/views/layouts/vueapp.html.erbindex 00dea820a43..24692813b94 100644--- a/app/views/layouts/vueapp.html.erb+++ b/app/views/layouts/vueapp.html.erb@@ -59,6 +59,7 @@enabledLanguages: <%= available_locales_with_name.to_json.html_safe %>,helpUrls: <%= feature_help_urls.to_json.html_safe %>,inboxEventsEnabled: '<%= ENV['ENABLE_INBOX_EVENTS'].present? %>',+ searchTimeRangeLimitDays: <%= Limits.search_time_range_limit_days %>,selectedLocale: '<%= I18n.locale %>'}window.globalConfig = <%= raw @global_config.to_json %>diff --git a/enterprise/app/services/enterprise/search_service.rb b/enterprise/app/services/enterprise/search_service.rbindex 4ce464693f4..c1ea50e7d13 100644--- a/enterprise/app/services/enterprise/search_service.rb+++ b/enterprise/app/services/enterprise/search_service.rb@@ -53,23 +53,8 @@ module Enterprise::SearchServicewhere_conditions[:created_at] = time_conditions if time_conditions.any?end- def cap_until_time(until_param)- max_future = 90.days.from_now- requested_time = Time.zone.at(until_param.to_i)-- [requested_time, max_future].min- end-def enforce_time_limit(since_param)- max_lookback = Limits::MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS.days.ago-- if since_param.present?- requested_time = Time.zone.at(since_param.to_i)- # Silently cap to max_lookback if requested time is too far back- [requested_time, max_lookback].max- else- max_lookback- end+ since_param.present? ? cap_since_time(since_param) : search_window_startenddef apply_inbox_filter(where_conditions)diff --git a/lib/limits.rb b/lib/limits.rbindex ce8ab787264..85d406ce838 100644--- a/lib/limits.rb+++ b/lib/limits.rb@@ -9,9 +9,13 @@ module LimitsCOMPANY_NAME_LENGTH_LIMIT = 100COMPANY_DESCRIPTION_LENGTH_LIMIT = 1000MAX_CUSTOM_FILTERS_PER_USER = 1000- MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS = 90def self.conversation_message_per_minute_limitENV.fetch('CONVERSATION_MESSAGE_PER_MINUTE_LIMIT', '200').to_iend++ def self.search_time_range_limit_days+ days = ENV.fetch('SEARCH_TIME_RANGE_LIMIT_DAYS', '90').to_i+ days.positive? ? days : 90+ endenddiff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rbindex 4231b1dc03f..1670482e9cd 100644--- a/spec/services/search_service_spec.rb+++ b/spec/services/search_service_spec.rb@@ -459,6 +459,20 @@ describe SearchService doendendend++ context 'with the search time window' do+ let!(:old_message) { create(:message, account: account, inbox: inbox, content: 'old test message', created_at: 120.days.ago) }++ it 'excludes messages older than the default window' do+ expect(search.send(:message_base_query)).not_to include(old_message)+ end++ it 'includes older messages when SEARCH_TIME_RANGE_LIMIT_DAYS is raised' do+ with_modified_env SEARCH_TIME_RANGE_LIMIT_DAYS: '180' do+ expect(search.send(:message_base_query)).to include(old_message)+ end+ end+ endenddescribe '#use_gin_search' do@@ -582,7 +596,7 @@ describe SearchService do'test',hash_including(where: hash_including(- created_at: hash_including(gte: be_within(1.second).of(Limits::MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS.days.ago))+ created_at: hash_including(gte: be_within(1.second).of(Limits.search_time_range_limit_days.days.ago))))).and_return([])@@ -591,7 +605,7 @@ describe SearchService doendit 'silently caps since timestamp to 90 day limit when exceeded' do- since_timestamp = (Limits::MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS * 2).days.ago.to_i+ since_timestamp = (Limits.search_time_range_limit_days * 2).days.ago.to_iparams = { q: 'test', since: since_timestamp }search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)@@ -599,7 +613,7 @@ describe SearchService do'test',hash_including(where: hash_including(- created_at: hash_including(gte: be_within(1.second).of(Limits::MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS.days.ago))+ created_at: hash_including(gte: be_within(1.second).of(Limits.search_time_range_limit_days.days.ago))))).and_return([])@@ -607,6 +621,25 @@ describe SearchService dosearch_service.performend+ it 'uses SEARCH_TIME_RANGE_LIMIT_DAYS as the lookback limit when set' do+ since_timestamp = 150.days.ago.to_i+ params = { q: 'test', since: since_timestamp }+ search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)++ expect(Message).to receive(:search).with(+ 'test',+ hash_including(+ where: hash_including(+ created_at: hash_including(gte: Time.zone.at(since_timestamp))+ )+ )+ ).and_return([])++ with_modified_env SEARCH_TIME_RANGE_LIMIT_DAYS: '180' do+ search_service.perform+ end+ end+it 'filters messages since timestamp when within 90 day limit' dosince_timestamp = 3.days.ago.to_iparams = { q: 'test', since: since_timestamp }