From 7b4f55238eeb8561c6a8e43321cd965667cefabe Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Wed, 18 Apr 2018 06:00:40 -0400 Subject: [PATCH] Handle unrepeats via the TwitterAPI --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../mastodon_api/mastodon_api_controller.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 18 ++++++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 10 ++-------- test/web/activity_pub/activity_pub_test.exs | 4 +++- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 3b918e28f..dccd2e757 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -146,7 +146,7 @@ def unannounce(%User{} = actor, %Object{} = object, local \\ true) do {:ok, unannounce_activity} <- insert(unannounce_data, local), {:ok, _activity} <- Repo.delete(activity), {:ok, object} <- remove_announce_from_object(activity, object) do - {:ok, unannounce_activity, object} + {:ok, unannounce_activity, activity, object} else _e -> {:ok, object} end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index c01552410..ebd587d26 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -297,7 +297,7 @@ def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do end def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do - with {:ok, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user), + with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}) end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index c12cd7f8a..b6ae7c7f7 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -12,6 +12,18 @@ def create_status(%User{} = user, %{"status" => _} = data) do CommonAPI.post(user, data) end + def delete(%User{} = user, id) do + # TwitterAPI does not have an "unretweet" endpoint; instead this is done + # via the "destroy" endpoint. Therefore, there is a need to handle + # when the status to "delete" is actually an Announce (repeat) object. + with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id) do + case type do + "Announce" -> unrepeat(user, id) + _ -> CommonAPI.delete(id, user) + end + end + end + def follow(%User{} = follower, params) do with {:ok, %User{} = followed} <- get_user(params), {:ok, follower} <- User.follow(follower, followed), @@ -64,6 +76,12 @@ def repeat(%User{} = user, ap_id_or_id) do end end + defp unrepeat(%User{} = user, ap_id_or_id) do + with {:ok, _unannounce, activity, _object} <- CommonAPI.unrepeat(ap_id_or_id, user) do + {:ok, activity} + end + end + def fav(%User{} = user, ap_id_or_id) do with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 986436326..e1c1cb5d4 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -150,8 +150,8 @@ def unblock(%{assigns: %{user: user}} = conn, params) do end def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do - with {:ok, delete} <- CommonAPI.delete(id, user) do - render(conn, ActivityView, "activity.json", %{activity: delete, for: user}) + with {:ok, activity} <- TwitterAPI.delete(id, user) do + render(conn, ActivityView, "activity.json", %{activity: activity, for: user}) end end @@ -229,12 +229,6 @@ def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do end end - def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do - with {:ok, activity} <- TwitterAPI.unrepeat(user, id) do - render(conn, ActivityView, "activity.json", %{activity: activity, for: user}) - end - end - def register(conn, params) do with {:ok, user} <- TwitterAPI.register_user(params) do render(conn, UserView, "show.json", %{user: user}) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 85a6aecf0..6a07da775 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -284,9 +284,11 @@ test "unannouncing a previously announced object" do {:ok, announce_activity, object} = ActivityPub.announce(user, object) assert object.data["announcement_count"] == 1 - {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object) + {:ok, unannounce_activity, activity, object} = ActivityPub.unannounce(user, object) assert object.data["announcement_count"] == 0 + assert activity == announce_activity + assert unannounce_activity.data["to"] == [ User.ap_followers(user), note_activity.data["actor"]