From f10291a1d322eed22ae594024c9d3d9011a7d5fe Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 17 Jul 2018 03:35:08 +0000 Subject: [PATCH 1/5] upload: use generic Document object type instead of Image (mastodon compatibility) Mastodon does not use the object name as alt text when the object is an Image. --- lib/pleroma/upload.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 43df0d418..92a89e296 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -19,7 +19,7 @@ defmodule Pleroma.Upload do end %{ - "type" => "Image", + "type" => "Document", "url" => [ %{ "type" => "Link", From cd19d37a90cfceb8bafb380fd8ff8e5a765b7e3d Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 17 Jul 2018 03:36:11 +0000 Subject: [PATCH 2/5] mastodon api: use object name as alt text --- lib/pleroma/web/mastodon_api/views/status_view.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 4c20581d6..5dbd59dd9 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -169,7 +169,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do remote_url: href, preview_url: MediaProxy.url(href), text_url: href, - type: type + type: type, + description: attachment["name"] } end From 99c0252314fc3391e6fae58bdad8110d0a053fb9 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 17 Jul 2018 03:36:50 +0000 Subject: [PATCH 3/5] mastodon api: support descriptions in media api, add PUT endpoint for updating metadata about a media upload --- .../web/mastodon_api/mastodon_api_controller.ex | 35 +++++++++++++++++++--- lib/pleroma/web/router.ex | 1 + 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 09e6b0b59..f270e1146 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,6 +1,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller - alias Pleroma.{Repo, Activity, User, Notification, Stats} + alias Pleroma.{Repo, Object, Activity, User, Notification, Stats} alias Pleroma.Web alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView} alias Pleroma.Web.ActivityPub.ActivityPub @@ -428,16 +428,43 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do render(conn, AccountView, "relationships.json", %{user: user, targets: targets}) end - def upload(%{assigns: %{user: _}} = conn, %{"file" => file}) do - with {:ok, object} <- ActivityPub.upload(file) do + def update_media(%{assigns: %{user: _}} = conn, data) do + with %Object{} = object <- Repo.get(Object, data["id"]), + true <- is_binary(data["description"]), + description <- data["description"] do + new_data = %{object.data | "name" => description} + + change = Object.change(object, %{data: new_data}) + {:ok, media_obj} = Repo.update(change) + data = - object.data + new_data |> Map.put("id", object.id) render(conn, StatusView, "attachment.json", %{attachment: data}) end end + def upload(%{assigns: %{user: _}} = conn, %{"file" => file} = data) do + with {:ok, object} <- ActivityPub.upload(file) do + objdata = + if Map.has_key?(data, "description") do + Map.put(object.data, "name", data["description"]) + else + object.data + end + + change = Object.change(object, %{data: objdata}) + {:ok, object} = Repo.update(change) + + objdata = + objdata + |> Map.put("id", object.id) + + render(conn, StatusView, "attachment.json", %{attachment: objdata}) + end + end + def favourited_by(conn, %{"id" => id}) do with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do q = from(u in User, where: u.ap_id in ^likes) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 34652cdde..fc7a947aa 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -127,6 +127,7 @@ defmodule Pleroma.Web.Router do get("/notifications/:id", MastodonAPIController, :get_notification) post("/media", MastodonAPIController, :upload) + put("/media/:id", MastodonAPIController, :update_media) get("/lists", MastodonAPIController, :get_lists) get("/lists/:id", MastodonAPIController, :get_list) From 489453c2467b12970258927015209c9895d5cf6e Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 17 Jul 2018 03:37:26 +0000 Subject: [PATCH 4/5] tests: verify media description api support is working --- test/web/mastodon_api/mastodon_api_controller_test.exs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index d1812457d..9e33c1d04 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -736,16 +736,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do filename: "an_image.jpg" } + desc = "Description of the image" + user = insert(:user) conn = conn |> assign(:user, user) - |> post("/api/v1/media", %{"file" => file}) + |> post("/api/v1/media", %{"file" => file, "description" => desc}) assert media = json_response(conn, 200) assert media["type"] == "image" + assert media["description"] == desc end test "hashtag timeline", %{conn: conn} do From 18cac1e36bd91e1554de1f495a7f178b27b2fbc3 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 17 Jul 2018 03:37:40 +0000 Subject: [PATCH 5/5] test: mastodon attachments: update for added description field --- test/web/mastodon_api/status_view_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index d28c3cbad..03c798bef 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -102,7 +102,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do url: "someurl", remote_url: "someurl", preview_url: "someurl", - text_url: "someurl" + text_url: "someurl", + description: nil } assert expected == StatusView.render("attachment.json", %{attachment: object})