Fix MastoAPI.AuthControllerTest, json_response(:no_content) --> empty_json_response()
[akkoma] / lib / pleroma / web / activity_pub / object_validators / attachment_validator.ex
index 16ed4905182fba55d54ac94d700118db052e154e..c8b1482802dd1b01202e3ced6e2d1ee23a46f88e 100644 (file)
@@ -12,7 +12,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
   @primary_key false
   embedded_schema do
     field(:type, :string)
-    field(:mediaType, :string)
+    field(:mediaType, :string, default: "application/octet-stream")
     field(:name, :string)
 
     embeds_many(:url, UrlObjectValidator)
@@ -41,26 +41,34 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
   end
 
   def fix_media_type(data) do
-    data
-    |> Map.put_new("mediaType", data["mimeType"])
+    data = Map.put_new(data, "mediaType", data["mimeType"])
+
+    if MIME.valid?(data["mediaType"]) do
+      data
+    else
+      Map.put(data, "mediaType", "application/octet-stream")
+    end
   end
 
-  def fix_url(data) do
-    case data["url"] do
-      url when is_binary(url) ->
-        data
-        |> Map.put(
-          "url",
-          [
-            %{
-              "href" => url,
-              "type" => "Link",
-              "mediaType" => data["mediaType"]
-            }
-          ]
-        )
-
-      _ ->
+  defp handle_href(href, mediaType) do
+    [
+      %{
+        "href" => href,
+        "type" => "Link",
+        "mediaType" => mediaType
+      }
+    ]
+  end
+
+  defp fix_url(data) do
+    cond do
+      is_binary(data["url"]) ->
+        Map.put(data, "url", handle_href(data["url"], data["mediaType"]))
+
+      is_binary(data["href"]) and data["url"] == nil ->
+        Map.put(data, "url", handle_href(data["href"], data["mediaType"]))
+
+      true ->
         data
     end
   end