Merge remote-tracking branch 'upstream/develop' into attachment-meta
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
index c4b11a65540ecf923a81768b3cbbaf4cefb2f17a..d1a0867e261494e83d8e1cd92b46611a0376930f 100644 (file)
@@ -244,6 +244,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
               "type" => Map.get(url || %{}, "type", "Link")
             }
             |> Maps.put_if_present("mediaType", media_type)
+            |> Maps.put_if_present("width", (url || %{})["width"] || data["width"])
+            |> Maps.put_if_present("height", (url || %{})["height"] || data["height"])
 
           %{
             "url" => [attachment_url],
@@ -534,7 +536,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   end
 
   def handle_incoming(%{"type" => type} = data, _options)
-      when type in ~w{Like EmojiReact Announce} do
+      when type in ~w{Like EmojiReact Announce Add Remove} do
     with :ok <- ObjectValidator.fetch_actor_and_object(data),
          {:ok, activity, _meta} <-
            Pipeline.common_pipeline(data, local: false) do
@@ -556,28 +558,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     end
   end
 
-  def handle_incoming(%{"type" => type} = data, _options) when type in ~w(Add Remove) do
-    with {:ok, %User{} = user} <- ObjectValidator.fetch_actor(data),
-         # maybe locally user doesn't have featured_address
-         {:ok, user} <- maybe_refetch_user(user),
-         %Object{} <- Object.normalize(data["object"], fetch: true) do
-      # Mastodon sends pin/unpin objects without id, to, cc fields
-      data =
-        data
-        |> Map.put_new("id", Utils.generate_activity_id())
-        |> Map.put_new("to", [Pleroma.Constants.as_public()])
-        |> Map.put_new("cc", [user.follower_address])
-
-      case Pipeline.common_pipeline(data,
-             local: false,
-             featured_address: user.featured_address
-           ) do
-        {:ok, activity, _meta} -> {:ok, activity}
-        error -> error
-      end
-    end
-  end
-
   def handle_incoming(
         %{"type" => "Delete"} = data,
         _options
@@ -586,7 +566,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
            Pipeline.common_pipeline(data, local: false) do
       {:ok, activity}
     else
-      {:error, {:validate_object, _}} = e ->
+      {:error, {:validate, _}} = e ->
         # Check if we have a create activity for this
         with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
              %Activity{data: %{"actor" => actor}} <-
@@ -671,12 +651,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
   def handle_incoming(_, _), do: :error
 
-  defp maybe_refetch_user(%User{featured_address: address} = user) when is_binary(address) do
-    {:ok, user}
-  end
-
-  defp maybe_refetch_user(%User{ap_id: ap_id}), do: upgrade_user_from_ap_id(ap_id)
-
   @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
   def get_obj_helper(id, options \\ []) do
     options = Keyword.put(options, :fetch, true)
@@ -977,7 +951,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       object
       |> Map.get("attachment", [])
       |> Enum.map(fn data ->
-        [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
+        [%{"mediaType" => media_type, "href" => href} = url | _] = data["url"]
 
         %{
           "url" => href,
@@ -985,6 +959,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
           "name" => data["name"],
           "type" => "Document"
         }
+        |> Maps.put_if_present("width", url["width"])
+        |> Maps.put_if_present("height", url["height"])
       end)
 
     Map.put(object, "attachment", attachments)