Create Object.hashtags/1 wrapper
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
index 39c8f7e396ac064afc69d185613aaa28f59b53e5..36ef6a454e54a0cc8ca38c0844532f3a68836a43 100644 (file)
@@ -252,6 +252,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
           }
           |> Maps.put_if_present("mediaType", media_type)
           |> Maps.put_if_present("name", data["name"])
+          |> Maps.put_if_present("blurhash", data["blurhash"])
         else
           nil
         end
@@ -311,16 +312,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   def fix_emoji(object), do: object
 
   def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
-    tags =
+    hashtags =
       tag
       |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
-      |> Enum.map(fn %{"name" => name} ->
-        name
-        |> String.slice(1..-1)
-        |> String.downcase()
+      |> Enum.map(fn
+        %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
+        %{"name" => hashtag} -> String.downcase(hashtag)
       end)
 
-    Map.put(object, "tag", tag ++ tags)
+    Map.put(object, "hashtags", hashtags)
   end
 
   def fix_tag(%{"tag" => %{} = tag} = object) do
@@ -863,23 +863,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   def maybe_fix_object_url(data), do: data
 
   def add_hashtags(object) do
-    tags =
-      (object["tag"] || [])
-      |> Enum.map(fn
-        # Expand internal representation tags into AS2 tags.
-        tag when is_binary(tag) ->
-          %{
-            "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
-            "name" => "##{tag}",
-            "type" => "Hashtag"
-          }
-
-        # Do not process tags which are already AS2 tag objects.
-        tag when is_map(tag) ->
-          tag
+    hashtags =
+      object
+      |> Object.hashtags()
+      |> Enum.map(fn tag ->
+        %{
+          "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
+          "name" => "##{tag}",
+          "type" => "Hashtag"
+        }
       end)
 
-    Map.put(object, "tag", tags)
+    Map.put(object, "tag", hashtags ++ (object["tag"] || []))
   end
 
   # TODO These should be added on our side on insertion, it doesn't make much
@@ -935,7 +930,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   end
 
   def set_sensitive(object) do
-    tags = object["tag"] || []
+    tags = object["hashtags"] || object["tag"] || []
     Map.put(object, "sensitive", "nsfw" in tags)
   end
 
@@ -1007,7 +1002,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
   def upgrade_user_from_ap_id(ap_id) do
     with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
-         {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id, force_http: true),
+         {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
          {:ok, user} <- update_user(user, data) do
       TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
       {:ok, user}