Skip to content

Commit f0c6bdc

Browse files
committed
created some scripts to clean spam, orphaned data, and dups
1 parent 30bb78e commit f0c6bdc

File tree

6 files changed

+111
-8
lines changed

6 files changed

+111
-8
lines changed

README.rdoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
## Features remaining prior release
22

33
* TODO: Getting avatars/uploads/cdn to work
4-
* TODO: Clean up (all protips & comments without author, all spam protips like PST tips)
5-
* TDOD: Clean up duplicate badges
64
* TODO: Fix commenting formatting issue (see: http://localhost:5000/p/lhsrcq/one-line-browser-notepad)
75
* TODO: Write announcement protip and link it on homepage
86

app/controllers/protips_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ def index
66
@protips = Protip.includes(:user).order({order_by => :desc}).page params[:page]
77
end
88

9+
def spam
10+
@protips = Protip.spam.page params[:page]
11+
render action: 'index'
12+
end
13+
914
def show
1015
return (@protip = Protip.random.first) if params[:id] == 'random'
1116
@protip = Protip.find_by_public_id!(params[:id])

app/models/protip.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ def to_param
2525
self.public_id
2626
end
2727

28+
def self.spam
29+
spammy = "
30+
title ILIKE '% OST %' OR
31+
title ILIKE '% PST %' OR
32+
title ILIKE '%exchange mailbox%' OR
33+
title ILIKE '% loans %' OR
34+
title ILIKE '%Exchange Migration%'
35+
"
36+
where(spammy)
37+
end
38+
2839
def display_date
2940
created_at.to_formatted_s(:explicitly_bold)
3041
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
resources :protips, path: '/p' do
4343
resources :likes, only: :create
4444
collection do
45+
get '/spam' => 'protips#spam'
4546
get '/:id/:slug' => 'protips#show', as: :slug, :constraints => { slug: /(?!.*?edit).*/ }
4647
end
4748
end

lib/coderwall_flavored_markdown.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class CoderwallFlavoredMarkdown < Redcarpet::Render::HTML
2+
3+
def self.render_to_html(text)
4+
return nil if text.blank?
5+
6+
extensions = {
7+
fenced_code_blocks: true,
8+
strikethrough: true,
9+
autolink: true
10+
}
11+
12+
renderer = CoderwallFlavoredMarkdown.new(link_attributes: {rel: "nofollow"})
13+
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
14+
redcarpet.render(text)
15+
end
16+
17+
# def preprocess(text)
18+
# turn_gist_links_into_embeds!(text)
19+
# end
20+
#
21+
# USERNAME_BLACKLIST = %w(include)
22+
# def coderwall_user_link(username)
23+
# (User.where(username: username).exists? && !USERNAME_BLACKLIST.include?(username)) ? ActionController::Base.helpers.link_to("@#{username}", "/#{username}") : "@#{username}"
24+
# end
25+
#
26+
# def inspect_line(line)
27+
# #hotlink coderwall usernames to their profile, but don't search for @mentions in code blocks
28+
# if line.start_with?(' ')
29+
# line
30+
# else
31+
# line.gsub(/((?<!\s{4}).*)@([a-zA-Z_\-0-9]+)/) { $1+coderwall_user_link($2) }
32+
# end
33+
# end
34+
#
35+
# def postprocess(text)
36+
# embed_gists!(text)
37+
# end
38+
#
39+
# def turn_gist_links_into_embeds!(text)
40+
# text.gsub! /https?:\/\/gist\.github\.com\/(.*?)(\.js)?/ do
41+
# "[gist #{Regexp.last_match.to_s}]"
42+
# end
43+
# raise text
44+
# end
45+
#
46+
# def embed_gists!(text)
47+
# raise text
48+
# text.gsub! /\[gist ([\w|\/]*)\]/ do
49+
# "<script src='https://gist.github.com/#{$1}.js'></script>"
50+
# end
51+
# end
52+
53+
end

lib/tasks/db.rake

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,46 @@ namespace :db do
2020
end
2121
end
2222

23-
task :clean => :environment do
24-
Like.delete_all
25-
Comment.delete_all
26-
Protip.delete_all
27-
Badge.delete_all
28-
User.delete_all
23+
namespace :clean do
24+
task :spam => :environment do
25+
spammers = Protip.spam.collect(&:user)
26+
spammers.each do |spammer|
27+
puts "Destroying spammer: #{spammer.username}"
28+
spammer.destroy
29+
end
30+
end
31+
32+
task :orphans => :environment do
33+
[Comment, Protip, Like].each do |klass|
34+
count = Klass.where("user_id NOT IN (select id from users)").count
35+
puts "#{klass}: deleting #{count} orphans"
36+
puts Klass.where("user_id NOT IN (select id from users)").delete_all
37+
end
38+
end
39+
40+
task :duplicates => :environment do
41+
puts "Badges#count prior cleaning duplicates: #{Badge.count}"
42+
Badge.connection.execute('
43+
DELETE FROM badges a USING (
44+
SELECT MIN(ctid) as ctid, user_id, name, created_at FROM badges
45+
GROUP BY user_id, name, created_at HAVING COUNT(*) > 1
46+
) b
47+
WHERE
48+
a.user_id = b.user_id AND
49+
a.name = b.name AND
50+
a.user_id = b.user_id AND
51+
a.ctid <> b.ctid
52+
')
53+
puts "Badges#count post cleaning duplicates: #{Badge.count}"
54+
end
55+
56+
task :data => :environment do
57+
Like.delete_all
58+
Comment.delete_all
59+
Protip.delete_all
60+
Badge.delete_all
61+
User.delete_all
62+
end
2963
end
3064

3165
namespace :port do
@@ -96,6 +130,7 @@ namespace :db do
96130
if LEGACY_BADGES[row[:badge_class_name]].nil?
97131
raise row[:badge_class_name].inspect
98132
end
133+
puts "Importing #{row[:id]}"
99134

100135
badge = Badge.find_or_initialize_by_id(row[:id])
101136
badge.user_id = row[:user_id]

0 commit comments

Comments
 (0)