From 812f5b058a3475e3b74780af8a250d941d8326d9 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 18 Nov 2018 18:40:31 +0100 Subject: [PATCH] Fix blocking. --- lib/pleroma/user.ex | 34 ++++++++++++++++++++-------------- lib/pleroma/user/info.ex | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 7b0edee75..3a0f78274 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -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) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index da5e26ebd..cae5562f0 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -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