X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Factivity_pub%2Factivity_pub_controller.ex;h=ff924a5362d6093113cf229bbb2e106bb4e57201;hb=b91a6dd85e143b6f1505a53b13b5c9d9ae6cbf8a;hp=7fd6a45f5dcc2f0c5f229eb52dac3e614643fc70;hpb=89fbed88212657e3dcd4bbcb2c0718b07802037f;p=akkoma
diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 7fd6a45f5..ff924a536 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -1,13 +1,19 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
use Pleroma.Web, :controller
- alias Pleroma.{User, Object}
- alias Pleroma.Web.ActivityPub.{ObjectView, UserView}
+
+ alias Pleroma.Activity
+ alias Pleroma.User
+ alias Pleroma.Object
+ alias Pleroma.Web.ActivityPub.ObjectView
+ alias Pleroma.Web.ActivityPub.UserView
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Web.ActivityPub.Relay
+ alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.Federator
@@ -16,6 +22,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
action_fallback(:errors)
plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay])
+ plug(:set_requester_reachable when action in [:inbox])
plug(:relay_active? when action in [:relay])
def relay_active?(conn, _) do
@@ -43,7 +50,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def object(conn, %{"uuid" => uuid}) do
with ap_id <- o_status_url(conn, :object, uuid),
%Object{} = object <- Object.get_cached_by_ap_id(ap_id),
- {_, true} <- {:public?, ActivityPub.is_public?(object)} do
+ {_, true} <- {:public?, Visibility.is_public?(object)} do
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(ObjectView.render("object.json", %{object: object}))
@@ -53,6 +60,49 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
end
end
+ def object_likes(conn, %{"uuid" => uuid, "page" => page}) do
+ with ap_id <- o_status_url(conn, :object, uuid),
+ %Object{} = object <- Object.get_cached_by_ap_id(ap_id),
+ {_, true} <- {:public?, Visibility.is_public?(object)},
+ likes <- Utils.get_object_likes(object) do
+ {page, _} = Integer.parse(page)
+
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(ObjectView.render("likes.json", ap_id, likes, page))
+ else
+ {:public?, false} ->
+ {:error, :not_found}
+ end
+ end
+
+ def object_likes(conn, %{"uuid" => uuid}) do
+ with ap_id <- o_status_url(conn, :object, uuid),
+ %Object{} = object <- Object.get_cached_by_ap_id(ap_id),
+ {_, true} <- {:public?, Visibility.is_public?(object)},
+ likes <- Utils.get_object_likes(object) do
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(ObjectView.render("likes.json", ap_id, likes))
+ else
+ {:public?, false} ->
+ {:error, :not_found}
+ end
+ end
+
+ def activity(conn, %{"uuid" => uuid}) do
+ with ap_id <- o_status_url(conn, :activity, uuid),
+ %Activity{} = activity <- Activity.normalize(ap_id),
+ {_, true} <- {:public?, Visibility.is_public?(activity)} do
+ conn
+ |> put_resp_header("content-type", "application/activity+json")
+ |> json(ObjectView.render("object.json", %{object: activity}))
+ else
+ {:public?, false} ->
+ {:error, :not_found}
+ end
+ end
+
def following(conn, %{"nickname" => nickname, "page" => page}) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
@@ -93,30 +143,26 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
end
end
- def outbox(conn, %{"nickname" => nickname, "max_id" => max_id}) do
+ def outbox(conn, %{"nickname" => nickname} = params) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
conn
|> put_resp_header("content-type", "application/activity+json")
- |> json(UserView.render("outbox.json", %{user: user, max_id: max_id}))
+ |> json(UserView.render("outbox.json", %{user: user, max_id: params["max_id"]}))
end
end
- def outbox(conn, %{"nickname" => nickname}) do
- outbox(conn, %{"nickname" => nickname, "max_id" => nil})
- end
-
def inbox(%{assigns: %{valid_signature: true}} = conn, %{"nickname" => nickname} = params) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
true <- Utils.recipient_in_message(user.ap_id, params),
params <- Utils.maybe_splice_recipient(user.ap_id, params) do
- Federator.enqueue(:incoming_ap_doc, params)
+ Federator.incoming_ap_doc(params)
json(conn, "ok")
end
end
def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
- Federator.enqueue(:incoming_ap_doc, params)
+ Federator.incoming_ap_doc(params)
json(conn, "ok")
end
@@ -156,6 +202,96 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
end
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
+ conn
+ |> put_status(:forbidden)
+ |> json("can't read inbox of #{nickname} as #{user.nickname}")
+ 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, "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, "Can't like object"}
+ end
+ end
+
+ def handle_user_activity(_, _) do
+ {:error, "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
+ conn
+ |> put_status(:forbidden)
+ |> json("can't update outbox of #{nickname} as #{user.nickname}")
+ end
+ end
+
def errors(conn, {:error, :not_found}) do
conn
|> put_status(404)
@@ -167,4 +303,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|> put_status(500)
|> json("error")
end
+
+ defp set_requester_reachable(%Plug.Conn{} = conn, _) do
+ with actor <- conn.params["actor"],
+ true <- is_binary(actor) do
+ Pleroma.Instances.set_reachable(actor)
+ end
+
+ conn
+ end
end