From b0363e80556b4c8271ab69d2680166ca844f660c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 14 Sep 2017 09:50:49 +0200 Subject: [PATCH] MastoAPI: Add favourited_by/reblogged_by. --- .../mastodon_api/mastodon_api_controller.ex | 22 +++++++++++++++++++ .../web/mastodon_api/views/account_view.ex | 4 ++++ lib/pleroma/web/router.ex | 2 ++ 3 files changed, 28 insertions(+) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index b537bcf71..0f7674f5d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -208,6 +208,28 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def favourited_by(conn, %{"id" => id}) do + with %Activity{data: %{"object" => %{"likes" => likes} = data}} <- Repo.get(Activity, id) do + q = from u in User, + where: u.ap_id in ^likes + users = Repo.all(q) + render conn, AccountView, "accounts.json", %{users: users, as: :user} + else + _ -> json(conn, []) + end + end + + def reblogged_by(conn, %{"id" => id}) do + with %Activity{data: %{"object" => %{"announcements" => announces}}} <- Repo.get(Activity, id) do + q = from u in User, + where: u.ap_id in ^announces + users = Repo.all(q) + render conn, AccountView, "accounts.json", %{users: users, as: :user} + else + _ -> json(conn, []) + end + end + def empty_array(conn, _) do Logger.debug("Unimplemented, returning an empty array") json(conn, []) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 22a7dddf8..f2fa49cb5 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -6,6 +6,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href defp image_url(_), do: nil + def render("accounts.json", %{users: users} = opts) do + render_many(users, AccountView, "account.json", opts) + end + def render("account.json", %{user: user}) do image = User.avatar_url(user) user_info = User.user_info(user) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 93b31aba5..05d1e54b5 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -68,6 +68,8 @@ defmodule Pleroma.Web.Router do get "/statuses/:id", MastodonAPIController, :get_status get "/statuses/:id/context", MastodonAPIController, :get_context + get "/statuses/:id/favourited_by", MastodonAPIController, :favourited_by + get "/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by get "/accounts/:id/statuses", MastodonAPIController, :user_statuses get "/accounts/:id", MastodonAPIController, :user