From a35299dd96c817d8c09e3eb9ce22afffc11ebfbc Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 18 Jan 2021 12:46:43 +0300 Subject: [PATCH] Dev patch applied --- lib/pleroma/media.ex | 6 +- lib/pleroma/object.ex | 31 +++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 17 +--- .../web/activity_pub/activity_pub_controller.ex | 9 +- lib/pleroma/web/activity_pub/builder.ex | 4 + lib/pleroma/web/activity_pub/transmogrifier.ex | 13 --- lib/pleroma/web/common_api.ex | 3 +- .../mastodon_api/controllers/media_controller.ex | 23 +---- .../pleroma_api/controllers/mascot_controller.ex | 9 +- lib/pleroma/workers/attachments_cleanup_worker.ex | 97 +++++++--------------- test/pleroma/object_test.exs | 43 +++++----- .../pleroma/web/activity_pub/activity_pub_test.exs | 38 +++++---- .../attachment_validator_test.exs | 4 +- .../object_validators/chat_validation_test.exs | 14 ++-- test/pleroma/web/common_api_test.exs | 2 +- .../controllers/account_controller_test.exs | 4 +- .../controllers/media_controller_test.exs | 51 ++++++------ .../controllers/status_controller_test.exs | 4 +- .../views/scheduled_activity_view_test.exs | 8 +- .../controllers/chat_controller_test.exs | 2 +- .../views/chat_message_reference_view_test.exs | 2 +- test/pleroma/web/push/impl_test.exs | 2 +- 22 files changed, 177 insertions(+), 209 deletions(-) diff --git a/lib/pleroma/media.ex b/lib/pleroma/media.ex index 431c06bb5..b9b001366 100644 --- a/lib/pleroma/media.ex +++ b/lib/pleroma/media.ex @@ -20,6 +20,8 @@ defmodule Pleroma.Media do field(:blurhash, :string) field(:meta, :map) + field(:removable, :boolean, virtual: true, default: false) + belongs_to(:object, Pleroma.Object) belongs_to(:user, Pleroma.User, type: FlakeId.Ecto.CompatType) @@ -27,7 +29,7 @@ defmodule Pleroma.Media do end def create_from_object_data(%{"url" => [url]} = data, %{user: user} = opts) do - object_id = get_in(opts, [:object, "id"]) + object_id = get_in(opts, [:object, "id"]) || Map.get(opts, :object_id) %Media{} |> changeset(%{ @@ -48,7 +50,7 @@ defmodule Pleroma.Media do @spec authorize_access(Media.t(), User.t()) :: :ok | {:error, :forbidden} def authorize_access(%Media{user_id: user_id}, %User{id: user_id}), do: :ok - def authorize_access(%Media{user_id: user_id}, %User{id: user_id}), do: {:error, :forbidden} + def authorize_access(_media, _user), do: {:error, :forbidden} def update(%Media{} = media, attrs \\ %{}) do media diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index aaf123840..a794b13c2 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Object do alias Pleroma.Activity alias Pleroma.Config + alias Pleroma.Media alias Pleroma.Object alias Pleroma.Object.Fetcher alias Pleroma.ObjectTombstone @@ -51,6 +52,7 @@ defmodule Pleroma.Object do def create(data) do Object.change(%Object{}, %{data: data}) |> Repo.insert() + |> maybe_handle_attachments() end def change(struct, params \\ %{}) do @@ -349,4 +351,33 @@ defmodule Pleroma.Object do def self_replies(object, opts \\ []), do: replies(object, Keyword.put(opts, :self_only, true)) + + defp maybe_handle_attachments( + {:ok, + %Object{id: object_id, data: %{"attachment" => [_ | _] = attachments} = data} = object} = + result + ) do + Enum.each(attachments, fn attachment -> + case attachment["id"] do + # New media incoming + nil -> + Media.create_from_object_data(attachment, %{ + user: User.get_by_ap_id(data["actor"]), + object_id: object_id + }) + + # Media pre-uploaded for a post + media_id -> + media_id + |> Media.get_by_id() + |> Media.update(%{object_id: object_id}) + end + + object + end) + + result + end + + defp maybe_handle_attachments(result), do: result end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index c2948fd52..4322fc729 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -123,7 +123,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do {:fake, false, map, recipients} <- {:fake, fake, map, recipients}, {:containment, :ok} <- {:containment, Containment.contain_child(map)}, {:ok, map, object} <- insert_full_object(map), - :ok <- maybe_update_media(object), {:ok, activity} <- insert_activity_with_expiration(map, local, recipients) do # Splice in the child object if we have one. activity = Maps.put_if_present(activity, :object, object) @@ -166,18 +165,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - defp maybe_update_media(%Object{data: %{"attachment" => []}}), do: :ok - - defp maybe_update_media(%Object{data: %{"id" => id, "attachment" => attachments}}) do - Enum.each(attachments, fn data -> - with %{"id" => media_id} <- data do - media_id - |> Pleroma.Media.get_by_id() - |> Pleroma.Media.update(%{object_id: id}) - end - end) - end - defp insert_activity_with_expiration(data, local, recipients) do struct = %Activity{ data: data, @@ -1214,8 +1201,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do @spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()} def upload(file, opts \\ []) do with {:ok, data} <- Upload.store(file, opts), - %User{} <- opts[:user] do - Pleroma.Media.create_from_object_data(data, %{user: opts[:user]}) + %User{} = user <- opts[:user] do + Pleroma.Media.create_from_object_data(data, %{user: user}) end end diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index 2e5069cb5..ecb632b75 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do alias Pleroma.Activity alias Pleroma.Delivery + alias Pleroma.Media alias Pleroma.Object alias Pleroma.Object.Fetcher alias Pleroma.User @@ -537,17 +538,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do end def upload_media(%{assigns: %{user: %User{} = user}} = conn, %{"file" => file} = data) do - with {:ok, object} <- + with {:ok, media} <- ActivityPub.upload( file, - actor: User.ap_id(user), + user: user, description: Map.get(data, "description") ) do - Logger.debug(inspect(object)) + Logger.debug(inspect(media)) conn |> put_status(:created) - |> json(object.data) + |> json(Media.to_object_form(media)) end end end diff --git a/lib/pleroma/web/activity_pub/builder.ex b/lib/pleroma/web/activity_pub/builder.ex index f56bfc600..abd67ebc2 100644 --- a/lib/pleroma/web/activity_pub/builder.ex +++ b/lib/pleroma/web/activity_pub/builder.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do """ alias Pleroma.Emoji + alias Pleroma.Media alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.Relay @@ -137,6 +138,9 @@ defmodule Pleroma.Web.ActivityPub.Builder do } case opts[:attachment] do + %Media{} = media -> + {:ok, Map.put(basic, "attachment", Media.to_object_form(media)), []} + %Object{data: attachment_data} -> { :ok, diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 7f7387c31..af9143324 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -9,7 +9,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do alias Pleroma.Activity alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.Maps - alias Pleroma.Media alias Pleroma.Object alias Pleroma.Object.Containment alias Pleroma.Repo @@ -37,7 +36,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> fix_actor() |> fix_url() |> fix_attachments() - |> fix_media() |> fix_context() |> fix_in_reply_to(options) |> fix_emoji() @@ -272,17 +270,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do def fix_attachments(object), do: object - def fix_media(%{"attachment" => [_ | _] = attachments} = object) do - Enum.each( - attachments, - &Media.create_from_object_data(&1, %{user: User.get_by_ap_id(object.actor), object: object}) - ) - - object - end - - def fix_media(object), do: object - def fix_url(%{"url" => url} = object) when is_map(url) do Map.put(object, "url", url["href"]) end diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index b003e30c7..0e17c7a36 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.CommonAPI do alias Pleroma.Activity alias Pleroma.Conversation.Participation alias Pleroma.Formatter + alias Pleroma.Media alias Pleroma.Object alias Pleroma.ThreadMute alias Pleroma.User @@ -31,7 +32,7 @@ defmodule Pleroma.Web.CommonAPI do end def post_chat_message(%User{} = user, %User{} = recipient, content, opts \\ []) do - with maybe_attachment <- opts[:media_id] && Object.get_by_id(opts[:media_id]), + with maybe_attachment <- opts[:media_id] && Media.get_by_id(opts[:media_id]), :ok <- validate_chat_content_length(content, !!maybe_attachment), {_, {:ok, chat_message_data, _meta}} <- {:build_object, diff --git a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex index 1bd521460..d9e5241da 100644 --- a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex @@ -36,34 +36,17 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do def create(_conn, _data), do: {:error, :bad_request} - def _create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do - with {:ok, object} <- - ActivityPub.upload( - file, - actor: User.ap_id(user), - description: Map.get(data, :description) - ) do - attachment_data = Map.put(object.data, "id", object.id) - - render(conn, "attachment.json", %{attachment: attachment_data}) - end - end - - def _create(_conn, _data), do: {:error, :bad_request} - @doc "POST /api/v2/media" def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do - with {:ok, object} <- + with {:ok, media} <- ActivityPub.upload( file, - actor: User.ap_id(user), + user: user, description: Map.get(data, :description) ) do - attachment_data = Map.put(object.data, "id", object.id) - conn |> put_status(202) - |> render("attachment.json", %{attachment: attachment_data}) + |> render("media.json", %{media: media}) end end diff --git a/lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex b/lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex index 429ef5112..4f2aa83e1 100644 --- a/lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/mascot_controller.ex @@ -24,8 +24,8 @@ defmodule Pleroma.Web.PleromaAPI.MascotController do @doc "PUT /api/v1/pleroma/mascot" def update(%{assigns: %{user: user}, body_params: %{file: file}} = conn, _) do with {:content_type, "image" <> _} <- {:content_type, file.content_type}, - {:ok, object} <- ActivityPub.upload(file, actor: User.ap_id(user)) do - attachment = render_attachment(object) + {:ok, media} <- ActivityPub.upload(file, user: user) do + attachment = Pleroma.Web.MastodonAPI.StatusView.render("media.json", %{media: media}) {:ok, _user} = User.mascot_update(user, attachment) json(conn, attachment) @@ -34,9 +34,4 @@ defmodule Pleroma.Web.PleromaAPI.MascotController do render_error(conn, :unsupported_media_type, "mascots can only be images") end end - - defp render_attachment(object) do - attachment_data = Map.put(object.data, "id", object.id) - Pleroma.Web.MastodonAPI.StatusView.render("attachment.json", %{attachment: attachment_data}) - end end diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index a2373ebb9..bd3e3c03b 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -5,7 +5,7 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do import Ecto.Query - alias Pleroma.Object + alias Pleroma.Media alias Pleroma.Repo use Pleroma.Workers.WorkerHelper, queue: "attachments_cleanup" @@ -14,22 +14,21 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do def perform(%Job{ args: %{ "op" => "cleanup_attachments", - "object" => %{"data" => %{"attachment" => [_ | _] = attachments, "actor" => actor}} + "object" => %{"data" => %{"attachment" => [_ | _] = attachments}} } }) do attachments - |> Enum.flat_map(fn item -> Enum.map(item["url"], & &1["href"]) end) - |> fetch_objects - |> prepare_objects(actor, Enum.map(attachments, & &1["name"])) - |> filter_objects - |> do_clean + |> Enum.map(& &1["id"]) + |> get_media() + |> set_removable() + |> do_clean() {:ok, :success} end def perform(%Job{args: %{"op" => "cleanup_attachments", "object" => _object}}), do: {:ok, :skip} - defp do_clean({object_ids, attachment_urls}) do + defp do_clean(medias) do uploader = Pleroma.Config.get([Pleroma.Upload, :uploader]) base_url = @@ -38,70 +37,38 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do "/" ) - Enum.each(attachment_urls, fn href -> - href - |> String.trim_leading("#{base_url}") - |> uploader.delete_file() + Enum.each(medias, fn media -> + with true <- media.removable do + media.href + |> String.trim_leading("#{base_url}") + |> uploader.delete_file() + end + + Repo.delete(media) end) - - delete_objects(object_ids) end - defp delete_objects([_ | _] = object_ids) do - Repo.delete_all(from(o in Object, where: o.id in ^object_ids)) + defp get_media(ids) do + from(m in Media, + where: m.id in ^ids + ) + |> Repo.all() end - defp delete_objects(_), do: :ok + defp set_removable(medias) do + Enum.map(medias, fn media -> + from(m in Media, + where: m.href == ^media.href, + select: count(m.id) + ) + |> Repo.one!() + |> case do + 1 -> + %Media{media | removable: true} - # we should delete 1 object for any given attachment, but don't delete - # files if there are more than 1 object for it - defp filter_objects(objects) do - Enum.reduce(objects, {[], []}, fn {href, %{id: id, count: count}}, {ids, hrefs} -> - with 1 <- count do - {ids ++ [id], hrefs ++ [href]} - else - _ -> {ids ++ [id], hrefs} + _ -> + %Media{media | removable: false} end end) end - - defp prepare_objects(objects, actor, names) do - objects - |> Enum.reduce(%{}, fn %{ - id: id, - data: %{ - "url" => [%{"href" => href}], - "actor" => obj_actor, - "name" => name - } - }, - acc -> - Map.update(acc, href, %{id: id, count: 1}, fn val -> - case obj_actor == actor and name in names do - true -> - # set id of the actor's object that will be deleted - %{val | id: id, count: val.count + 1} - - false -> - # another actor's object, just increase count to not delete file - %{val | count: val.count + 1} - end - end) - end) - end - - defp fetch_objects(hrefs) do - from(o in Object, - where: - fragment( - "to_jsonb(array(select jsonb_array_elements((?)#>'{url}') ->> 'href' where jsonb_typeof((?)#>'{url}') = 'array'))::jsonb \\?| (?)", - o.data, - o.data, - ^hrefs - ) - ) - # The query above can be time consumptive on large instances until we - # refactor how uploads are stored - |> Repo.all(timeout: :infinity) - end end diff --git a/test/pleroma/object_test.exs b/test/pleroma/object_test.exs index 4a8d80fcc..7119530d8 100644 --- a/test/pleroma/object_test.exs +++ b/test/pleroma/object_test.exs @@ -5,10 +5,13 @@ defmodule Pleroma.ObjectTest do use Pleroma.DataCase use Oban.Testing, repo: Pleroma.Repo + import ExUnit.CaptureLog import Pleroma.Factory import Tesla.Mock + alias Pleroma.Activity + alias Pleroma.Media alias Pleroma.Object alias Pleroma.Repo alias Pleroma.Tests.ObanHelpers @@ -89,11 +92,11 @@ defmodule Pleroma.ObjectTest do user = insert(:user) - {:ok, %Object{} = attachment} = - Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + {:ok, %Media{} = media} = Pleroma.Web.ActivityPub.ActivityPub.upload(file, user: user) %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = - note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + note = + insert(:note, %{user: user, data: %{"attachment" => [Media.to_object_form(media)]}}) uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) @@ -106,7 +109,7 @@ defmodule Pleroma.ObjectTest do ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) assert Object.get_by_id(note.id).data["deleted"] - refute Object.get_by_id(attachment.id) == nil + refute Media.get_by_id(media.id) == nil assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}") end @@ -123,11 +126,11 @@ defmodule Pleroma.ObjectTest do user = insert(:user) - {:ok, %Object{} = attachment} = - Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + {:ok, %Media{} = media} = Pleroma.Web.ActivityPub.ActivityPub.upload(file, user: user) %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = - note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + note = + insert(:note, %{user: user, data: %{"attachment" => [Media.to_object_form(media)]}}) uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) @@ -140,7 +143,7 @@ defmodule Pleroma.ObjectTest do ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) assert Object.get_by_id(note.id).data["deleted"] - assert Object.get_by_id(attachment.id) == nil + assert Media.get_by_id(media.id) == nil assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") end @@ -162,11 +165,11 @@ defmodule Pleroma.ObjectTest do user = insert(:user) - {:ok, %Object{} = attachment} = - Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + {:ok, %Media{} = media} = Pleroma.Web.ActivityPub.ActivityPub.upload(file, user: user) %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = - note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + note = + insert(:note, %{user: user, data: %{"attachment" => [Media.to_object_form(media)]}}) filename = Path.basename(href) @@ -178,7 +181,7 @@ defmodule Pleroma.ObjectTest do ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) assert Object.get_by_id(note.id).data["deleted"] - assert Object.get_by_id(attachment.id) == nil + assert Media.get_by_id(media.id) == nil assert {:ok, files} = File.ls(uploads_dir) refute filename in files end @@ -195,13 +198,13 @@ defmodule Pleroma.ObjectTest do user = insert(:user) - {:ok, %Object{} = attachment} = - Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + {:ok, %Media{} = media} = Pleroma.Web.ActivityPub.ActivityPub.upload(file, user: user) {:ok, %Object{}} = Object.create(%{url: "https://google.com", actor: user.ap_id}) %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = - note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + note = + insert(:note, %{user: user, data: %{"attachment" => [Media.to_object_form(media)]}}) uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) @@ -214,7 +217,7 @@ defmodule Pleroma.ObjectTest do ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) assert Object.get_by_id(note.id).data["deleted"] - assert Object.get_by_id(attachment.id) == nil + assert Media.get_by_id(media.id) == nil assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") end @@ -232,11 +235,11 @@ defmodule Pleroma.ObjectTest do user = insert(:user) - {:ok, %Object{} = attachment} = - Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + {:ok, %Media{} = media} = Pleroma.Web.ActivityPub.ActivityPub.upload(file, user: user) %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = - note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + note = + insert(:note, %{user: user, data: %{"attachment" => [Media.to_object_form(media)]}}) uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) @@ -249,7 +252,7 @@ defmodule Pleroma.ObjectTest do ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) assert Object.get_by_id(note.id).data["deleted"] - assert Object.get_by_id(attachment.id) == nil + assert Media.get_by_id(media.id) == nil assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") end diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index 24576b31a..713a012b4 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -9,6 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do alias Pleroma.Activity alias Pleroma.Builders.ActivityBuilder alias Pleroma.Config + alias Pleroma.Media alias Pleroma.Notification alias Pleroma.Object alias Pleroma.User @@ -1068,42 +1069,47 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do filename: "an_image.jpg" } - %{test_file: test_file} + user = insert(:user) + + %{test_file: test_file, user: user} end - test "sets a description if given", %{test_file: file} do - {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file") - assert object.data["name"] == "a cool file" + test "sets a description if given", %{test_file: file, user: user} do + {:ok, %Media{} = media} = ActivityPub.upload(file, description: "a cool file", user: user) + assert media.name == "a cool file" end - test "it sets the default description depending on the configuration", %{test_file: file} do + test "it sets the default description depending on the configuration", %{ + test_file: file, + user: user + } do clear_config([Pleroma.Upload, :default_description]) Pleroma.Config.put([Pleroma.Upload, :default_description], nil) - {:ok, %Object{} = object} = ActivityPub.upload(file) - assert object.data["name"] == "" + {:ok, %Media{} = media} = ActivityPub.upload(file, user: user) + assert media.name == nil Pleroma.Config.put([Pleroma.Upload, :default_description], :filename) - {:ok, %Object{} = object} = ActivityPub.upload(file) - assert object.data["name"] == "an_image.jpg" + {:ok, %Media{} = media} = ActivityPub.upload(file, user: user) + assert media.name == "an_image.jpg" Pleroma.Config.put([Pleroma.Upload, :default_description], "unnamed attachment") - {:ok, %Object{} = object} = ActivityPub.upload(file) - assert object.data["name"] == "unnamed attachment" + {:ok, %Media{} = media} = ActivityPub.upload(file, user: user) + assert media.name == "unnamed attachment" end - test "copies the file to the configured folder", %{test_file: file} do + test "copies the file to the configured folder", %{test_file: file, user: user} do clear_config([Pleroma.Upload, :default_description], :filename) - {:ok, %Object{} = object} = ActivityPub.upload(file) - assert object.data["name"] == "an_image.jpg" + {:ok, %Media{} = media} = ActivityPub.upload(file, user: user) + assert media.name == "an_image.jpg" end - test "works with base64 encoded images" do + test "works with base64 encoded images", %{user: user} do file = %{ img: data_uri() } - {:ok, %Object{}} = ActivityPub.upload(file) + {:ok, %Media{}} = ActivityPub.upload(file, user: user) end end diff --git a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs index b775515e0..940ab8ab3 100644 --- a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs @@ -63,10 +63,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do filename: "an_image.jpg" } - {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, attachment} = ActivityPub.upload(file, user: user) {:ok, attachment} = - attachment.data + attachment |> AttachmentValidator.cast_and_validate() |> Ecto.Changeset.apply_action(:insert) diff --git a/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs b/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs index 782f6c652..4b12eabc6 100644 --- a/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs @@ -4,6 +4,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatValidationTest do use Pleroma.DataCase + + alias Pleroma.Media alias Pleroma.Object alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Builder @@ -82,11 +84,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatValidationTest do filename: "an_image.jpg" } - {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, media} = ActivityPub.upload(file, user: user) valid_chat_message = valid_chat_message - |> Map.put("attachment", attachment.data) + |> Map.put("attachment", Media.to_object_form(media)) assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, []) @@ -103,11 +105,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatValidationTest do filename: "an_image.jpg" } - {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, media} = ActivityPub.upload(file, user: user) valid_chat_message = valid_chat_message - |> Map.put("attachment", [attachment.data]) + |> Map.put("attachment", [Media.to_object_form(media)]) assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, []) @@ -124,11 +126,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatValidationTest do filename: "an_image.jpg" } - {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, media} = ActivityPub.upload(file, user: user) valid_chat_message = valid_chat_message - |> Map.put("attachment", attachment.data) + |> Map.put("attachment", Media.to_object_form(media)) |> Map.delete("content") assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, []) diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index 2ece92806..b705dc7ad 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -119,7 +119,7 @@ defmodule Pleroma.Web.CommonAPITest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: author) with_mocks([ { diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 7b3cc7344..8227146c9 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -385,11 +385,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do filename: "an_image.jpg" } - {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, %{id: media_id}} = ActivityPub.upload(file, user: user) {:ok, %{id: image_post_id}} = CommonAPI.post(user, %{status: "cofe", media_ids: [media_id]}) - {:ok, %{id: media_id}} = ActivityPub.upload(file, actor: other_user.ap_id) + {:ok, %{id: media_id}} = ActivityPub.upload(file, user: other_user) {:ok, %{id: other_image_post_id}} = CommonAPI.post(other_user, %{status: "cofe2", media_ids: [media_id]}) diff --git a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs index 6c8f984d5..c7470d3df 100644 --- a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs @@ -5,8 +5,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do use Pleroma.Web.ConnCase - alias Pleroma.Object - alias Pleroma.User + alias Pleroma.Media alias Pleroma.Web.ActivityPub.ActivityPub describe "Upload media" do @@ -38,8 +37,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do assert media["description"] == desc assert media["id"] - object = Object.get_by_id(media["id"]) - assert object.data["actor"] == User.ap_id(conn.assigns[:user]) + media = Media.get_by_id(media["id"]) + assert media.user_id == conn.assigns[:user].id end test "/api/v2/media", %{conn: conn, user: user, image: image} do @@ -64,8 +63,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do assert media["description"] == desc assert media["id"] - object = Object.get_by_id(media["id"]) - assert object.data["actor"] == user.ap_id + media = Media.get_by_id(media["id"]) + assert media.user_id == user.id end end @@ -79,25 +78,25 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do filename: "an_image.jpg" } - {:ok, %Object{} = object} = + {:ok, %Media{} = media} = ActivityPub.upload( file, - actor: User.ap_id(actor), + user: actor, description: "test-m" ) - [object: object] + [media: media] end - test "/api/v1/media/:id good request", %{conn: conn, object: object} do - media = + test "/api/v1/media/:id good request", %{conn: conn, media: media} do + media2 = conn |> put_req_header("content-type", "multipart/form-data") - |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"}) + |> put("/api/v1/media/#{media.id}", %{"description" => "test-media"}) |> json_response_and_validate_schema(:ok) - assert media["description"] == "test-media" - assert refresh_record(object).data["name"] == "test-media" + assert media2["description"] == "test-media" + assert refresh_record(media).name == "test-media" end end @@ -111,35 +110,35 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do filename: "an_image.jpg" } - {:ok, %Object{} = object} = + {:ok, %Media{} = media} = ActivityPub.upload( file, - actor: User.ap_id(actor), + user: actor, description: "test-media" ) - [object: object] + [media: media] end - test "it returns media object when requested by owner", %{conn: conn, object: object} do - media = + test "it returns media object when requested by owner", %{conn: conn, media: media} do + media2 = conn - |> get("/api/v1/media/#{object.id}") + |> get("/api/v1/media/#{media.id}") |> json_response_and_validate_schema(:ok) - assert media["description"] == "test-media" - assert media["type"] == "image" - assert media["id"] + assert media2["description"] == "test-media" + assert media2["type"] == "image" + assert media2["id"] end - test "it returns 403 if media object requested by non-owner", %{object: object, user: user} do + test "it returns 403 if media object requested by non-owner", %{media: media, user: user} do %{conn: conn, user: other_user} = oauth_access(["read:media"]) - assert object.data["actor"] == user.ap_id + assert media.user_id == user.id refute user.id == other_user.id conn - |> get("/api/v1/media/#{object.id}") + |> get("/api/v1/media/#{media.id}") |> json_response(403) end end diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index 8a2267099..2c018cb98 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -168,7 +168,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: user) conn = conn @@ -409,7 +409,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: user) conn = conn diff --git a/test/pleroma/web/mastodon_api/views/scheduled_activity_view_test.exs b/test/pleroma/web/mastodon_api/views/scheduled_activity_view_test.exs index c3b7f0f41..9794dcc3d 100644 --- a/test/pleroma/web/mastodon_api/views/scheduled_activity_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/scheduled_activity_view_test.exs @@ -27,11 +27,11 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityViewTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, media} = ActivityPub.upload(file, user: user) attrs = %{ params: %{ - "media_ids" => [upload.id], + "media_ids" => [media.id], "status" => "hi", "sensitive" => true, "spoiler_text" => "spoiler", @@ -47,12 +47,12 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityViewTest do expected = %{ id: to_string(scheduled_activity.id), media_attachments: - %{media_ids: [upload.id]} + %{media_ids: [media.id]} |> Utils.attachments_from_ids() |> Enum.map(&StatusView.render("attachment.json", %{attachment: &1})), params: %{ in_reply_to_id: to_string(activity.id), - media_ids: [upload.id], + media_ids: [media.id], poll: nil, scheduled_at: nil, sensitive: true, diff --git a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs index 372613b8b..bb4b906ea 100644 --- a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs @@ -112,7 +112,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: user) other_user = insert(:user) diff --git a/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs b/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs index 6deaa2102..6098d6e11 100644 --- a/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs +++ b/test/pleroma/web/pleroma_api/views/chat_message_reference_view_test.exs @@ -24,7 +24,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceViewTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: user) {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "kippis :firefox:", idempotency_key: "123") diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs index b3ca1a337..ee4b38d4e 100644 --- a/test/pleroma/web/push/impl_test.exs +++ b/test/pleroma/web/push/impl_test.exs @@ -242,7 +242,7 @@ defmodule Pleroma.Web.Push.ImplTest do filename: "an_image.jpg" } - {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id) + {:ok, upload} = ActivityPub.upload(file, user: user) {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id) object = Object.normalize(chat, fetch: false)