From: eal Date: Tue, 7 Nov 2017 22:04:53 +0000 (+0200) Subject: TwitterAPI: Add blocks. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=6c0758e041e63d9a8792d2fe7760813f074bc06e;p=akkoma TwitterAPI: Add blocks. --- diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index b43235d8d..0a0aea966 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 912d5e278..baa3dac96 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -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), diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 6154d5ad7..2604b54dc 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -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}) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 798309f7d..6bdf29a13 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -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" diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 1ec9ef388..994cc8f90 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -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"})