end
end
+ def unfavorite(id_or_ap_id, user) do
+ with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
+ false <- activity.data["actor"] == user.ap_id,
+ object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
+ ActivityPub.unlike(user, object)
+ else
+ _ ->
+ {:error, "Could not unfavorite"}
+ end
+ end
+
# This is a hack for twidere.
def get_by_id_or_ap_id(id) do
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
end
def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
- with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
+ with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(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
+ end
+
+ def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
+ with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(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
post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
post "/statuses/:id/favourite", MastodonAPIController, :fav_status
+ post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status
end
scope "/api", Pleroma.Web do
end
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 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),
end
end
+ def unfav(%User{} = user, ap_id_or_id) do
+ with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
+ status <- activity_to_status(activity, %{for: user}) do
+ {:ok, status}
+ end
+ end
+
def upload(%Plug.Upload{} = file, format \\ "xml") do
{:ok, object} = ActivityPub.upload(file)
end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- activity = get_by_id_or_ap_id(id)
- {:ok, status} = TwitterAPI.unfavorite(user, activity)
- response = Poison.encode!(status)
-
- conn
- |> json_reply(200, response)
+ with {:ok, status} <- TwitterAPI.unfav(user, id) do
+ json(conn, status)
+ end
end
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.{Repo, User, Activity}
- alias Pleroma.Web.OStatus
+ alias Pleroma.Web.{OStatus, CommonAPI}
import Pleroma.Factory
assert activity.id == id
end
end
+
+ describe "unfavoriting" do
+ test "unfavorites a status and returns it", %{conn: conn} do
+ activity = insert(:note_activity)
+ user = insert(:user)
+
+ {:ok, _, _} = CommonAPI.favorite(activity.id, user)
+
+ conn = conn
+ |> assign(:user, user)
+ |> post("/api/v1/statuses/#{activity.id}/unfavourite")
+
+ assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200)
+ assert activity.id == id
+ end
+ end
end
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)
+ {:ok, status} = TwitterAPI.unfav(user, note_activity.id)
assert status["fave_num"] == 0
end