aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Braun <roger@rogerbraun.net>2017-09-17 13:09:49 +0200
committerRoger Braun <roger@rogerbraun.net>2017-09-17 13:09:49 +0200
commit422d0f324fa18743129ce81139319ead7cb579ce (patch)
tree92bb87867f9093be0cce9ffe7503d0fd5d8dfb80
parent6d5bd4dcc0b5c323718c70a6815a7f46e314d74b (diff)
downloadpleroma-422d0f324fa18743129ce81139319ead7cb579ce.tar.gz
MastodonAPI: Add user favorites endpoint.
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex7
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex12
-rw-r--r--lib/pleroma/web/router.ex2
-rw-r--r--test/web/mastodon_api/mastodon_api_controller_test.exs17
4 files changed, 38 insertions, 0 deletions
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index c4f7f432c..0cfd22d7b 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -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
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index c231ff7f8..dd1044024 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -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
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 38ad3ebcb..6abf234c6 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -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
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
index 4261ac6b1..56888140d 100644
--- a/test/web/mastodon_api/mastodon_api_controller_test.exs
+++ b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -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