-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Federails::Moderation | ||
class DomainBlock < ApplicationRecord | ||
validates :domain, presence: true, uniqueness: true, "federails/moderation/domain_name": true | ||
|
||
def self.blocked?(query) | ||
self.exists?(domain: [ query, PublicSuffix.parse(query).domain ]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "public_suffix" | ||
|
||
module Federails::Moderation | ||
class DomainNameValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
record.errors.add attribute, :invalid unless PublicSuffix.valid?(value, default_rule: nil) | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
db/migrate/20241128115659_create_federails_moderation_domain_blocks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateFederailsModerationDomainBlocks < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :federails_moderation_domain_blocks do |t| | ||
t.string "domain", null: false, index: { unique: true } | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
RSpec.describe Federails::Moderation::DomainBlock do | ||
[ | ||
"example.com", | ||
"my.website.co.uk", | ||
"Bücher.de", | ||
"кц.рф", | ||
"互联网中心.中国", | ||
"☃.com" | ||
].each do |test| | ||
it "is valid if domain name is #{test.inspect}" do | ||
block = described_class.new(domain: test) | ||
expect(block).to be_valid | ||
end | ||
end | ||
|
||
[ | ||
nil, | ||
"", | ||
"example.invalid", | ||
".example.com" | ||
].each do |test| | ||
it "is invalid if domain name is #{test.inspect}" do | ||
block = described_class.new(domain: test) | ||
expect(block).not_to be_valid | ||
end | ||
end | ||
|
||
context "with a blocked domain" do | ||
before do | ||
described_class.create(domain: "blocked.com") | ||
end | ||
|
||
it "blocks the domain" do | ||
expect(described_class.blocked?("blocked.com")).to be true | ||
end | ||
|
||
it "blocks subdomains" do | ||
expect(described_class.blocked?("www.blocked.com")).to be true | ||
end | ||
|
||
it "allows other domains" do | ||
expect(described_class.blocked?("example.com")).to be false | ||
end | ||
end | ||
end |