MastodonAPI: Add user favorites endpoint.
authorRoger Braun <roger@rogerbraun.net>
Sun, 17 Sep 2017 11:09:49 +0000 (13:09 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 17 Sep 2017 11:09:49 +0000 (13:09 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index c4f7f432c0b0d88cde005510ac4f4017245e3a5c..0cfd22d7b0ca994cd968a48afdb594403b745090 100644 (file)
@@ -147,6 +147,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
   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
     base_query = from activity in Activity,
       limit: 20,
@@ -160,6 +166,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> restrict_max(opts)
     |> restrict_actor(opts)
     |> restrict_type(opts)
+    |> restrict_favorited_by(opts)
     |> Repo.all
     |> Enum.reverse
   end
index c231ff7f829e6b270a3a7677f30d38cd15cc01e9..dd104402400f06a770cde404591a5946f14f7a86 100644 (file)
@@ -312,6 +312,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     json(conn, res)
   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
     Logger.debug("Unimplemented, returning unmodified relationship")
     with %User{} = target <- Repo.get(User, id) do
index 38ad3ebcbe7b072c21177b18691b458eec40a923..6abf234c6640555d588e392bba6e727ed802fee6 100644 (file)
@@ -58,6 +58,8 @@ defmodule Pleroma.Web.Router do
 
     get "/timelines/home", MastodonAPIController, :home_timeline
 
+    get "/favourites", MastodonAPIController, :favourites
+
     post "/statuses", MastodonAPIController, :post_status
     delete "/statuses/:id", MastodonAPIController, :delete_status
 
index 4261ac6b16fa9b4dd124bb7c3adf8d1eaca196c8..56888140de04d61cb44c2424f7b767469f5c4765 100644 (file)
@@ -341,4 +341,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     [account] = results["accounts"]
     assert account["acct"] == "shp@social.heldscal.la"
   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