+ json(conn, dgettext("errors", "error"))
+ end
+
+ defp represent_service_actor(%User{} = user, conn) do
+ with {:ok, user} <- User.ensure_keys_present(user) do
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(UserView.render("user.json", %{user: user}))
+ else
+ nil -> {:error, :not_found}
+ end
+ end
+
+ defp represent_service_actor(nil, _), do: {:error, :not_found}
+
+ def relay(conn, _params) do
+ Relay.get_actor()
+ |> represent_service_actor(conn)
+ end
+
+ def internal_fetch(conn, _params) do
+ InternalFetchActor.get_actor()
+ |> represent_service_actor(conn)
+ end
+
+ def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(UserView.render("user.json", %{user: user}))
+ end
+
+ def whoami(_conn, _params), do: {:error, :not_found}
+
+ def read_inbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do
+ if nickname == user.nickname do
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(UserView.render("inbox.json", %{user: user, max_id: params["max_id"]}))
+ else
+ err =
+ dgettext("errors", "can't read inbox of %{nickname} as %{as_nickname}",
+ nickname: nickname,
+ as_nickname: user.nickname
+ )
+
+ conn
+ |> put_status(:forbidden)
+ |> json(err)
+ end
+ end
+
+ def handle_user_activity(user, %{"type" => "Create"} = params) do
+ object =
+ params["object"]
+ |> Map.merge(Map.take(params, ["to", "cc"]))
+ |> Map.put("attributedTo", user.ap_id())
+ |> Transmogrifier.fix_object()
+
+ ActivityPub.create(%{
+ to: params["to"],
+ actor: user,
+ context: object["context"],
+ object: object,
+ additional: Map.take(params, ["cc"])
+ })
+ end
+
+ def handle_user_activity(user, %{"type" => "Delete"} = params) do
+ with %Object{} = object <- Object.normalize(params["object"]),
+ true <- user.info.is_moderator || user.ap_id == object.data["actor"],
+ {:ok, delete} <- ActivityPub.delete(object) do
+ {:ok, delete}
+ else
+ _ -> {:error, dgettext("errors", "Can't delete object")}
+ end
+ end
+
+ def handle_user_activity(user, %{"type" => "Like"} = params) do
+ with %Object{} = object <- Object.normalize(params["object"]),
+ {:ok, activity, _object} <- ActivityPub.like(user, object) do
+ {:ok, activity}
+ else
+ _ -> {:error, dgettext("errors", "Can't like object")}
+ end
+ end
+
+ def handle_user_activity(_, _) do
+ {:error, dgettext("errors", "Unhandled activity type")}
+ end
+
+ def update_outbox(
+ %{assigns: %{user: user}} = conn,
+ %{"nickname" => nickname} = params
+ ) do
+ if nickname == user.nickname do
+ actor = user.ap_id()
+
+ params =
+ params
+ |> Map.drop(["id"])
+ |> Map.put("actor", actor)
+ |> Transmogrifier.fix_addressing()
+
+ with {:ok, %Activity{} = activity} <- handle_user_activity(user, params) do
+ conn
+ |> put_status(:created)
+ |> put_resp_header("location", activity.data["id"])
+ |> json(activity.data)
+ else
+ {:error, message} ->
+ conn
+ |> put_status(:bad_request)
+ |> json(message)
+ end
+ else
+ err =
+ dgettext("errors", "can't update outbox of %{nickname} as %{as_nickname}",
+ nickname: nickname,
+ as_nickname: user.nickname
+ )
+
+ conn
+ |> put_status(:forbidden)
+ |> json(err)
+ end