Skip to content

Commit

Permalink
domain banning
Browse files Browse the repository at this point in the history
  • Loading branch information
pushcx committed Feb 10, 2020
1 parent 9b4382a commit 630ace4
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Expand Up @@ -227,7 +227,7 @@ def for_domain

@heading = params[:name]
@title = "Stories submitted for #{@domain.domain}."
@cur_url = for_domain_url(params[:name])
@cur_url = domain_path(params[:name])

@rss_link = {
title: "RSS 2.0 - For #{@domain}",
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/moderations_controller.rb
Expand Up @@ -11,10 +11,11 @@ def index
:comments => params.dig(:what, :comments),
:tags => params.dig(:what, :tags),
:users => params.dig(:what, :users),
:domains => params.dig(:what, :domains),
}
@what.transform_values! { true } if @what.values.none?

@moderations = Moderation.all.eager_load(:moderator, :story, :comment, :tag, :user)
@moderations = Moderation.all.eager_load(:moderator, :story, :comment, :tag, :user, :domain)

# filter based on target
@moderations = case @moderator
Expand Down
26 changes: 26 additions & 0 deletions app/models/domain.rb
@@ -1,6 +1,32 @@
class Domain < ApplicationRecord
has_many :stories # rubocop:disable Rails/HasManyOrHasOneDependent
belongs_to :banned_by_user,
:class_name => "User",
:inverse_of => false,
:optional => true
validates :banned_reason, :length => { :maximum => 200 }

validates :domain, presence: true
validates :is_tracker, inclusion: { in: [true, false] }

def ban_by_user_for_reason!(banner, reason)
self.banned_at = Time.current
self.banned_by_user_id = banner.id
self.banned_reason = reason

m = Moderation.new
m.moderator_user_id = banner.id
m.domain = self
m.action = "Banned"
m.reason = reason
m.save!
end

def banned?
banned_at?
end

def to_param
domain
end
end
14 changes: 14 additions & 0 deletions app/models/mod_note.rb
Expand Up @@ -45,4 +45,18 @@ def self.create_from_message(message, moderator)
NOTE
)
end

def self.tattle_on_story_domain!(story, reason)
ModNote.create!(
moderator: InactiveUser.inactive_user,
user: story.user,
created_at: Time.current,
note: "Attempted to post a story from a #{reason} domain:\n" +
"url: #{story.url}" +
"title: #{story.title}" +
"user_is_author: #{story.user_id_author}" +
"tags: #{story.tags.map(&:tag).join(' ')}" +
"text: #{text}"
)
end
end
2 changes: 2 additions & 0 deletions app/models/moderation.rb
Expand Up @@ -6,6 +6,8 @@ class Moderation < ApplicationRecord
:optional => true
belongs_to :comment,
:optional => true
belongs_to :domain,
:optional => true
belongs_to :story,
:optional => true
belongs_to :tag,
Expand Down
15 changes: 6 additions & 9 deletions app/models/story.rb
Expand Up @@ -183,16 +183,13 @@ def already_posted_recently?
end

def check_not_tracking_domain
return unless self.url.present? && self.new_record?

if domain && domain.is_tracker
ModNote.create!(
moderator: InactiveUser.inactive_user,
user: self.user,
created_at: Time.current,
note: "Attempted to post a tracking url: #{self.url}"
)
return unless self.url.present? && self.new_record? && self.domain

if domain.banned?
ModNote.tattle_on_story_domain!(self, "banned")
errors.add(:url, "is from banned domain #{domain.domain}: #{domain.banned_reason}")
elsif domain.is_tracker
ModNote.tattle_on_story_domain!(self, "tracking")
errors.add(:url, "is a link shortening or ad tracking domain")
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -412,7 +412,7 @@ def good_riddance?
(self.comments.where('created_at >= now() - interval 30 day AND is_moderated').count +
self.stories.where('created_at >= now() - interval 30 day AND is_expired AND is_moderated')
.count >= 3) ||
DownvotedCommenters.new('90d').check_list_for(self).try(:[], :rank).try(:<, 10)
DownvotedCommenters.new('90d').check_list_for(self)
end

def grant_moderatorship_by_user!(user)
Expand Down
2 changes: 2 additions & 0 deletions app/views/moderations/_table.html.erb
Expand Up @@ -30,6 +30,8 @@
<% else %>
User <%= mod.user_id %> (Deleted)
<% end %>
<% elsif mod.domain %>
Domain <%= link_to mod.domain.domain, domain_path(mod.domain) %>
<% end %></td>
</tr>
<tr class="<%= mod.reason.present?? "nobottom" : "" %>">
Expand Down
6 changes: 2 additions & 4 deletions app/views/stories/_listdetail.html.erb
Expand Up @@ -42,8 +42,7 @@ class="story <%= story.vote && story.vote[:vote] == 1 ? "upvoted" : "" %>
<% end %>
</span>
<% if story.domain.present? %>
<a class="domain" href="<%= for_domain_url(name: story.domain.domain) %>"><%=
break_long_words(story.domain.domain) %></a>
<a class="domain" href="<%= domain_path(story.domain) %>"><%= break_long_words(story.domain.domain) %></a>
<% end %>
<% if defined?(single_story) && single_story %>
Expand All @@ -62,8 +61,7 @@ class="story <%= story.vote && story.vote[:vote] == 1 ? "upvoted" : "" %>
<% end %>
</span>
<% if ms.domain.present? %>
<a class="domain" href="<%= for_domain_url(name: ms.domain.domain) %>"><%=
break_long_words(ms.domain.domain) %></a>
<a class="domain" href="<%= domain_path(ms.domain) %>"><%= break_long_words(ms.domain.domain) %></a>
<% end %>
<span class="byline">
<% if (@user && @user.show_avatars?) || !@user %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -66,7 +66,7 @@
get "/t/:tag" => "home#tagged", :as => "tag"
get "/t/:tag/page/:page" => "home#tagged"

get "/domain/:name" => "home#for_domain", :as => "for_domain", :constraints => { name: /[^\/]+/ }
get "/domain/:name" => "home#for_domain", :as => "domain", :constraints => { name: /[^\/]+/ }
get "/domain/:name/page/:page" => "home#for_domain", :constraints => { name: /[^\/]+/ }

get "/search" => "search#index"
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20200210125425_add_domain_banning.rb
@@ -0,0 +1,9 @@
class AddDomainBanning < ActiveRecord::Migration[5.2]
def change
add_column :domains, :banned_at, :datetime, null: true, default: nil
add_column :domains, :banned_by_user_id, :integer, null: true, default: nil
add_column :domains, :banned_reason, :string, :limit => 200
add_column :moderations, :domain_id, :integer, null: true, default: nil
add_index :moderations, :domain_id
end
end
9 changes: 7 additions & 2 deletions db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_27_234358) do
ActiveRecord::Schema.define(version: 2020_02_10_125425) do

create_table "comments", id: :bigint, unsigned: true, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.datetime "created_at", null: false
Expand Down Expand Up @@ -40,11 +40,14 @@
t.index ["user_id"], name: "index_comments_on_user_id"
end

create_table "domains", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "domains", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "domain"
t.boolean "is_tracker", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "banned_at"
t.integer "banned_by_user_id"
t.string "banned_reason", limit: 200
end

create_table "hat_requests", id: :bigint, unsigned: true, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
Expand Down Expand Up @@ -146,8 +149,10 @@
t.text "reason", limit: 16777215
t.boolean "is_from_suggestions", default: false
t.bigint "tag_id", unsigned: true
t.integer "domain_id"
t.index ["comment_id"], name: "moderations_comment_id_fk"
t.index ["created_at"], name: "index_moderations_on_created_at"
t.index ["domain_id"], name: "index_moderations_on_domain_id"
t.index ["moderator_user_id"], name: "moderations_moderator_user_id_fk"
t.index ["story_id"], name: "moderations_story_id_fk"
t.index ["tag_id"], name: "moderations_tag_id_fk"
Expand Down

0 comments on commit 630ace4

Please sign in to comment.