FEATURE: Send user activation reminders. (#7280)

This commit is contained in:
Bianca Nenciu 2019-04-10 17:53:52 +03:00 committed by Régis Hanol
parent af15ba86a0
commit 3d545d66df
5 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,22 @@
module Jobs
class ActivationReminderEmails < Jobs::Scheduled
every 2.hours
def execute(args)
User.joins("LEFT JOIN user_custom_fields ON users.id = user_id AND user_custom_fields.name = 'activation_reminder'")
.where(active: false, user_custom_fields: { value: nil })
.where('users.created_at < ?', 2.days.ago)
.find_each do |user|
user.custom_fields['activation_reminder'] = true
user.save_custom_fields
email_token = user.email_tokens.create!(email: user.email)
Jobs.enqueue(:user_email,
type: :activation_reminder,
user_id: user.id,
email_token: email_token.token)
end
end
end
end

View File

@ -21,6 +21,14 @@ class UserNotifications < ActionMailer::Base
) )
end end
def activation_reminder(user, opts = {})
build_user_email_token_by_template(
"user_notifications.activation_reminder",
user,
opts[:email_token]
)
end
def signup_after_approval(user, opts = {}) def signup_after_approval(user, opts = {})
locale = user_locale(user) locale = user_locale(user)
tips = I18n.t('system_messages.usage_tips.text_body_template', tips = I18n.t('system_messages.usage_tips.text_body_template',

View File

@ -66,6 +66,7 @@ class EmailToken < ActiveRecord::Base
user.send_welcome_message = !user.active? user.send_welcome_message = !user.active?
user.email = result[:email_token].email user.email = result[:email_token].email
user.active = true user.active = true
user.custom_fields.delete('activation_reminder')
user.save! user.save!
user.create_reviewable unless skip_reviewable user.create_reviewable unless skip_reviewable
user.set_automatic_groups user.set_automatic_groups

View File

@ -3417,6 +3417,19 @@ en:
If the above link is not clickable, try copying and pasting it into the address bar of your web browser. If the above link is not clickable, try copying and pasting it into the address bar of your web browser.
activation_reminder:
title: "Activation Reminder"
subject_template: "[%{email_prefix}] Reminder to confirm your account"
text_body_template: |
Welcome to %{site_name}!
This is a friendly reminder to activate your account.
Click the following link to confirm and activate your new account:
%{base_url}/u/activate-account/%{email_token}
If the above link is not clickable, try copying and pasting it into the address bar of your web browser.
suspicious_login: suspicious_login:
title: "New Login Alert" title: "New Login Alert"
subject_template: "[%{site_name}] New Login from %{location}" subject_template: "[%{site_name}] New Login from %{location}"

View File

@ -0,0 +1,29 @@
require 'rails_helper'
describe Jobs::ActivationReminderEmails do
before do
Jobs.run_immediately!
end
it 'should email inactive users' do
user = Fabricate(:user, active: false, created_at: 3.days.ago)
expect { described_class.new.execute({}) }
.to change { ActionMailer::Base.deliveries.size }.by(1)
.and change { user.email_tokens.count }.by(1)
expect(user.custom_fields['activation_reminder']).to eq("t")
expect { described_class.new.execute({}) }.to change { ActionMailer::Base.deliveries.size }.by(0)
user.activate
expect(user.reload.custom_fields['activation_reminder']).to eq(nil)
end
it 'should not email active users' do
user = Fabricate(:user, active: true, created_at: 3.days.ago)
expect { described_class.new.execute({}) }
.to change { ActionMailer::Base.deliveries.size }.by(0)
.and change { user.email_tokens.count }.by(0)
end
end