Skip to content

Commit

Permalink
CV2-4696 extract multiple media from tipline request (#1936)
Browse files Browse the repository at this point in the history
* CV2-4696: Extract media from tipline request

* CV2-5696: attach short text to all items

* CV2-4696: append text to item requests

* CV2-4696: add more tests

* CV2-4696: rename method

* CV2-4696: apply PR comments

* CV2-4696: add more comments

* CV2-4696: apply PR comment

* CV2-4696: do a search against each message

* Revert "CV2-4696: do a search against each message"

This reverts commit fb60962.
  • Loading branch information
melsawy authored Jul 3, 2024
1 parent 278e15d commit a7db37a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 14 deletions.
63 changes: 55 additions & 8 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,60 @@ def bundle_list_of_messages(list, last, reject_payload = false)
self.adjust_media_type(bundle)
end

def handle_bundle_messages(type, list, last, app_id, annotated, send_message = true)
bundle = self.bundle_list_of_messages(list, last)
if ['default_requests', 'irrelevant_search_result_requests'].include?(type)
self.process_message(bundle, app_id, send_message, type)
def bundle_list_of_messages_to_items(list, last)
# Collect messages from list based on media files, long text and short text
# so we have three types of messages
# Long text (text with number of words > min_number_of_words_for_tipline_submit_shortcut)
# Short text (text with number of words <= min_number_of_words_for_tipline_submit_shortcut)
# Media (image, audio, video, etc)
messages = []
# Define a text variable to hold short text
text = []
list.collect{ |m| JSON.parse(m) }.sort_by{ |m| m['received'].to_f }.each do |message|
if message['type'] == 'text'
# Get an item for long text (message that match number of words condition)
if message['payload'].nil? && ::Bot::Alegre.get_number_of_words(message['text'].to_s) > CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer)
messages << message
text << message['text']
else
text << begin JSON.parse(message['payload'])['keyword'] rescue message['text'] end
end
else
# Get an item for each media file
message['text'] = [message['text'], message['mediaUrl'].to_s].compact.join("\n#{Bot::Smooch::MESSAGE_BOUNDARY}")
text << message['text']
messages << self.adjust_media_type(message)
end
end
# collect all text in right order and add a boundary so we can easily split messages if needed
all_text = text.reject{ |t| t.blank? }.join("\n#{Bot::Smooch::MESSAGE_BOUNDARY}")
if messages.blank?
# No messages exist (this happens when all messages are short text)
# So will create a new message of type text and assign short text to it
message = last.clone
message['text'] = all_text
messages << message
else
# Attach all existing text (media text, long text and short text) to each item
messages.each do |raw|
# Define a new key `request_body` so we can append all text to request body
raw['request_body'] = all_text
end
end
if ['timeout_requests', 'menu_options_requests', 'resource_requests', 'relevant_search_result_requests', 'timeout_search_requests'].include?(type)
key = "smooch:banned:#{bundle['authorId']}"
if Rails.cache.read(key).nil?
[annotated].flatten.uniq.each_with_index { |a, i| self.save_message_later(bundle, app_id, type, a, i * 60) }
messages
end

def handle_bundle_messages(type, list, last, app_id, annotated, send_message = true)
messages = self.bundle_list_of_messages_to_items(list, last)
messages.each do |message|
if ['default_requests', 'irrelevant_search_result_requests'].include?(type)
self.process_message(message, app_id, send_message, type)
end
if ['timeout_requests', 'menu_options_requests', 'resource_requests', 'relevant_search_result_requests', 'timeout_search_requests'].include?(type)
key = "smooch:banned:#{message['authorId']}"
if Rails.cache.read(key).nil?
[annotated].flatten.uniq.each_with_index { |a, i| self.save_message_later(message, app_id, type, a, i * 60) }
end
end
end
end
Expand Down Expand Up @@ -364,6 +409,8 @@ def save_message(message_json, app_id, author = nil, request_type = 'default_req
end

def smooch_save_tipline_request(message, associated, app_id, author, request_type, associated_obj)
message['text'] = message['request_body'] unless message['request_body'].blank?
message.delete('request_body')
fields = { smooch_data: message.merge({ app_id: app_id }) }
result = self.smooch_api_get_messages(app_id, message['authorId'])
fields[:smooch_conversation_id] = result.conversation.id unless result.nil? || result.conversation.nil?
Expand Down
63 changes: 57 additions & 6 deletions test/models/bot/smooch_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ def teardown
end

test "should bundle messages" do
long_text = []
15.times{ long_text << random_string }
# messages contain the following:
# 1). long text( > min_number_of_words_for_tipline_submit_shortcut)
# 2). short text (< min_number_of_words_for_tipline_submit_shortcut)
# 3). 2 medias
# Result: created three items (on claim and two items of type image)
Sidekiq::Testing.fake! do
uid = random_string
messages = [
Expand All @@ -79,7 +86,7 @@ def teardown
authorId: uid,
type: 'text',
source: { type: "whatsapp" },
text: 'foo',
text: long_text.join(' '),
},
{
'_id': random_string,
Expand All @@ -94,7 +101,6 @@ def teardown
authorId: uid,
type: 'image',
source: { type: "whatsapp" },
text: 'second image',
mediaUrl: @media_url_2
},
{
Expand All @@ -121,12 +127,57 @@ def teardown
Bot::Smooch.run(payload)
sleep 1
end
assert_difference 'ProjectMedia.count' do
Sidekiq::Worker.drain_all
assert_difference 'ProjectMedia.count', 3 do
assert_difference 'UploadedImage.count', 2 do
assert_difference 'Claim.count' do
Sidekiq::Worker.drain_all
end
end
end
pm = ProjectMedia.last
assert_no_match /#{@media_url}/, pm.text
assert_equal 'UploadedImage', pm.media.type
request = pm.tipline_requests.last
text = request.smooch_data['text'].split("\n#{Bot::Smooch::MESSAGE_BOUNDARY}")
target_text = [long_text.join(' '), 'first image', @media_url, @media_url_2, 'bar']
assert_equal target_text, text
# Messages with short text only
messages = [
{
'_id': random_string,
authorId: uid,
type: 'text',
source: { type: "whatsapp" },
text: 'foo',
},
{
'_id': random_string,
authorId: uid,
type: 'text',
text: 'bar',
source: { type: "whatsapp" },
}
]
messages.each do |message|
payload = {
trigger: 'message:appUser',
app: {
'_id': @app_id
},
version: 'v1.1',
messages: [message],
appUser: {
'_id': random_string,
'conversationStarted': true
}
}.to_json
Bot::Smooch.run(payload)
sleep 1
end
assert_difference 'TiplineRequest.count' do
Sidekiq::Worker.drain_all
end
request = TiplineRequest.last
text = request.smooch_data['text'].split("\n#{Bot::Smooch::MESSAGE_BOUNDARY}")
assert_equal ['foo', 'bar'], text
end
end

Expand Down

0 comments on commit a7db37a

Please sign in to comment.