Fix blocking.
authorlain <lain@soykaf.club>
Sun, 18 Nov 2018 17:40:31 +0000 (18:40 +0100)
committerlain <lain@soykaf.club>
Sun, 18 Nov 2018 17:40:31 +0000 (18:40 +0100)
lib/pleroma/user.ex
lib/pleroma/user/info.ex

index 7b0edee75d3728d2283dff4a5f02a9e9eeda2abc..3a0f782742c6a0d10383cb48de098d3e9a7e54f5 100644 (file)
@@ -550,12 +550,15 @@ defmodule Pleroma.User do
       unfollow(blocked, blocker)
     end
 
-    blocks = blocker.info["blocks"] || []
-    new_blocks = Enum.uniq([ap_id | blocks])
-    new_info = Map.put(blocker.info, "blocks", new_blocks)
+    info_cng =
+      blocker.info
+      |> User.Info.add_to_block(ap_id)
 
-    cs = User.info_changeset(blocker, %{info: new_info})
-    update_and_set_cache(cs)
+    cng =
+      change(blocker)
+      |> put_embed(:info, info_cng)
+
+    update_and_set_cache(cng)
   end
 
   # helper to handle the block given only an actor's AP id
@@ -563,18 +566,21 @@ defmodule Pleroma.User do
     block(blocker, User.get_by_ap_id(ap_id))
   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)
+  def unblock(blocker, %{ap_id: ap_id}) do
+    info_cng =
+      blocker.info
+      |> User.Info.remove_from_block(ap_id)
 
-    cs = User.info_changeset(user, %{info: new_info})
-    update_and_set_cache(cs)
+    cng =
+      change(blocker)
+      |> put_embed(:info, info_cng)
+
+    update_and_set_cache(cng)
   end
 
   def blocks?(user, %{ap_id: ap_id}) do
-    blocks = user.info["blocks"] || []
-    domain_blocks = user.info["domain_blocks"] || []
+    blocks = user.info.blocks
+    domain_blocks = user.info.domain_blocks
     %{host: host} = URI.parse(ap_id)
 
     Enum.member?(blocks, ap_id) ||
@@ -584,7 +590,7 @@ defmodule Pleroma.User do
   end
 
   def block_domain(user, domain) do
-    domain_blocks = user.info["domain_blocks"] || []
+    domain_blocks = user.info.domain_blocks
     new_blocks = Enum.uniq([domain | domain_blocks])
     new_info = Map.put(user.info, "domain_blocks", new_blocks)
 
index da5e26ebd9d363ea10576ebe76b55b5bc737eac0..cae5562f02b5b59c53208f8fcfe0bd713f2cdc12 100644 (file)
@@ -41,4 +41,20 @@ defmodule Pleroma.User.Info do
     |> cast(params, [:follower_count])
     |> validate_required([:follower_count])
   end
+
+  def set_blocks(info, blocks) do
+    params = %{blocks: blocks}
+
+    info
+    |> cast(params, [:blocks])
+    |> validate_required([:blocks])
+  end
+
+  def add_to_block(info, blocked) do
+    set_blocks(info, Enum.uniq([blocked | info.blocks]))
+  end
+
+  def remove_from_block(info, blocked) do
+    set_blocks(info, List.delete(info.blocks, blocked))
+  end
 end