From 6a5f0871745316b50742ea9ebf3ff7c69881e105 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 21:57:37 +0100 Subject: [PATCH] Add blocks to User. --- lib/pleroma/user.ex | 24 ++++++++++++++++++++++++ test/user_test.exs | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 5f1750035..771c54e81 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -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 diff --git a/test/user_test.exs b/test/user_test.exs index 36ac27656..151b9afc0 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -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