Fix blocking.

This commit is contained in:
lain 2018-11-18 18:40:31 +01:00
parent 756764266c
commit 812f5b058a
2 changed files with 36 additions and 14 deletions

View file

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

View file

@ -41,4 +41,20 @@ def set_follower_count(info, number) do
|> cast(params, [:follower_count]) |> cast(params, [:follower_count])
|> validate_required([:follower_count]) |> validate_required([:follower_count])
end 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 end