Commit d625d8db authored by lain's avatar lain
Browse files

Add unfav to Mastodon API.

parent 454dc185
......@@ -33,6 +33,17 @@ def favorite(id_or_ap_id, user) do
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)
......
......@@ -89,7 +89,14 @@ def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
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
......
......@@ -60,6 +60,7 @@ def user_fetcher(username) do
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
......
......@@ -115,19 +115,6 @@ def unfollow(%User{} = follower, params) 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),
......@@ -144,6 +131,14 @@ def fav(%User{} = user, ap_id_or_id) do
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)
......
......@@ -155,12 +155,9 @@ def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
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
......
......@@ -3,7 +3,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.{Repo, User, Activity}
alias Pleroma.Web.OStatus
alias Pleroma.Web.{OStatus, CommonAPI}
import Pleroma.Factory
......@@ -149,4 +149,20 @@ test "favs a status and returns it", %{conn: conn} do
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
......@@ -280,7 +280,7 @@ test "it unfavorites a status, returns the updated status" do
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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment