Skip to content

Commit

Permalink
CV2-4754: force relation between media and long caption (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy authored Jul 25, 2024
1 parent 3660b70 commit 12daff2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
19 changes: 14 additions & 5 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,11 @@ def self.save_text_message(message)
extra = { url: link.url }
pm = ProjectMedia.joins(:media).where('medias.url' => link.url, 'project_medias.team_id' => team.id).last
end

if pm.nil?
type = link.nil? ? 'Claim' : 'Link'
pm = self.create_project_media(message, type, extra)
end

self.add_hashtags(text, pm)

pm
rescue SecurityError
self.ban_user(message)
Expand Down Expand Up @@ -923,14 +920,26 @@ def self.save_media_message(message)
pm.save!
end
end
self.relate_item_and_caption(pm, message) unless pm.nil? || message['caption'].nil?
FileUtils.rm_f filepath

self.add_hashtags(text, pm)

pm
end
end

def self.relate_item_and_caption(pm, message)
message['text'] = message['caption']
target = self.create_project_media(message, 'Claim', { quote: message['caption'] })
unless target.nil?
r = Relationship.new
r.skip_check_ability = true
r.relationship_type = Relationship.suggested_type
r.source_id = pm.id
r.target_id = target.id
r.save!
end
end

def self.send_report_to_users(pm, action)
parent = Relationship.confirmed_parent(pm)
report = parent.get_annotations('report_design').last&.load
Expand Down
3 changes: 3 additions & 0 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ def bundle_list_of_messages_to_items(list, last)
end
elsif !message['mediaUrl'].blank?
# Get an item for each media file
if !message['text'].blank? && ::Bot::Alegre.get_number_of_words(message['text'].to_s) > CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer)
message['caption'] = message['text']
end
message['text'] = [message['text'], message['mediaUrl'].to_s].compact.join("\n#{Bot::Smooch::MESSAGE_BOUNDARY}")
text << message['text']
messages << self.adjust_media_type(message)
Expand Down
64 changes: 63 additions & 1 deletion test/models/bot/smooch_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def teardown
# 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)
# Result: created three items (one claim and two items of type image)
Sidekiq::Testing.fake! do
uid = random_string
messages = [
Expand Down Expand Up @@ -181,6 +181,68 @@ def teardown
end
end

test "should force relationship between media and caption text" do
long_text = []
15.times{ long_text << random_string }
caption = long_text.join(' ')
# messages contain the following:
# 1). media with long text( > min_number_of_words_for_tipline_submit_shortcut)
# 2). media with short text (< min_number_of_words_for_tipline_submit_shortcut)
# Result: created three items and one relationship (one claim for caption and two items of type image)
last_id = ProjectMedia.last.id
Sidekiq::Testing.fake! do
uid = random_string
messages = [
{
'_id': random_string,
authorId: uid,
type: 'image',
source: { type: "whatsapp" },
text: 'first image',
mediaUrl: @media_url
},
{
'_id': random_string,
authorId: uid,
type: 'image',
source: { type: "whatsapp" },
text: caption,
mediaUrl: @media_url_2
}
]
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 'ProjectMedia.count', 3 do
assert_difference 'UploadedImage.count', 2 do
assert_difference 'Claim.count' do
assert_difference 'Relationship.count' do
Sidekiq::Worker.drain_all
end
end
end
end
claim_item = ProjectMedia.joins(:media).where('medias.type' => 'Claim').last
assert_equal caption, claim_item.media.quote
r = Relationship.last
assert_equal Relationship.suggested_type, r.relationship_type
assert_equal claim_item.id, r.target_id
end
end

test "should delete cache entries when user annotation is deleted" do
create_flag_annotation_type
create_annotation_type_and_fields('Smooch User', { 'Id' => ['Text', false], 'App Id' => ['Text', false], 'Data' => ['JSON', false] })
Expand Down

0 comments on commit 12daff2

Please sign in to comment.