Skip to content

Commit

Permalink
Merge branch 'develop' into CV2-3738-item-page-crashes-after-collabor…
Browse files Browse the repository at this point in the history
…ator-edits-the-fact-check-section
  • Loading branch information
melsawy committed Sep 20, 2023
2 parents 009edb6 + 7039a67 commit 62c6f85
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def available_newsletter_header_types
tbi = TeamBotInstallation.where(team_id: self.id, user_id: BotUser.smooch_user&.id.to_i).last
unless tbi.nil?
['none', 'image', 'video', 'audio', 'link_preview'].each do |header_type|
mapped_header_type = TiplineNewsletter::HEADER_TYPE_MAPPING[header_type]
mapped_header_type = TiplineNewsletter::WHATSAPP_HEADER_TYPE_MAPPING[header_type]
if !tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_no_articles").blank? &&
!tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_one_articles").blank? &&
!tbi.send("get_smooch_template_name_for_newsletter_#{mapped_header_type}_two_articles").blank? &&
Expand Down
20 changes: 17 additions & 3 deletions app/models/tipline_newsletter.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class TiplineNewsletter < ApplicationRecord
SCHEDULE_DAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
HEADER_TYPE_MAPPING = {
WHATSAPP_HEADER_TYPE_MAPPING = {
'none' => 'none',
'image' => 'image',
'video' => 'video',
'audio' => 'video', # WhatsApp doesn't support audio header, so we convert it to video
'link_preview' => 'none'
}
NON_WHATSAPP_HEADER_TYPE_MAPPING = {
'image' => 'image',
'video' => 'file',
'audio' => 'file'
}
MAXIMUM_ARTICLE_LENGTH = { # Number of articles => Maximum length for each article
1 => 694,
2 => 345,
Expand Down Expand Up @@ -114,7 +119,7 @@ def last_scheduled_by

def whatsapp_template_name
number = ['no', 'one', 'two', 'three'][self.articles.size]
type = HEADER_TYPE_MAPPING[self.header_type]
type = WHATSAPP_HEADER_TYPE_MAPPING[self.header_type]
"newsletter_#{type}_#{number}_articles"
end

Expand Down Expand Up @@ -142,19 +147,28 @@ def body
self.articles.join("\n\n")
end

# For WhatsApp tiplines
def format_as_template_message
date = I18n.l(Time.now.to_date, locale: self.language.to_s.tr('_', '-'), format: :long)
file_url = file_type = nil
if ['image', 'audio', 'video'].include?(self.header_type)
file_url = CheckS3.rewrite_url(self.header_media_url)
file_type = HEADER_TYPE_MAPPING[self.header_type]
file_type = WHATSAPP_HEADER_TYPE_MAPPING[self.header_type]
end
introduction = self.team.get_shorten_outgoing_urls ? UrlRewriter.shorten_and_utmize_urls(self.introduction, self.team.get_outgoing_urls_utm_code, self) : self.introduction
params = [date, introduction, self.articles].flatten.reject{ |param| param.blank? }
preview_url = (self.header_type == 'link_preview')
Bot::Smooch.format_template_message(self.whatsapp_template_name, params, file_url, self.build_content, self.language, file_type, preview_url)
end

# For non-WhatsApp tiplines, ideally
def format_as_tipline_message
message = self.build_content
message = (self.team.get_shorten_outgoing_urls ? UrlRewriter.shorten_and_utmize_urls(message, self.team.get_outgoing_urls_utm_code) : message)
params = (['image', 'audio', 'video'].include?(self.header_type) ? { 'type' => NON_WHATSAPP_HEADER_TYPE_MAPPING[self.header_type], 'mediaUrl' => CheckS3.rewrite_url(self.header_media_url) } : {})
[message, params]
end

def self.content_name
'newsletter'
end
Expand Down
4 changes: 1 addition & 3 deletions app/models/tipline_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def format_as_tipline_message
message = []
message << "*#{self.title.strip}*" unless self.title.strip.blank?
message << self.content unless self.content.blank?
if self.content_type == 'static'
# FIXME: Handle static content
elsif self.content_type == 'rss' && !self.rss_feed_url.blank?
if self.content_type == 'rss' && !self.rss_feed_url.blank?
message << Rails.cache.fetch("tipline_resource:rss_feed:#{Digest::MD5.hexdigest(self.rss_feed_url)}:#{self.number_of_articles}", expires_in: 15.minutes, skip_nil: true) do
rss_feed = RssFeed.new(self.rss_feed_url)
rss_feed.get_articles(self.number_of_articles).join("\n\n")
Expand Down
2 changes: 1 addition & 1 deletion app/workers/tipline_newsletter_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def perform(team_id, language, job_created_at = 0)
RequestStore.store[:smooch_bot_platform] = ts.platform
Bot::Smooch.get_installation('team_bot_installation_id', tbi.id) { |i| i.id == tbi.id }

response = Bot::Smooch.send_message_to_user(ts.uid, newsletter.format_as_template_message)
response = (ts.platform == 'WhatsApp' ? Bot::Smooch.send_message_to_user(ts.uid, newsletter.format_as_template_message) : Bot::Smooch.send_message_to_user(ts.uid, *newsletter.format_as_tipline_message))

if response.code.to_i < 400
log team_id, language, "Newsletter sent to subscriber ##{ts.id}, response: (#{response.code}) #{response.body.inspect}"
Expand Down
7 changes: 7 additions & 0 deletions test/workers/tipline_newsletter_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ def setup
TiplineNewsletter.any_instance.stubs(:content_has_changed?).raises(RssFeed::RssLoadError)
assert_equal 0, TiplineNewsletterWorker.new.perform(@team.id, 'en')
end

test "should send newsletter for non-WhatsApp subscription" do
create_tipline_subscription team_id: @team.id, platform: 'Telegram'
assert_nothing_raised do
TiplineNewsletterWorker.perform_async(@team.id, 'en')
end
end
end

0 comments on commit 62c6f85

Please sign in to comment.