aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaniini <ariadne@dereferenced.org>2019-09-30 11:21:49 +0000
committerkaniini <ariadne@dereferenced.org>2019-09-30 11:21:49 +0000
commit26f66fb70ab1bd777a92daf4347aaa63b044d0af (patch)
tree75d562e8e5d9f6a036d626e0cb9d081998242263
parent0e356cc8003665ec05a42e39b3a5c47e31ec384c (diff)
parent0dc8f3d6d2fa18261e9a6fa8da540c434f1fa67b (diff)
downloadpleroma-26f66fb70ab1bd777a92daf4347aaa63b044d0af.tar.gz
Merge branch 'features/apc2s-media-upload' into 'develop'
AP C2S mediaUpload Closes #1171 See merge request pleroma/pleroma!1706
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub_controller.ex28
-rw-r--r--lib/pleroma/web/activity_pub/views/user_view.ex3
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--priv/static/schemas/litepub-0.1.jsonld4
-rw-r--r--test/web/activity_pub/activity_pub_controller_test.exs40
-rw-r--r--test/web/ostatus/ostatus_controller_test.exs6
6 files changed, 79 insertions, 3 deletions
diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 8112f6642..7cd13b4b8 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -334,6 +334,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|> represent_service_actor(conn)
end
+ @doc "Returns the authenticated user's ActivityPub User object or a 404 Not Found if non-authenticated"
def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
conn
|> put_resp_content_type("application/activity+json")
@@ -509,4 +510,31 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{new_user, for_user}
end
+
+ # TODO: Add support for "object" field
+ @doc """
+ Endpoint based on <https://www.w3.org/wiki/SocialCG/ActivityPub/MediaUpload>
+
+ Parameters:
+ - (required) `file`: data of the media
+ - (optionnal) `description`: description of the media, intended for accessibility
+
+ Response:
+ - HTTP Code: 201 Created
+ - HTTP Body: ActivityPub object to be inserted into another's `attachment` field
+ """
+ def upload_media(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
+ with {:ok, object} <-
+ ActivityPub.upload(
+ file,
+ actor: User.ap_id(user),
+ description: Map.get(data, "description")
+ ) do
+ Logger.debug(inspect(object))
+
+ conn
+ |> put_status(:created)
+ |> json(object.data)
+ end
+ end
end
diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex
index 993307287..c94c5a225 100644
--- a/lib/pleroma/web/activity_pub/views/user_view.ex
+++ b/lib/pleroma/web/activity_pub/views/user_view.ex
@@ -24,7 +24,8 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
"oauthRegistrationEndpoint" => Helpers.mastodon_api_url(Endpoint, :create_app),
"oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
- "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
+ "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
+ "uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
}
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index fa6f1ec76..9fd13c2fd 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -608,6 +608,7 @@ defmodule Pleroma.Web.Router do
scope [] do
pipe_through(:oauth_write)
post("/users/:nickname/outbox", ActivityPubController, :update_outbox)
+ post("/api/ap/upload_media", ActivityPubController, :upload_media)
end
scope [] do
diff --git a/priv/static/schemas/litepub-0.1.jsonld b/priv/static/schemas/litepub-0.1.jsonld
index 6e4bb29b1..f01c2c33a 100644
--- a/priv/static/schemas/litepub-0.1.jsonld
+++ b/priv/static/schemas/litepub-0.1.jsonld
@@ -28,6 +28,10 @@
"oauthRegistrationEndpoint": {
"@id": "litepub:oauthRegistrationEndpoint",
"@type": "@id"
+ },
+ "uploadMedia": {
+ "@id": "litepub:uploadMedia",
+ "@type": "@id"
}
}
]
diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs
index ab52044ae..1ffa91b70 100644
--- a/test/web/activity_pub/activity_pub_controller_test.exs
+++ b/test/web/activity_pub/activity_pub_controller_test.exs
@@ -976,4 +976,44 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
assert Delivery.get(object.id, other_user.id)
end
end
+
+ describe "Additionnal ActivityPub C2S endpoints" do
+ test "/api/ap/whoami", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> get("/api/ap/whoami")
+
+ user = User.get_cached_by_id(user.id)
+
+ assert UserView.render("user.json", %{user: user}) == json_response(conn, 200)
+ end
+
+ clear_config([:media_proxy])
+ clear_config([Pleroma.Upload])
+
+ test "uploadMedia", %{conn: conn} do
+ user = insert(:user)
+
+ desc = "Description of the image"
+
+ image = %Plug.Upload{
+ content_type: "image/jpg",
+ path: Path.absname("test/fixtures/image.jpg"),
+ filename: "an_image.jpg"
+ }
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/ap/upload_media", %{"file" => image, "description" => desc})
+
+ assert object = json_response(conn, :created)
+ assert object["name"] == desc
+ assert object["type"] == "Document"
+ assert object["actor"] == user.ap_id
+ end
+ end
end
diff --git a/test/web/ostatus/ostatus_controller_test.exs b/test/web/ostatus/ostatus_controller_test.exs
index 2b40fb47e..f06023dff 100644
--- a/test/web/ostatus/ostatus_controller_test.exs
+++ b/test/web/ostatus/ostatus_controller_test.exs
@@ -396,7 +396,8 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
"oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
"oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
"oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
- "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
+ "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox",
+ "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media"
}
assert response["@context"] == [
@@ -458,7 +459,8 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
"oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
"oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
"oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
- "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox"
+ "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox",
+ "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media"
}
assert response["@context"] == [