Skip to content

Commit

Permalink
Do not submit as unconfirmed media a text with just a few words
Browse files Browse the repository at this point in the history
In that case, the request should still be saved, but associated with the workspace, not with a text media.

Fixes CV2-3784.
  • Loading branch information
caiosba authored Oct 7, 2023
1 parent 5da8afa commit a7de721
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 60 deletions.
4 changes: 4 additions & 0 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions test/controllers/graphql_controller_10_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 0 additions & 43 deletions test/controllers/graphql_controller_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 46 additions & 15 deletions test/models/bot/smooch_4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<rss version="1"><channel><title>x</title><link>x</link><description>x</description><item><title>x</title><link>x</link></item></channel></rss>'
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
Expand Down
26 changes: 26 additions & 0 deletions test/models/bot/smooch_6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a7de721

Please sign in to comment.