about summary refs log tree commit diff
path: root/app/models/domain_block.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-12-18 08:30:41 +0100
committerGitHub <noreply@github.com>2020-12-18 08:30:41 +0100
commit8a95867693dfe072241d5ddcd0ba7ed8546eab1e (patch)
tree15a9faa3f1f7b7672294f3c2410c3ae706444120 /app/models/domain_block.rb
parentb1feb47055c121c1b6949bd7ef6734bf56c847bd (diff)
Add option to obfuscate domain name in public list of domain blocks (#15355)
- Replace the middle of the domain with * characters (except for periods)
- Add SHA-256 digest of the domain name in tooltip
Diffstat (limited to 'app/models/domain_block.rb')
-rw-r--r--app/models/domain_block.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb
index 829d7583b..bba04c603 100644
--- a/app/models/domain_block.rb
+++ b/app/models/domain_block.rb
@@ -12,6 +12,7 @@
 #  reject_reports  :boolean          default(FALSE), not null
 #  private_comment :text
 #  public_comment  :text
+#  obfuscate       :boolean          default(FALSE), not null
 #
 
 class DomainBlock < ApplicationRecord
@@ -73,4 +74,23 @@ class DomainBlock < ApplicationRecord
     scope = suspend? ? accounts.where(suspended_at: created_at) : accounts.where(silenced_at: created_at)
     scope.count
   end
+
+  def public_domain
+    return domain unless obfuscate?
+
+    length        = domain.size
+    visible_ratio = length / 4
+
+    domain.chars.map.with_index do |chr, i|
+      if i > visible_ratio && i < length - visible_ratio && chr != '.'
+        '*'
+      else
+        chr
+      end
+    end.join
+  end
+
+  def domain_digest
+    Digest::SHA256.hexdigest(domain)
+  end
 end