post "/media/upload", TwitterAPI.Controller, :upload_json
post "/favorites/create/:id", TwitterAPI.Controller, :favorite
post "/favorites/create", TwitterAPI.Controller, :favorite
+ post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
end
end
|> json_reply(200, response)
end
+ def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+ activity = Repo.get(Activity, id)
+ {:ok, status} = TwitterAPI.unfavorite(user, activity)
+ response = Poison.encode!(status)
+
+ conn
+ |> json_reply(200, response)
+ end
+
defp json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")
use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
- alias Pleroma.{Repo, Activity, User}
+ alias Pleroma.{Repo, Activity, User, Object}
+ alias Pleroma.Web.ActivityPub.ActivityPub
import Pleroma.Factory
end
end
+ describe "POST /api/favorites/destroy/:id" do
+ setup [:valid_user]
+ test "without valid credentials", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ ActivityPub.like(current_user, object)
+
+ conn = conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/favorites/destroy/#{note_activity.id}.json")
+
+ assert json_response(conn, 200)
+ end
+ end
+
defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
[user: user]
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)
+ {: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