From a7de7219d9157e12445e3f9892e8de5f32005261 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Sat, 7 Oct 2023 19:42:47 -0300 Subject: [PATCH] Do not submit as unconfirmed media a text with just a few words In that case, the request should still be saved, but associated with the workspace, not with a text media. Fixes CV2-3784. --- app/models/bot/smooch.rb | 4 ++ app/models/concerns/smooch_messages.rb | 4 +- .../controllers/graphql_controller_10_test.rb | 46 ++++++++++++++ test/controllers/graphql_controller_3_test.rb | 43 ------------- test/models/bot/smooch_4_test.rb | 61 ++++++++++++++----- test/models/bot/smooch_6_test.rb | 26 ++++++++ 6 files changed, 124 insertions(+), 60 deletions(-) diff --git a/app/models/bot/smooch.rb b/app/models/bot/smooch.rb index 31fd749316..5c5220305a 100644 --- a/app/models/bot/smooch.rb +++ b/app/models/bot/smooch.rb @@ -820,6 +820,10 @@ def self.save_text_message(message) claim = self.extract_claim(text).gsub(/\s+/, ' ').strip extra = { quote: claim } pm = ProjectMedia.joins(:media).where('trim(lower(quote)) = ?', claim.downcase).where('project_medias.team_id' => team.id).last + # Don't create a new text media if it's an unconfirmed request with just a few words + if pm.nil? && message['archived'] == CheckArchivedFlags::FlagCodes::UNCONFIRMED && ::Bot::Alegre.get_number_of_words(claim) < CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer) + return team + end else extra = { url: link.url } pm = ProjectMedia.joins(:media).where('medias.url' => link.url, 'project_medias.team_id' => team.id).last diff --git a/app/models/concerns/smooch_messages.rb b/app/models/concerns/smooch_messages.rb index c8f69a9461..fb433182f9 100644 --- a/app/models/concerns/smooch_messages.rb +++ b/app/models/concerns/smooch_messages.rb @@ -335,10 +335,10 @@ def save_message(message_json, app_id, author = nil, request_type = 'default_req self.get_installation(self.installation_setting_id_keys, app_id) Team.current = Team.where(id: self.config['team_id']).last annotated = nil - if ['default_requests', 'timeout_requests', 'resource_requests', 'irrelevant_search_result_requests'].include?(request_type) + if ['default_requests', 'timeout_requests', 'irrelevant_search_result_requests'].include?(request_type) message['archived'] = ['default_requests', 'irrelevant_search_result_requests'].include?(request_type) ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED annotated = self.create_project_media_from_message(message) - elsif ['menu_options_requests', 'relevant_search_result_requests', 'timeout_search_requests'].include?(request_type) + elsif ['menu_options_requests', 'relevant_search_result_requests', 'timeout_search_requests', 'resource_requests'].include?(request_type) annotated = annotated_obj end diff --git a/test/controllers/graphql_controller_10_test.rb b/test/controllers/graphql_controller_10_test.rb index 0193d1efb8..90479327bf 100644 --- a/test/controllers/graphql_controller_10_test.rb +++ b/test/controllers/graphql_controller_10_test.rb @@ -809,4 +809,50 @@ def setup assert_response :success assert !JSON.parse(@response.body)['data']['sendTiplineMessage']['success'] end + + test "should set smooch user Slack channel URL in background" do + u = create_user + t = create_team + create_team_user team: t, user: u, role: 'admin' + p = create_project team: t + author_id = random_string + set_fields = { smooch_user_data: { id: author_id }.to_json, smooch_user_app_id: 'fake', smooch_user_id: 'fake' }.to_json + d = create_dynamic_annotation annotated: p, annotation_type: 'smooch_user', set_fields: set_fields + authenticate_with_token + url = random_url + query = 'mutation { updateDynamicAnnotationSmoochUser(input: { clientMutationId: "1", id: "' + d.graphql_id + '", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url + '\"}" }) { project { dbid } } }' + + Sidekiq::Testing.fake! do + post :create, params: { query: query } + assert_response :success + end + Sidekiq::Worker.drain_all + assert_equal url, Dynamic.find(d.id).get_field_value('smooch_user_slack_channel_url') + + # Check that cache key exists + key = "SmoochUserSlackChannelUrl:Team:#{d.team_id}:#{author_id}" + assert_equal url, Rails.cache.read(key) + + # Test using a new mutation `smoochBotAddSlackChannelUrl` + url2 = random_url + query = 'mutation { smoochBotAddSlackChannelUrl(input: { clientMutationId: "1", id: "' + d.id.to_s + '", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url2 + '\"}" }) { annotation { dbid } } }' + Sidekiq::Testing.fake! do + post :create, params: { query: query } + assert_response :success + end + assert Sidekiq::Worker.jobs.size > 0 + assert_equal url, d.reload.get_field_value('smooch_user_slack_channel_url') + + # Execute job and check that URL was set + Sidekiq::Worker.drain_all + assert_equal url2, d.get_field_value('smooch_user_slack_channel_url') + + # Check that cache key exists + assert_equal url2, Rails.cache.read(key) + + # Call mutation with non existing ID + query = 'mutation { smoochBotAddSlackChannelUrl(input: { clientMutationId: "1", id: "99999", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url2 + '\"}" }) { annotation { dbid } } }' + post :create, params: { query: query } + assert_response :success + end end diff --git a/test/controllers/graphql_controller_3_test.rb b/test/controllers/graphql_controller_3_test.rb index 57f9408831..e2892e565c 100644 --- a/test/controllers/graphql_controller_3_test.rb +++ b/test/controllers/graphql_controller_3_test.rb @@ -341,49 +341,6 @@ def setup assert_equal 1, response['item_navigation_offset'] end - test "should set smooch user slack channel url in background" do - Sidekiq::Testing.fake! do - u = create_user - t = create_team - create_team_user team: t, user: u, role: 'admin' - p = create_project team: t - author_id = random_string - set_fields = { smooch_user_data: { id: author_id }.to_json, smooch_user_app_id: 'fake', smooch_user_id: 'fake' }.to_json - d = create_dynamic_annotation annotated: p, annotation_type: 'smooch_user', set_fields: set_fields - Sidekiq::Worker.drain_all - assert_equal 0, Sidekiq::Worker.jobs.size - authenticate_with_token - url = random_url - query = 'mutation { updateDynamicAnnotationSmoochUser(input: { clientMutationId: "1", id: "' + d.graphql_id + '", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url + '\"}" }) { project { dbid } } }' - post :create, params: { query: query } - assert_response :success - Sidekiq::Worker.drain_all - sleep 1 - assert_equal url, Dynamic.find(d.id).get_field_value('smooch_user_slack_channel_url') - # check that cache key exists - key = "SmoochUserSlackChannelUrl:Team:#{d.team_id}:#{author_id}" - assert_equal url, Rails.cache.read(key) - # test using a new mutation `smoochBotAddSlackChannelUrl` - Sidekiq::Worker.drain_all - assert_equal 0, Sidekiq::Worker.jobs.size - url2 = random_url - query = 'mutation { smoochBotAddSlackChannelUrl(input: { clientMutationId: "1", id: "' + d.id.to_s + '", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url2 + '\"}" }) { annotation { dbid } } }' - post :create, params: { query: query } - assert_response :success - assert Sidekiq::Worker.jobs.size > 0 - assert_equal url, d.reload.get_field_value('smooch_user_slack_channel_url') - # execute job and check that url was set - Sidekiq::Worker.drain_all - assert_equal url2, d.get_field_value('smooch_user_slack_channel_url') - # check that cache key exists - assert_equal url2, Rails.cache.read(key) - # call mutation with non existing id - query = 'mutation { smoochBotAddSlackChannelUrl(input: { clientMutationId: "1", id: "99999", set_fields: "{\"smooch_user_slack_channel_url\":\"' + url2 + '\"}" }) { annotation { dbid } } }' - post :create, params: { query: query } - assert_response :success - end - end - test "should get requests from media" do u = create_user is_admin: true t = create_team diff --git a/test/models/bot/smooch_4_test.rb b/test/models/bot/smooch_4_test.rb index 80db102e3a..1d619bc543 100644 --- a/test/models/bot/smooch_4_test.rb +++ b/test/models/bot/smooch_4_test.rb @@ -476,50 +476,81 @@ def teardown test "should request resource" do setup_smooch_bot(true) - RequestStore.store[:skip_cached_field_update] = false uid = random_string rss = 'xxxxx' WebMock.stub_request(:get, 'http://test.com/feed.rss').to_return(status: 200, body: rss) Sidekiq::Testing.fake! do send_message_to_smooch_bot('Hello', uid) send_message_to_smooch_bot('1', uid) + send_message_to_smooch_bot('4', uid) + end + Sidekiq::Worker.drain_all + a = Dynamic.where(annotation_type: 'smooch').last + assert_equal 'TiplineResource', a.annotated_type + assert_not_nil a.get_field('smooch_resource_id') + end + + test "should submit short unconfirmed request" do + setup_smooch_bot(true) + RequestStore.store[:skip_cached_field_update] = false + uid = random_string + message = "Hey ho, let's go" + Sidekiq::Testing.fake! do + send_message_to_smooch_bot('Hello', uid) + send_message_to_smooch_bot(message, uid) + end + assert_no_difference 'ProjectMedia.count' do + Sidekiq::Worker.drain_all + end + a = Dynamic.where(annotation_type: 'smooch').last + annotated = a.annotated + assert_equal 'Team', a.annotated_type + end + + test "should submit long unconfirmed request" do + setup_smooch_bot(true) + RequestStore.store[:skip_cached_field_update] = false + uid = random_string + message = 'This is a message that has enough words to be considered a media' + Sidekiq::Testing.fake! do + send_message_to_smooch_bot('Hello', uid) + send_message_to_smooch_bot(message, uid) + end + assert_difference 'ProjectMedia.count' do + Sidekiq::Worker.drain_all end - Rails.cache.stubs(:read).returns(nil) - Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds) - send_message_to_smooch_bot('4', uid) a = Dynamic.where(annotation_type: 'smooch').last annotated = a.annotated assert_equal 'ProjectMedia', a.annotated_type assert_equal CheckArchivedFlags::FlagCodes::UNCONFIRMED, annotated.archived - # verify requests_count & demand count + # Verify requests count & demand assert_equal 1, annotated.requests_count assert_equal 1, annotated.demand - assert_not_nil a.get_field('smooch_resource_id') - # Test auto confirm the media if resend same media as a default request + # Auto confirm the media if the same media is sent as a default request Sidekiq::Testing.fake! do - send_message_to_smooch_bot('Hello', uid) + send_message_to_smooch_bot(message, uid) send_message_to_smooch_bot('1', uid) + send_message_to_smooch_bot('2', uid) end Rails.cache.stubs(:read).returns(nil) Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds) assert_no_difference 'ProjectMedia.count' do - send_message_to_smooch_bot('2', uid) + send_message_to_smooch_bot('Query', uid) end + Rails.cache.unstub(:read) + Sidekiq::Worker.drain_all assert_equal CheckArchivedFlags::FlagCodes::NONE, annotated.reload.archived assert_equal 2, annotated.reload.requests_count - # Test resend same media (should not update archived cloumn) + # Test resend the same media (should not update archived column) Sidekiq::Testing.fake! do send_message_to_smooch_bot('Hello', uid) - send_message_to_smooch_bot('1', uid) + send_message_to_smooch_bot(message, uid) end - Rails.cache.stubs(:read).returns(nil) - Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds) assert_no_difference 'ProjectMedia.count' do - send_message_to_smooch_bot('2', uid) + Sidekiq::Worker.drain_all end assert_equal CheckArchivedFlags::FlagCodes::NONE, annotated.reload.archived assert_equal 3, annotated.reload.requests_count - Rails.cache.unstub(:read) end test "should get default TOS message" do diff --git a/test/models/bot/smooch_6_test.rb b/test/models/bot/smooch_6_test.rb index 77d18a14bb..5f66ead55c 100644 --- a/test/models/bot/smooch_6_test.rb +++ b/test/models/bot/smooch_6_test.rb @@ -854,4 +854,30 @@ def send_message_outside_24_hours_window(template, pm = nil) send_message 'Hello' assert_state 'main' end + + test 'should save unconfirmed media with enough words' do + @installation.set_smooch_disable_timeout = false + @installation.save! + reload_tipline_settings + send_message 'hello', '1' # Sends a first message and confirms language as English + send_message 'This is message is so long that it is considered a media' + assert_difference 'ProjectMedia.count' do + assert_difference "Dynamic.where(annotation_type: 'smooch').count" do + Sidekiq::Worker.drain_all + end + end + end + + test 'should not save unconfirmed media with just a few words' do + @installation.set_smooch_disable_timeout = false + @installation.save! + reload_tipline_settings + send_message 'hello', '1' # Sends a first message and confirms language as English + send_message 'Hi, there!' + assert_no_difference 'ProjectMedia.count' do + assert_difference "Dynamic.where(annotation_type: 'smooch').count" do + Sidekiq::Worker.drain_all + end + end + end end