Add unfav to Mastodon API.

This commit is contained in:
Roger Braun 2017-09-09 18:30:02 +02:00
parent 454dc18570
commit d625d8db7d
7 changed files with 49 additions and 22 deletions

View file

@ -33,6 +33,17 @@ defmodule Pleroma.Web.CommonAPI 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)

View file

@ -89,7 +89,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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

View file

@ -60,6 +60,7 @@ defmodule Pleroma.Web.Router 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

View file

@ -115,19 +115,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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)

View file

@ -155,12 +155,9 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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

View file

@ -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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest 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

View file

@ -280,7 +280,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest 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