TwitterAPI: Add blocks.
authoreal <eal@waifu.club>
Tue, 7 Nov 2017 22:04:53 +0000 (00:04 +0200)
committereal <eal@waifu.club>
Tue, 7 Nov 2017 22:04:53 +0000 (00:04 +0200)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index b43235d8db28d3ca92a27f8b659c858fef75d8dc..0a0aea96622c13b435e92b846a8d771f0143e63d 100644 (file)
@@ -164,6 +164,8 @@ defmodule Pleroma.Web.Router do
 
     post "/friendships/create", TwitterAPI.Controller, :follow
     post "/friendships/destroy", TwitterAPI.Controller, :unfollow
+    post "/blocks/create", TwitterAPI.Controller, :block
+    post "/blocks/destroy", TwitterAPI.Controller, :unblock
 
     post "/statusnet/media/upload", TwitterAPI.Controller, :upload
     post "/media/upload", TwitterAPI.Controller, :upload_json
index 912d5e278b01676eef4e97e3d979b79505c929cf..baa3dac96d911194eb2577055901b9659f982893 100644 (file)
@@ -86,6 +86,26 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
+  def block(%User{} = blocker, params) do
+    with {:ok, %User{} = blocked} <- get_user(params),
+         {:ok, blocker} <- User.block(blocker, blocked)
+    do
+      {:ok, blocker, blocked}
+    else
+      err -> err
+    end
+  end
+
+  def unblock(%User{} = blocker, params) do
+    with {:ok, %User{} = blocked} <- get_user(params),
+         {:ok, blocker} <- User.unblock(blocker, blocked)
+    do
+      {:ok, blocker, blocked}
+    else
+      err -> err
+    end
+  end
+
   def repeat(%User{} = user, ap_id_or_id) do
     with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
index 6154d5ad76b220ed1ca40b922bd19c9224389a0d..2604b54dcdb55bd9739b784bac955935f93d210b 100644 (file)
@@ -93,6 +93,22 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     end
   end
 
+  def block(%{assigns: %{user: user}} = conn, params) do
+    case TwitterAPI.block(user, params) do
+      {:ok, user, blocked} ->
+        render conn, UserView, "show.json", %{user: blocked, for: user}
+      {:error, msg} -> forbidden_json_reply(conn, msg)
+    end
+  end
+
+  def unblock(%{assigns: %{user: user}} = conn, params) do
+    case TwitterAPI.unblock(user, params) do
+      {:ok, user, blocked} ->
+        render conn, UserView, "show.json", %{user: blocked, for: user}
+      {:error, msg} -> forbidden_json_reply(conn, msg)
+    end
+  end
+
   def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
     with {:ok, delete} <- CommonAPI.delete(id, user) do
       json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
index 798309f7d2505fc631f8273e3a3ef72754ff8179..6bdf29a132c54b14bcb72dfedba61f059fec94d8 100644 (file)
@@ -277,6 +277,49 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /blocks/create.json" do
+    setup [:valid_user]
+    test "without valid credentials", %{conn: conn} do
+      conn = post conn, "/api/blocks/create.json"
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      blocked = insert(:user)
+
+      conn = conn
+      |> with_credentials(current_user.nickname, "test")
+      |> post("/api/blocks/create.json", %{user_id: blocked.id})
+
+      current_user = Repo.get(User, current_user.id)
+      assert User.blocks?(current_user, blocked)
+      assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user})
+    end
+  end
+
+  describe "POST /blocks/destroy.json" do
+    setup [:valid_user]
+    test "without valid credentials", %{conn: conn} do
+      conn = post conn, "/api/blocks/destroy.json"
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      blocked = insert(:user)
+
+      {:ok, current_user} = User.block(current_user, blocked)
+      assert User.blocks?(current_user, blocked)
+
+      conn = conn
+      |> with_credentials(current_user.nickname, "test")
+      |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
+
+      current_user = Repo.get(User, current_user.id)
+      assert current_user.info["blocks"] == []
+      assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user})
+    end
+  end
+
   describe "GET /help/test.json" do
     test "returns \"ok\"", %{conn: conn} do
       conn = get conn, "/api/help/test.json"
index 1ec9ef38836b9f776fe9494cb592dfb05b27a9a5..994cc8f90cf7eb5cd5ae1dbf9fabbab49160666b 100644 (file)
@@ -224,6 +224,40 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert msg == "Not subscribed!"
   end
 
+  test "Block another user using user_id" do
+    user = insert(:user)
+    blocked = insert(:user)
+
+    {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
+    assert User.blocks?(user, blocked)
+  end
+
+  test "Block another user using screen_name" do
+    user = insert(:user)
+    blocked = insert(:user)
+
+    {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
+    assert User.blocks?(user, blocked)
+  end
+
+  test "Unblock another user using user_id" do
+    unblocked = insert(:user)
+    user = insert(:user)
+    User.block(user, unblocked)
+
+    {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
+    assert user.info["blocks"] == []
+  end
+
+  test "Unblock another user using screen_name" do
+    unblocked = insert(:user)
+    user = insert(:user)
+    User.block(user, unblocked)
+
+    {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
+    assert user.info["blocks"] == []
+  end
+
   test "fetch statuses in a context using the conversation id" do
     {:ok, user} = UserBuilder.insert()
     {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})