From f4eea0847b4bfcc2a2e6e0c80480ca5818f17aa9 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 14 Apr 2017 18:15:15 +0200 Subject: [PATCH] Add unfavoriting to TwAPI. --- lib/pleroma/web/twitter_api/twitter_api.ex | 13 +++++++++++++ test/web/twitter_api/twitter_api_test.exs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d6ef0f4ed..a23f465d4 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -137,6 +137,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do {:ok, status} end + def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do + object = Object.get_by_ap_id(object["id"]) + + {:ok, object} = ActivityPub.unlike(user, object) + new_data = activity.data + |> Map.put("object", object.data) + + status = %{activity | data: new_data} + |> activity_to_status(%{for: user}) + + {:ok, status} + end + def upload(%Plug.Upload{} = file, format \\ "xml") do {:ok, object} = ActivityPub.upload(file) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 040a392e5..88e6dd684 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -4,6 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.{Activity, User, Object, Repo} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter + alias Pleroma.Web.ActivityPub.ActivityPub import Pleroma.Factory @@ -191,6 +192,22 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user}) end + test "it unfavorites a status, returns the updated status" do + user = insert(:user) + note_activity = insert(:note_activity) + activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) + object = Object.get_by_ap_id(note_activity.data["object"]["id"]) + + {:ok, like_activity, object } = ActivityPub.like(user, object) + updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) + assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1 + + {:ok, status} = TwitterAPI.unfavorite(user, note_activity) + updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) + + assert status["fave_num"] == 0 + end + setup do Supervisor.terminate_child(Pleroma.Supervisor, ConCache) Supervisor.restart_child(Pleroma.Supervisor, ConCache)