MastodonAPI: Add user favorites endpoint.

This commit is contained in:
Roger Braun 2017-09-17 13:09:49 +02:00
parent 6d5bd4dcc0
commit 422d0f324f
4 changed files with 38 additions and 0 deletions

View file

@ -147,6 +147,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
defp restrict_type(query, _), do: query defp restrict_type(query, _), do: query
defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
from activity in query,
where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
end
defp restrict_favorited_by(query, _), do: query
def fetch_activities(recipients, opts \\ %{}) do def fetch_activities(recipients, opts \\ %{}) do
base_query = from activity in Activity, base_query = from activity in Activity,
limit: 20, limit: 20,
@ -160,6 +166,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> restrict_max(opts) |> restrict_max(opts)
|> restrict_actor(opts) |> restrict_actor(opts)
|> restrict_type(opts) |> restrict_type(opts)
|> restrict_favorited_by(opts)
|> Repo.all |> Repo.all
|> Enum.reverse |> Enum.reverse
end end

View file

@ -312,6 +312,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, res) json(conn, res)
end end
def favourites(%{assigns: %{user: user}} = conn, params) do
params = conn
|> Map.put("type", "Create")
|> Map.put("favorited_by", user.ap_id)
activities = ActivityPub.fetch_activities([], params)
|> Enum.reverse
conn
|> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
end
def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
Logger.debug("Unimplemented, returning unmodified relationship") Logger.debug("Unimplemented, returning unmodified relationship")
with %User{} = target <- Repo.get(User, id) do with %User{} = target <- Repo.get(User, id) do

View file

@ -58,6 +58,8 @@ defmodule Pleroma.Web.Router do
get "/timelines/home", MastodonAPIController, :home_timeline get "/timelines/home", MastodonAPIController, :home_timeline
get "/favourites", MastodonAPIController, :favourites
post "/statuses", MastodonAPIController, :post_status post "/statuses", MastodonAPIController, :post_status
delete "/statuses/:id", MastodonAPIController, :delete_status delete "/statuses/:id", MastodonAPIController, :delete_status

View file

@ -341,4 +341,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
[account] = results["accounts"] [account] = results["accounts"]
assert account["acct"] == "shp@social.heldscal.la" assert account["acct"] == "shp@social.heldscal.la"
end end
test "returns the favorites of a user", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
{:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
{:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
{:ok, _, _} = CommonAPI.favorite(activity.id, user)
conn = conn
|> assign(:user, user)
|> get("/api/v1/favourites")
assert [status] = json_response(conn, 200)
assert status["id"] == activity.id
end
end end