From 5afdca7ddf7a4a1a1c28764611c158d144cc0f5e Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Thu, 21 Jan 2021 16:15:53 +0300 Subject: Replaced Media.user_id with Media.actor --- lib/pleroma/media.ex | 23 ++++--- lib/pleroma/object.ex | 58 ++++++++++------ lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../web/activity_pub/activity_pub_controller.ex | 11 +-- .../transmogrifier/audio_handling_test.exs | 35 +++++----- .../transmogrifier/video_handling_test.exs | 70 ++++++++++--------- test/pleroma/web/common_api/utils_test.exs | 79 +++++++++++++++++----- .../controllers/media_controller_test.exs | 6 +- test/support/factory.ex | 9 +++ 9 files changed, 183 insertions(+), 110 deletions(-) diff --git a/lib/pleroma/media.ex b/lib/pleroma/media.ex index b9b001366..965041809 100644 --- a/lib/pleroma/media.ex +++ b/lib/pleroma/media.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Media do alias Pleroma.User @derive {Jason.Encoder, - only: [:href, :type, :media_type, :name, :blurhash, :meta, :object_id, :user_id]} + only: [:href, :type, :media_type, :name, :blurhash, :meta, :object_id, :actor]} @type t() :: %__MODULE__{} @@ -19,16 +19,16 @@ defmodule Pleroma.Media do field(:name, :string) field(:blurhash, :string) field(:meta, :map) + field(:actor, :string) field(:removable, :boolean, virtual: true, default: false) belongs_to(:object, Pleroma.Object) - belongs_to(:user, Pleroma.User, type: FlakeId.Ecto.CompatType) timestamps() end - def create_from_object_data(%{"url" => [url]} = data, %{user: user} = opts) do + def create_from_object_data(%{"url" => [url]} = data, %{actor: actor} = opts) do object_id = get_in(opts, [:object, "id"]) || Map.get(opts, :object_id) %Media{} @@ -39,7 +39,7 @@ defmodule Pleroma.Media do name: data["name"], blurhash: nil, meta: %{}, - user_id: user.id, + actor: actor, object_id: object_id }) |> Repo.insert() @@ -49,7 +49,12 @@ defmodule Pleroma.Media do def get_by_id(id), do: Repo.get(Media, id) @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{actor: ap_id}, %User{ap_id: ap_id}), do: :ok + + def authorize_access(_media, %User{is_admin: is_admin?, is_moderator: is_moderator?}) + when true in [is_admin?, is_moderator?], + do: :ok + def authorize_access(_media, _user), do: {:error, :forbidden} def update(%Media{} = media, attrs \\ %{}) do @@ -58,10 +63,6 @@ defmodule Pleroma.Media do |> Repo.update() end - def from_object(%Pleroma.Object{data: data}, %{user: user}) do - %Media{href: data["href"], user_id: user.id} - end - def insert(%Media{} = media) do media |> changeset() @@ -70,7 +71,7 @@ defmodule Pleroma.Media do def changeset(struct, params \\ %{}) do struct - |> cast(params, [:href, :type, :media_type, :name, :blurhash, :meta, :user_id, :object_id]) + |> cast(params, [:href, :type, :media_type, :name, :blurhash, :meta, :actor, :object_id]) |> validate_required([:href, :type, :media_type]) end @@ -88,7 +89,7 @@ defmodule Pleroma.Media do "type" => "Document", "blurhash" => media.blurhash, "mediaType" => media.media_type, - "actor" => User.get_by_id(media.user_id).ap_id + "actor" => media.actor } end end diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index a794b13c2..fb61d4796 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -52,7 +52,7 @@ defmodule Pleroma.Object do def create(data) do Object.change(%Object{}, %{data: data}) |> Repo.insert() - |> maybe_handle_attachments() + |> maybe_set_media_object_id() end def change(struct, params \\ %{}) do @@ -60,6 +60,7 @@ defmodule Pleroma.Object do |> cast(params, [:data]) |> validate_required([:data]) |> unique_constraint(:ap_id, name: :objects_unique_apid_index) + |> maybe_create_media() end def get_by_id(nil), do: nil @@ -352,32 +353,45 @@ 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 + defp maybe_create_media( + %{ + valid?: true, + changes: %{data: %{"actor" => actor, "attachment" => [_ | _] = attachments}} + } = changeset ) 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 + new_attachments = + Enum.map(attachments, fn attachment -> + if is_nil(attachment["id"]) do + {:ok, media} = Media.create_from_object_data(attachment, %{actor: actor}) + + Map.put(attachment, "id", media.id) + else + attachment + end + end) + + %{ + changeset + | changes: %{ + changeset.changes + | data: %{changeset.changes.data | "attachment" => new_attachments} + } + } + end - object + defp maybe_create_media(changeset), do: changeset + + defp maybe_set_media_object_id( + {:ok, %Object{id: object_id, data: %{"attachment" => [_ | _] = attachments}}} = result + ) do + Enum.each(attachments, fn %{"id" => media_id} -> + media_id + |> Media.get_by_id() + |> Media.update(%{object_id: object_id}) end) result end - defp maybe_handle_attachments(result), do: result + defp maybe_set_media_object_id(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 4322fc729..cf0cc604c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1202,7 +1202,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do def upload(file, opts \\ []) do with {:ok, data} <- Upload.store(file, opts), %User{} = user <- opts[:user] do - Pleroma.Media.create_from_object_data(data, %{user: user}) + Pleroma.Media.create_from_object_data(data, %{actor: user.ap_id}) 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 ecb632b75..4f0a186eb 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -272,12 +272,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do end end - def inbox(conn, params) do - IO.inspect(%{conn: conn, params: params}) - inbox2(conn, params) - end - - def inbox2(%{assigns: %{valid_signature: true}} = conn, %{"nickname" => nickname} = params) do + def inbox(%{assigns: %{valid_signature: true}} = conn, %{"nickname" => nickname} = params) do with %User{} = recipient <- User.get_cached_by_nickname(nickname), {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(params["actor"]), true <- Utils.recipient_in_message(recipient, actor, params), @@ -287,13 +282,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do end end - def inbox2(%{assigns: %{valid_signature: true}} = conn, params) do + def inbox(%{assigns: %{valid_signature: true}} = conn, params) do Federator.incoming_ap_doc(params) json(conn, "ok") end # POST /relay/inbox -or- POST /internal/fetch/inbox - def inbox2(conn, params) do + def inbox(conn, params) do if params["type"] == "Create" && FederatingPlug.federating?() do post_inbox_relayed_create(conn, params) else diff --git a/test/pleroma/web/activity_pub/transmogrifier/audio_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/audio_handling_test.exs index e733f167d..827cdb0dd 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/audio_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/audio_handling_test.exs @@ -65,21 +65,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.AudioHandlingTest do assert object.data["url"] == "https://channels.tests.funkwhale.audio/library/tracks/74" - assert object.data["attachment"] == [ - %{ - "mediaType" => "audio/ogg", - "type" => "Link", - "name" => nil, - "blurhash" => nil, - "url" => [ - %{ - "href" => - "https://channels.tests.funkwhale.audio/api/v1/listen/3901e5d8-0445-49d5-9711-e096cf32e515/?upload=42342395-0208-4fee-a38d-259a6dae0871&download=false", - "mediaType" => "audio/ogg", - "type" => "Link" - } - ] - } - ] + assert match?( + [ + %{ + "mediaType" => "audio/ogg", + "type" => "Link", + "name" => nil, + "blurhash" => nil, + "url" => [ + %{ + "href" => + "https://channels.tests.funkwhale.audio/api/v1/listen/3901e5d8-0445-49d5-9711-e096cf32e515/?upload=42342395-0208-4fee-a38d-259a6dae0871&download=false", + "mediaType" => "audio/ogg", + "type" => "Link" + } + ] + } + ], + object.data["attachment"] + ) end end diff --git a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs index c00df6a04..c8ef354f4 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs @@ -49,22 +49,25 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.VideoHandlingTest do assert object.data["url"] == "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" - assert object.data["attachment"] == [ - %{ - "type" => "Link", - "mediaType" => "video/mp4", - "name" => nil, - "blurhash" => nil, - "url" => [ - %{ - "href" => - "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", - "mediaType" => "video/mp4", - "type" => "Link" - } - ] - } - ] + assert match?( + [ + %{ + "type" => "Link", + "mediaType" => "video/mp4", + "name" => nil, + "blurhash" => nil, + "url" => [ + %{ + "href" => + "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", + "mediaType" => "video/mp4", + "type" => "Link" + } + ] + } + ], + object.data["attachment"] + ) data = File.read!("test/fixtures/tesla_mock/framatube.org-video.json") |> Jason.decode!() @@ -72,22 +75,25 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.VideoHandlingTest do assert object = Object.normalize(activity, fetch: false) - assert object.data["attachment"] == [ - %{ - "type" => "Link", - "mediaType" => "video/mp4", - "name" => nil, - "blurhash" => nil, - "url" => [ - %{ - "href" => - "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4", - "mediaType" => "video/mp4", - "type" => "Link" - } - ] - } - ] + assert match?( + [ + %{ + "type" => "Link", + "mediaType" => "video/mp4", + "name" => nil, + "blurhash" => nil, + "url" => [ + %{ + "href" => + "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4", + "mediaType" => "video/mp4", + "type" => "Link" + } + ] + } + ], + object.data["attachment"] + ) assert object.data["url"] == "https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206" diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs index f2043e152..5cb106b36 100644 --- a/test/pleroma/web/common_api/utils_test.exs +++ b/test/pleroma/web/common_api/utils_test.exs @@ -511,31 +511,76 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do end test "returns list attachments with desc" do - object = insert(:note) - desc = Jason.encode!(%{object.id => "test-desc"}) - - assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [ - Map.merge(object.data, %{"name" => "test-desc"}) - ] + media = insert(:media) + desc = Jason.encode!(%{media.id => "test-desc"}) + + assert match?( + [ + %{ + "mediaType" => "image/png", + "name" => "test-desc", + "type" => "Document", + "url" => [ + %{ + "href" => "https://pleroma.social/images/pleroma_tan_2.1_cofe.png", + "mediaType" => "image/png", + "type" => "Link" + } + ] + } + ], + Utils.attachments_from_ids_descs(["#{media.id}", "34"], desc) + ) end end describe "attachments_from_ids/1" do test "returns attachments with descs" do - object = insert(:note) - desc = Jason.encode!(%{object.id => "test-desc"}) - - assert Utils.attachments_from_ids(%{ - media_ids: ["#{object.id}"], - descriptions: desc - }) == [ - Map.merge(object.data, %{"name" => "test-desc"}) - ] + media = insert(:media) + desc = Jason.encode!(%{media.id => "test-desc"}) + + assert match?( + [ + %{ + "mediaType" => "image/png", + "name" => "test-desc", + "type" => "Document", + "url" => [ + %{ + "href" => "https://pleroma.social/images/pleroma_tan_2.1_cofe.png", + "mediaType" => "image/png", + "type" => "Link" + } + ] + } + ], + Utils.attachments_from_ids(%{ + media_ids: ["#{media.id}"], + descriptions: desc + }) + ) end test "returns attachments without descs" do - object = insert(:note) - assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data] + media = insert(:media) + + assert match?( + [ + %{ + "mediaType" => "image/png", + "name" => nil, + "type" => "Document", + "url" => [ + %{ + "href" => "https://pleroma.social/images/pleroma_tan_2.1_cofe.png", + "mediaType" => "image/png", + "type" => "Link" + } + ] + } + ], + Utils.attachments_from_ids(%{media_ids: ["#{media.id}"]}) + ) end test "returns [] when not pass media_ids" do 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 c7470d3df..b285bca7a 100644 --- a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs @@ -38,7 +38,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do assert media["id"] media = Media.get_by_id(media["id"]) - assert media.user_id == conn.assigns[:user].id + assert media.actor == conn.assigns[:user].ap_id end test "/api/v2/media", %{conn: conn, user: user, image: image} do @@ -64,7 +64,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do assert media["id"] media = Media.get_by_id(media["id"]) - assert media.user_id == user.id + assert media.actor == user.ap_id end end @@ -134,7 +134,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest 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 media.user_id == user.id + assert media.actor == user.ap_id refute user.id == other_user.id conn diff --git a/test/support/factory.ex b/test/support/factory.ex index bf9592064..2f5aa970e 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -458,4 +458,13 @@ defmodule Pleroma.Factory do phrase: "cofe" } end + + def media_factory do + %Pleroma.Media{ + href: "https://pleroma.social/images/pleroma_tan_2.1_cofe.png", + type: "Link", + media_type: "image/png", + actor: build(:user).ap_id + } + end end -- cgit v1.2.3