Browse Source

Delete unconfirmed players after 7 days

Ticket #1270 @1h
pull/45/head
Martin Bober 2 years ago
parent
commit
ed3dc6d4b4
  1. 6
      Readme.md
  2. 10
      app/mailers/player_mailer.rb
  3. 3
      app/models/player.rb
  4. 11
      app/views/player_mailer/player_delete.de.markerb
  5. 11
      app/views/player_mailer/player_delete.markerb
  6. 1
      config/locales/players/de.yml
  7. 1
      config/locales/players/en.yml
  8. 7
      lib/tasks/cron.rake
  9. 12
      lib/tasks/unconfirmed_players.rake
  10. 5
      test/fixtures/one_time_tokens.yml
  11. 7
      test/fixtures/players.yml
  12. 10
      test/tasks/unconfirmed_players_test.rb

6
Readme.md

@ -40,6 +40,12 @@ On first start, you have to start a terminal in the container and run the follow
bin/rake db:schema:load
## Cron Job
You will need to run the daily cron tasks
rails cron_daily
## RubyMine Integration
### Ruby Remote SDK

10
app/mailers/player_mailer.rb

@ -492,6 +492,16 @@ class PlayerMailer < ApplicationMailer
end
end
def player_delete(name, email, locale)
@name = name
I18n.with_locale(locale) do
mail to: email, subject: t('player.email.deleted_email') do |format|
format.text
format.html
end
end
end
def push_announcement(player, announcement)
@player = player
@announcement = announcement

3
app/models/player.rb

@ -2,6 +2,7 @@ class Player < ApplicationRecord
attr_accessor :remember_token
attr_accessor :invite_token
before_save { self.email = email.downcase }
before_destroy { PlayerMailer.player_delete(self.name, self.email, self.get_locale).deliver_now }
validates(:name, presence: true, length: {maximum: 50})
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates(:email, presence: true,
@ -18,7 +19,7 @@ class Player < ApplicationRecord
@allowed_genders = %w(m f)
validates :gender, inclusion: {in: @allowed_genders}
has_many :characters, dependent: :destroy
has_many :owned_campaigns, class_name: 'Campaign', foreign_key: 'owner_id'
has_many :owned_campaigns, class_name: 'Campaign', foreign_key: 'owner_id', dependent: :nullify
has_and_belongs_to_many :gming_campaigns, join_table: 'game_masters', class_name: 'Campaign'
has_many :notifications, dependent: :destroy
has_many :forum_posts, dependent: :nullify

11
app/views/player_mailer/player_delete.de.markerb

@ -0,0 +1,11 @@
Hallo <%=@name%>,
dies ist die Bestätigung, dass wir dein Konto in der [Pen&PaperBox][1] gelöscht haben.
Du kannst gerne wieder ein neues Konto eröffnen.
Alles Gute
Dein Pen&PaperBox Team
[1]: <%= root_url%>

11
app/views/player_mailer/player_delete.markerb

@ -0,0 +1,11 @@
Hi <%=@name%>,
this is a confirmation that we have deleted your player account.
If you change your mind, you are welcome to create a new account in the [Pen&PaperBox][1].
Best Regards
Your Pen&PaperBox Team
[1]: <%= root_url%>

1
config/locales/players/de.yml

@ -38,6 +38,7 @@ de:
subject: "Dein Passwort zur Pen&PaperBox wurde zurückgesetzt"
validate_email: "Bitte bestätige deine E-Mail-Adresse"
validate_email_reminder: "Bitte bestätige deine E-Mail-Adresse (Erinnerung)"
deleted_email: "Dein Account wurde gelöscht"
index:
title: "Spieler"
name: "Name"

1
config/locales/players/en.yml

@ -40,6 +40,7 @@ en:
subject: "The password for your Pen&PaperBox has been reset"
validate_email: "Please validate your email address."
validate_email_reminder: "Please validate your email address (Reminder)"
deleted_email: "Your account has been deleted"
index:
title: "Player"
name: "Name"

7
lib/tasks/cron.rake

@ -0,0 +1,7 @@
desc "Run daily tasks"
task cron_daily: [:environment] do
puts "Started daily cron tasks at #{DateTime.now}"
Rake::Task['remind_unconfirmed_players'].invoke
Rake::Task['delete_unconfirmed_players'].invoke
puts "Finished daily cron tasks at #{DateTime.now}"
end

12
lib/tasks/unconfirmed_players.rake

@ -30,4 +30,16 @@ task remind_unconfirmed_players: [:environment] do
else
puts "Sent #{np} emails. #{nw} Warnings!"
end
end
desc "Delete players that have not confirmed their email addresses 7 days after registration"
task delete_unconfirmed_players: [:environment] do
puts "Deleting unconfirmed players:"
np = 0
Player.where("email_validated = ? AND created_at < ?", false , DateTime.now - 7.days).each do |p|
puts "#{p.name} #{p.email}"
p.destroy
np += 1
end
puts "#{np} player deleted."
end

5
test/fixtures/one_time_tokens.yml vendored

@ -8,4 +8,9 @@ one:
two:
player: to_be_warned
token: MyString
action: 0
three:
player: to_be_deleted
token: MyString
action: 0

7
test/fixtures/players.yml vendored

@ -75,4 +75,9 @@ campaign_player_wo_character:
to_be_warned:
name: To be warned
email: to_be_warned@example.com
created_at: <%=2.days.ago.to_s(:db)%>
created_at: <%=2.days.ago.to_s(:db)%>
to_be_deleted:
name: To be deleted
email: to_be_deleted@example.com
created_at: <%=8.days.ago.to_s(:db)%>

10
test/tasks/unconfirmed_players_test.rb

@ -16,4 +16,14 @@ class UnconfirmedPlayersTest < ActiveSupport::TestCase
assert_equal players.count, ActionMailer::Base.deliveries.count
end
test "Should delete players that did not confirm their email address" do
players = Player.where("email_validated = ? AND created_at < ?", false , DateTime.now - 7.days)
count = players.count
assert count > 0
assert_difference 'Player.count', -count do
Rake::Task["delete_unconfirmed_players"].invoke
end
assert_equal count, ActionMailer::Base.deliveries.count
end
end
Loading…
Cancel
Save