Add blocks to User.
authorRoger Braun <roger@rogerbraun.net>
Thu, 2 Nov 2017 20:57:37 +0000 (21:57 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 2 Nov 2017 20:57:37 +0000 (21:57 +0100)
lib/pleroma/user.ex
test/user_test.exs

index 5f1750035127b77ff9867af0bd145bf2a6a07daf..771c54e813e51f73bce54d0acde84d379698393a 100644 (file)
@@ -293,4 +293,28 @@ defmodule Pleroma.User do
       limit: 20
     Repo.all(q)
   end
+
+  def block(user, %{ap_id: ap_id}) do
+    blocks = user.info["blocks"] || []
+    new_blocks = Enum.uniq([ap_id | blocks])
+    new_info = Map.put(user.info, "blocks", new_blocks)
+
+    cs = User.info_changeset(user, %{info: new_info})
+    Repo.update(cs)
+  end
+
+  def unblock(user, %{ap_id: ap_id}) do
+    blocks = user.info["blocks"] || []
+    new_blocks = List.delete(blocks, ap_id)
+    new_info = Map.put(user.info, "blocks", new_blocks)
+
+    cs = User.info_changeset(user, %{info: new_info})
+    Repo.update(cs)
+  end
+
+  def blocks?(user, %{ap_id: ap_id}) do
+    blocks = user.info["blocks"] || []
+    Enum.member?(blocks, ap_id)
+  end
+
 end
index 36ac2765656954f0dc83de17ffacd4af6781cda4..151b9afc04d482c732ef208b7be37e49db5e8e8c 100644 (file)
@@ -273,5 +273,28 @@ defmodule Pleroma.UserTest do
       assert user.info["follower_count"] == 1
     end
   end
+
+  describe "blocks" do
+    test "it blocks people" do
+      user = insert(:user)
+      blocked_user = insert(:user)
+
+      refute User.blocks?(user, blocked_user)
+
+      {:ok, user} = User.block(user, blocked_user)
+
+      assert User.blocks?(user, blocked_user)
+    end
+
+    test "it unblocks users" do
+      user = insert(:user)
+      blocked_user = insert(:user)
+
+      {:ok, user} = User.block(user, blocked_user)
+      {:ok, user} = User.unblock(user, blocked_user)
+
+      refute User.blocks?(user, blocked_user)
+    end
+  end
 end