diff --git a/app/assets/stylesheets/notifications.scss b/app/assets/stylesheets/notifications.scss index ccd6db2f9..8910aa68e 100644 --- a/app/assets/stylesheets/notifications.scss +++ b/app/assets/stylesheets/notifications.scss @@ -67,11 +67,21 @@ margin: 10px auto; width: 90%; color:#666666; + &.milestone-emojis{ + font-size: xx-large; + padding-top: 10px; + padding-bottom: 0px; + } } img.badge-image{ margin:10px 0px 20px; height: 150px; } + img.milestone-gif{ + margin: 10px 0px 10px; + border-radius: 8px; + width: 300px; + } p.badge-reward-message{ margin: 8px auto; width: 90%; diff --git a/app/decorators/notification_decorator.rb b/app/decorators/notification_decorator.rb index c2be08aed..6e994653f 100644 --- a/app/decorators/notification_decorator.rb +++ b/app/decorators/notification_decorator.rb @@ -10,4 +10,12 @@ class NotificationDecorator < Draper::Decorator end struct.new(json_data[type]["class"]["name"], json_data[type]["id"]) end + + def milestone_type + action.split("::")[1] + end + + def milestone_count + action.split("::")[2] + end end diff --git a/app/labor/article_analytics_fetcher.rb b/app/labor/article_analytics_fetcher.rb index 0f98690fc..be6215f5e 100644 --- a/app/labor/article_analytics_fetcher.rb +++ b/app/labor/article_analytics_fetcher.rb @@ -19,9 +19,11 @@ class ArticleAnalyticsFetcher page_views_obj = pageviews.to_h chunk.each do |article| article.update_columns(previous_positive_reactions_count: article.positive_reactions_count) + Notification.send_milestone_notification(type: "Reaction", article: article) next if article.page_views_count > page_views_obj[article.id].to_i article.update_columns(page_views_count: page_views_obj[article.id].to_i) + Notification.send_milestone_notification(type: "View", article: article) end end end diff --git a/app/labor/random_gif.rb b/app/labor/random_gif.rb new file mode 100644 index 000000000..d042455d4 --- /dev/null +++ b/app/labor/random_gif.rb @@ -0,0 +1,27 @@ +class RandomGif + def initialize(_action = "congratulations") + @random_gifs = { + "xjZtu4qi1biIo" => { aspect_ratio: 1.000 }, + "12P29BwtrvsbbW" => { aspect_ratio: 0.760 }, + "9PyhoXey73EpW" => { aspect_ratio: 0.753 }, + "OcZp0maz6ALok" => { aspect_ratio: 1.000 }, + "Is1O1TWV0LEJi" => { aspect_ratio: 0.565 }, + "xjZtu4qi1biIo" => { aspect_ratio: 1.0000 }, + "lz24Z42jLcTa8" => { aspect_ratio: 0.776 }, + "g9582DNuQppxC" => { aspect_ratio: 0.562 }, + "l4HodBpDmoMA5p9bG" => { aspect_ratio: 1.000 }, + "3oxOCfV7z28QtXXAtO" => { aspect_ratio: 0.750 }, + "y8Mz1yj13s3kI" => { aspect_ratio: 0.750 }, + "111ebonMs90YLu" => { aspect_ratio: 0.750 }, + "Sk5uipPXyBjfW" => { aspect_ratio: 0.422 } + } + end + + def random_id + @random_gifs.keys.sample + end + + def get_aspect_ratio(id) + (@random_gifs[id] || "xjZtu4qi1biIo")[:aspect_ratio] + end +end diff --git a/app/models/notification.rb b/app/models/notification.rb index 457d5866c..b332ceda4 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -254,6 +254,29 @@ class Notification < ApplicationRecord end handle_asynchronously :send_tag_adjustment_notification + def send_milestone_notification(milestone_hash) + milestone_hash[:next_milestone] = next_milestone(milestone_hash) + return unless should_send_milestone?(milestone_hash) + + Notification.create!( + user_id: milestone_hash[:article].user_id, + notifiable_id: milestone_hash[:article].id, + notifiable_type: "Article", + json_data: { article: article_data(milestone_hash[:article]), gif_id: RandomGif.new.random_id }, + action: "Milestone::#{milestone_hash[:type]}::#{milestone_hash[:next_milestone]}", + ) + if milestone_hash[:article].organization_id + Notification.create!( + organization_id: milestone_hash[:article].organization_id, + notifiable_id: milestone_hash[:article].id, + notifiable_type: "Article", + json_data: { article: article_data(milestone_hash[:article]) }, + action: "Milestone::#{milestone_hash[:type]}::#{milestone_hash[:next_milestone]}", + ) + end + end + handle_asynchronously :send_milestone_notification + def remove_all(notifiable_hash) Notification.where( notifiable_id: notifiable_hash[:notifiable_id], @@ -348,6 +371,41 @@ class Notification < ApplicationRecord } end + def should_send_milestone?(milestone_hash) + return if milestone_hash[:article].published_at < DateTime.new(2019, 2, 25) + + last_milestone_notification = Notification.find_by( + user_id: milestone_hash[:article].user_id, + notifiable_type: "Article", + notifiable_id: milestone_hash[:article].id, + action: "Milestone::#{milestone_hash[:type]}::#{milestone_hash[:next_milestone]}", + ) + + if milestone_hash[:type] == "View" + last_milestone_notification.blank? && milestone_hash[:article].page_views_count > milestone_hash[:next_milestone] + elsif milestone_hash[:type] == "Reaction" + last_milestone_notification.blank? && milestone_hash[:article].positive_reactions_count > milestone_hash[:next_milestone] + end + end + + def next_milestone(milestone_hash) + case milestone_hash[:type] + when "View" + milestones = [1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576] + milestone_count = milestone_hash[:article].page_views_count + when "Reaction" + milestones = [64, 128, 256, 512, 1024, 2048, 4096, 8192] + milestone_count = milestone_hash[:article].positive_reactions_count + end + + closest_number = milestones.min_by { |num| (milestone_count - num).abs } + if milestone_count > closest_number + closest_number + else + milestones[milestones.index(closest_number) - 1] + end + end + def send_push_notifications(user_id, title, body, path) return unless ApplicationConfig["PUSHER_BEAMS_KEY"] && ApplicationConfig["PUSHER_BEAMS_KEY"].size == 64 diff --git a/app/views/notifications/_article.html.erb b/app/views/notifications/_article.html.erb index a3be60d4c..a84bc1b71 100644 --- a/app/views/notifications/_article.html.erb +++ b/app/views/notifications/_article.html.erb @@ -1,16 +1,18 @@ <% if notification.action == "Reaction" %> <%= render "reaction", notification: notification %> +<% elsif notification.action.include? "Milestone" %> + <%= render "milestone", notification: notification %> <% else %> <% json_data = notification.json_data %> <% if json_data["organization"] %>
🎉🎉🎉🎉🎉🎉🎉🎉
+ + " rel="noopener noreferrer" target="_blank"> +