Add favoriting to Mastodon API.

This commit is contained in:
Roger Braun 2017-09-09 18:09:37 +02:00
parent 66e4c710d4
commit 454dc18570
8 changed files with 47 additions and 21 deletions

View File

@ -22,6 +22,17 @@ def repeat(id_or_ap_id, user) do
end end
end end
def favorite(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.like(user, object)
else
_ ->
{:error, "Could not favorite"}
end
end
# This is a hack for twidere. # This is a hack for twidere.
def get_by_id_or_ap_id(id) do def get_by_id_or_ap_id(id) do
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id) activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)

View File

@ -87,4 +87,11 @@ def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity} render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
end end
end 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),
%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
end end

View File

@ -22,6 +22,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
|> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end) |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || []) repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
%{ %{
id: activity.id, id: activity.id,
@ -36,7 +37,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
reblogs_count: announcement_count, reblogs_count: announcement_count,
favourites_count: like_count, favourites_count: like_count,
reblogged: !!repeated, reblogged: !!repeated,
favourited: false, # fix favourited: !!favorited,
muted: false, muted: false,
sensitive: sensitive, sensitive: sensitive,
spoiler_text: "", spoiler_text: "",

View File

@ -59,6 +59,7 @@ def user_fetcher(username) do
delete "/statuses/:id", MastodonAPIController, :delete_status delete "/statuses/:id", MastodonAPIController, :delete_status
post "/statuses/:id/reblog", MastodonAPIController, :reblog_status post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
post "/statuses/:id/favourite", MastodonAPIController, :fav_status
end end
scope "/api", Pleroma.Web do scope "/api", Pleroma.Web do

View File

@ -115,19 +115,6 @@ def unfollow(%User{} = follower, params) do
end end
end end
def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
object = Object.get_by_ap_id(object["id"])
{:ok, _like_activity, object} = ActivityPub.like(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 unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
object = Object.get_by_ap_id(object["id"]) object = Object.get_by_ap_id(object["id"])
@ -149,6 +136,14 @@ def repeat(%User{} = user, ap_id_or_id) do
end end
end end
def fav(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(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 def upload(%Plug.Upload{} = file, format \\ "xml") do
{:ok, object} = ActivityPub.upload(file) {:ok, object} = ActivityPub.upload(file)

View File

@ -149,12 +149,9 @@ def get_by_id_or_ap_id(id) do
end end
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
activity = get_by_id_or_ap_id(id) with {:ok, status} <- TwitterAPI.fav(user, id) do
{:ok, status} = TwitterAPI.favorite(user, activity) json(conn, status)
response = Poison.encode!(status) end
conn
|> json_reply(200, response)
end end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do

View File

@ -135,4 +135,18 @@ test "reblogs and returns the reblogged status", %{conn: conn} do
assert activity.id == id assert activity.id == id
end end
end end
describe "favoriting" do
test "favs a status and returns it", %{conn: conn} do
activity = insert(:note_activity)
user = insert(:user)
conn = conn
|> assign(:user, user)
|> post("/api/v1/statuses/#{activity.id}/favourite")
assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
assert activity.id == id
end
end
end end

View File

@ -264,7 +264,7 @@ test "it favorites a status, returns the updated status" do
note_activity = insert(:note_activity) note_activity = insert(:note_activity)
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
{:ok, status} = TwitterAPI.favorite(user, note_activity) {:ok, status} = TwitterAPI.fav(user, note_activity.id)
updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user}) assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})