Skip to content

Commit

Permalink
Merge pull request #107 from fablabbcn/feature/remove-old-spam-users
Browse files Browse the repository at this point in the history
Feature/remove old spam users
  • Loading branch information
timcowlishaw authored Jan 10, 2024
2 parents 0427dc6 + 47f0c16 commit 729c42a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gem 'country_select'
gem "devise", "~> 4.7"
gem "devise_invitable", "~> 2"
gem "devise-i18n"
gem 'discard', '~> 1.2'
gem 'friendly_id', '~> 5.3.0' # 5.4.0 has a breaking change! https://github.com/norman/friendly_id/pull/787
gem 'geocoder'
gem 'groupdate'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ GEM
devise_invitable (2.0.1)
actionmailer (>= 5.0)
devise (>= 4.6)
discard (1.3.0)
activerecord (>= 4.2, < 8)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.8.1)
Expand Down Expand Up @@ -465,6 +467,7 @@ DEPENDENCIES
devise (~> 4.7)
devise-i18n
devise_invitable (~> 2)
discard (~> 1.2)
dotenv-rails
faker (~> 2.19)
friendly_id (~> 5.3.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def only_admins
def index
@q = User
.includes(avatar_attachment: :blob)
.ransack(params[:q])
.ransack(params[:q], {auth_object: :api})
@users = @q.result(distinct: true).page(params[:page])
end

Expand Down
9 changes: 8 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
include Discard::Model
default_scope { kept }

devise :database_authenticatable, :registerable,
:invitable, :confirmable,
:recoverable, :rememberable, :validatable,
Expand Down Expand Up @@ -31,7 +34,11 @@ class User < ApplicationRecord
# The /users.json endpoint is using Ransack.
# Only allow to search for name, not email.
def self.ransackable_attributes(auth_object = nil)
super & %w(first_name last_name slug)
if auth_object == :api
super & %w(first_name last_name slug)
else
super
end
end

def to_s
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20240110181520_add_discarded_at_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDiscardedAtToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :discarded_at, :datetime
add_index :users, :discarded_at
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_12_01_122728) do
ActiveRecord::Schema.define(version: 2024_01_10_181520) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -433,6 +433,8 @@
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "discarded_at"
t.index ["discarded_at"], name: "index_users_on_discarded_at"
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
t.index ["invitations_count"], name: "index_users_on_invitations_count"
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
Expand Down
18 changes: 18 additions & 0 deletions lib/tasks/makeworks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,22 @@ namespace :makeworks do
end
end
end

desc "Delete spam users from CSV file"
task delete_spam_users: :environment do
require 'csv'
path = ENV.fetch("SPAM_USERS_CSV")
CSV.foreach(path, encoding: "bom|utf-8", liberal_parsing: true, col_sep: ";") do |line|
if line[1] == "TRUE"
line[0].split(";").each do |email|
user = User.find_by(email: email.strip)
if user
user.discard!
else
print "WARN: user not found for email: #{email.strip}"
end
end
end
end
end
end

0 comments on commit 729c42a

Please sign in to comment.