MastoAPI: Add favourited_by/reblogged_by.
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_api_controller.ex
index 3f0c7407f756b1262faf49115bed16863d6d1e9e..0f7674f5dbe2c9ce1ec09629ce5a5cba5ca846ef 100644 (file)
@@ -28,6 +28,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     json(conn, account)
   end
 
+  def user(conn, %{"id" => id}) do
+    with %User{} = user <- Repo.get(User, id) do
+      account = AccountView.render("account.json", %{user: user})
+      json(conn, account)
+    else
+      _e -> conn
+      |> put_status(404)
+      |> json(%{error: "Can't find user"})
+    end
+  end
+
   def masto_instance(conn, _params) do
     response = %{
       uri: Web.base_url,
@@ -158,7 +169,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     result = Enum.map(notifications, fn (%{id: id, activity: activity, inserted_at: created_at}) ->
       actor = User.get_cached_by_ap_id(activity.data["actor"])
       created_at = NaiveDateTime.to_iso8601(created_at)
-      |> String.replace(~r/\.\d+$/, ".000Z")
+      |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
       case activity.data["type"] do
         "Create" ->
           %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity})}
@@ -188,6 +199,37 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     render conn, AccountView, "relationships.json", %{user: user, targets: targets}
   end
 
+  def upload(%{assigns: %{user: user}} = conn, %{"file" => file}) do
+    with {:ok, object} <- ActivityPub.upload(file) do
+      data = object.data
+      |> Map.put("id", object.id)
+
+      render conn, StatusView, "attachment.json", %{attachment: data}
+    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, [])