user.ex: add domain blocks.
authoreal <eal@waifu.club>
Sun, 3 Jun 2018 19:01:37 +0000 (22:01 +0300)
committereal <eal@waifu.club>
Sun, 3 Jun 2018 19:01:37 +0000 (22:01 +0300)
lib/pleroma/user.ex
test/user_test.exs

index b1b935a0f4e95919f2a10e31999c976b16a48915..c8e61f8a9f1079735c44352ae807035c3b67c5a3 100644 (file)
@@ -479,7 +479,29 @@ defmodule Pleroma.User do
 
   def blocks?(user, %{ap_id: ap_id}) do
     blocks = user.info["blocks"] || []
-    Enum.member?(blocks, ap_id)
+    domain_blocks = user.info["domain_blocks"] || []
+    %{host: host} = URI.parse(ap_id)
+    Enum.member?(blocks, ap_id) || Enum.any?(domain_blocks, fn domain ->
+      host == domain
+    end)
+  end
+
+  def block_domain(user, domain) do
+    domain_blocks = user.info["domain_blocks"] || []
+    new_blocks = Enum.uniq([domain | domain_blocks])
+    new_info = Map.put(user.info, "domain_blocks", new_blocks)
+
+    cs = User.info_changeset(user, %{info: new_info})
+    update_and_set_cache(cs)
+  end
+
+  def unblock_domain(user, domain) do
+    blocks = user.info["domain_blocks"] || []
+    new_blocks = List.delete(blocks, domain)
+    new_info = Map.put(user.info, "domain_blocks", new_blocks)
+
+    cs = User.info_changeset(user, %{info: new_info})
+    update_and_set_cache(cs)
   end
 
   def local_user_query() do
index 8c8cfd673fe31237e3fddda45e21344e95c606d9..200352981cf12f98b91271fbcad45a9135c6b742 100644 (file)
@@ -361,6 +361,27 @@ defmodule Pleroma.UserTest do
     end
   end
 
+  describe "domain blocking" do
+    test "blocks domains" do
+      user = insert(:user)
+      collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
+
+      {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
+
+      assert User.blocks?(user, collateral_user)
+    end
+
+    test "unblocks domains" do
+      user = insert(:user)
+      collateral_user = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/bully"})
+
+      {:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
+      {:ok, user} = User.unblock_domain(user, "awful-and-rude-instance.com")
+
+      refute User.blocks?(user, collateral_user)
+    end
+  end
+
   test "get recipients from activity" do
     actor = insert(:user)
     user = insert(:user, local: true)