-
Notifications
You must be signed in to change notification settings - Fork 37
/
spam_domains_controller.rb
92 lines (77 loc) · 2.6 KB
/
spam_domains_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true
class SpamDomainsController < ApplicationController
before_action :check_if_smokedetector, only: [:create_from_post]
before_action :authenticate_user!, only: %i[edit update destroy create new]
before_action :verify_core, only: %i[edit update create new fix_asn_tags]
before_action :verify_admin, only: [:destroy]
before_action :set_spam_domain, only: %i[show edit update destroy fix_asn_tags]
def index
@total = SpamDomain.count
@domains = if params[:filter].present?
SpamDomain.where('domain LIKE ?', "%#{params[:filter]}%")
else
SpamDomain.all
end.includes(:domain_tags).order(domain: :asc).paginate(page: params[:page], per_page: 100)
SpamDomain.preload_post_counts(@domains)
end
def fix_asn_tags
@domain.fix_asn_tags
redirect_to action: :show, id: @domain.id
end
def create_from_post
@post = Post.find params[:post_id]
domains = params[:domains]
domains.each do |d|
record = SpamDomain.find_or_create_by domain: d
@post.spam_domains << record
end
render json: { status: 'success', total_domains: @post.spam_domains.count }
end
def show
@counts = { all: SpamDomain.where(id: params[:id]).joins(:posts).count,
tp: SpamDomain.where(id: params[:id]).joins(:posts).where(posts: { is_tp: true }).count }
@posts = @domain.posts.order(created_at: :desc).includes_for_post_row.paginate(page: params[:page], per_page: 100)
@sites = Site.where(id: @posts.map(&:site_id))
end
def edit; end
def new
@domain = SpamDomain.new
end
def create
@domain = SpamDomain.new domain_params
if @domain.save
redirect_to action: :show, id: @domain.id
else
flash[:danger] = @domain.errors.full_messages
redirect_back fallback_location: request.path
end
end
def update
if @domain.update(domain_params.tap { |d| d.delete(:domain) })
flash[:success] = 'Updated successfully.'
redirect_to spam_domain_path(@domain)
else
flash[:danger] = 'Failed to update.'
render :edit
end
end
def destroy
if @domain.destroy
flash[:success] = 'Removed domain.'
redirect_to spam_domains_path
else
flash[:danger] = 'Failed to remove domain.'
render :show
end
end
def query
render json: SpamDomain.where('domain LIKE ?', "%#{params[:q]}%").map { |d| { value: d.id, text: d.domain } }
end
private
def set_spam_domain
@domain = SpamDomain.find params[:id]
end
def domain_params
params.require(:spam_domain).permit(:whois, :domain)
end
end