* Initial automatic cleanup with rubocop * Fix syntax error introduced by rubocop * Cleanup seeds file * Cleanup lib folder * Exclude bin folder because it contains auto generated files * Make Rubocop a little bit more chatty * Block length should not include comments in the count * Cleanup config folder * Cleanup specs * Updated Rubocop version and generated a todo file * Fix broken ArticlesApi spec * Fix tests * Restored rubocop pre-commit hook
60 lines
1.7 KiB
Ruby
60 lines
1.7 KiB
Ruby
class UnreadNotificationsEmailer
|
|
attr_reader :user
|
|
|
|
def self.send_all_emails(num = 1000)
|
|
# This will run once a day (defined outside the app)
|
|
# only to users who have made at least one comment or article.
|
|
# We can change this up later.
|
|
users = User.where("comments_count > ? OR reactions_count > ?", 0, 0).order("RANDOM()").limit(num)
|
|
users.find_each do |user|
|
|
UnreadNotificationsEmailer.new(user).send_email_if_appropriate
|
|
rescue StandardError => e
|
|
logger = Logger.new(STDOUT)
|
|
logger = Airbrake::AirbrakeLogger.new(logger)
|
|
logger.error(e)
|
|
end
|
|
end
|
|
|
|
def initialize(user)
|
|
@user = user
|
|
end
|
|
|
|
def send_email_if_appropriate
|
|
if should_send_email?
|
|
send_email
|
|
end
|
|
end
|
|
|
|
def should_send_email?
|
|
return false if !user.email_unread_notifications
|
|
return false if last_email_sent_after(24.hours.ago)
|
|
emailable_notifications_count = 0
|
|
user_activities.each do |activity|
|
|
emailable_notifications_count += 1 if proper_activity(activity)
|
|
end
|
|
emailable_notifications_count > rand(1..6)
|
|
end
|
|
|
|
def user_activities
|
|
return [] if Rails.env.test?
|
|
feed = StreamRails.feed_manager.get_notification_feed(user.id)
|
|
results = feed.get["results"]
|
|
StreamRails::Enrich.new.enrich_aggregated_activities(results)
|
|
end
|
|
|
|
def send_email
|
|
NotifyMailer.unread_notifications_email(user).deliver
|
|
end
|
|
|
|
private
|
|
|
|
def proper_activity(activity)
|
|
activity["verb"] != "Reaction" && activity["is_seen"] == false
|
|
end
|
|
|
|
def last_email_sent_after(time)
|
|
last_email = user.email_messages.last
|
|
time_check = last_email&.sent_at && (last_email.sent_at > time)
|
|
time_check.present?
|
|
end
|
|
end
|