transmogrifier: Use oneliners when applicable
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
index ed5df6f7bd773f267e4944cc35dae07587f17e36..29d7b642b8e5b9ec438646784fa8dfccd4909e4c 100644 (file)
@@ -21,13 +21,13 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     if is_binary(Enum.at(actor, 0)) do
       Enum.at(actor, 0)
     else
-      Enum.find(actor, fn %{"type" => type} -> type == "Person" end)
+      Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
       |> Map.get("id")
     end
   end
 
-  def get_actor(%{"actor" => actor}) when is_map(actor) do
-    actor["id"]
+  def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
+    id
   end
 
   @doc """
@@ -100,11 +100,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       when not is_nil(in_reply_to) do
     in_reply_to_id =
       cond do
-        is_bitstring(in_reply_to) -> in_reply_to
-        is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) -> in_reply_to["id"]
-        is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) -> Enum.at(in_reply_to, 0)
+        is_bitstring(in_reply_to) ->
+          in_reply_to
+
+        is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
+          in_reply_to["id"]
+
+        is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
+          Enum.at(in_reply_to, 0)
+
         # Maybe I should output an error too?
-        true -> ""
+        true ->
+          ""
       end
 
     case ActivityPub.fetch_object_from_id(in_reply_to_id) do
@@ -160,9 +167,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     Map.put(object, "attachment", attachment)
   end
 
-  def fix_attachments(object) do
-    object
-  end
+  def fix_attachments(object), do: object
 
   def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
     emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
@@ -190,9 +195,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     |> Map.put("emoji", emoji)
   end
 
-  def fix_emoji(object) do
-    object
-  end
+  def fix_emoji(object), do: object
 
   def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
     tags =
@@ -206,10 +209,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     |> Map.put("tag", combined)
   end
 
-  def fix_tag(object) do
+  def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
+    combined = [tag, String.slice(hashtag, 1..-1)]
+
     object
+    |> Map.put("tag", combined)
   end
 
+  def fix_tag(object), do: object
+
   # content map usually only has one language so this will do for now.
   def fix_content_map(%{"contentMap" => content_map} = object) do
     content_groups = Map.to_list(content_map)