ActivityPub: Save emoji reactions in object.
authorlain <lain@soykaf.club>
Tue, 3 Sep 2019 21:50:04 +0000 (16:50 -0500)
committerlain <lain@soykaf.club>
Tue, 3 Sep 2019 21:50:04 +0000 (16:50 -0500)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/utils.ex
test/web/activity_pub/activity_pub_test.exs

index 532db17c466303f7a3361ca4b947acfc8c609229..6cd1684275973c942eaaa32c77ff5f344abc1104 100644 (file)
@@ -317,7 +317,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
          activity_id <- Keyword.get(options, :activity_id, nil),
          is_emoji?(emoji),
          reaction_data <- make_emoji_reaction_data(user, object, emoji, activity_id),
-         {:ok, activity} <- insert(reaction_data, local) do
+         {:ok, activity} <- insert(reaction_data, local),
+         {:ok, object} <- add_emoji_reaction_to_object(activity, object) do
       {:ok, activity, object}
     end
   end
index 32e22d8d7bd102084f697b5ddea5541b3356c508..1e6a67debc1b84db69bf7f575b3460ec5087d2d6 100644 (file)
@@ -321,10 +321,21 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   @spec update_element_in_object(String.t(), list(any), Object.t()) ::
           {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
   def update_element_in_object(property, element, object) do
+    length =
+      if is_map(element) do
+        element
+        |> Map.values()
+        |> List.flatten()
+        |> length()
+      else
+        element
+        |> length()
+      end
+
     data =
       Map.merge(
         object.data,
-        %{"#{property}_count" => length(element), "#{property}s" => element}
+        %{"#{property}_count" => length, "#{property}s" => element}
       )
 
     object
@@ -332,6 +343,20 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     |> Object.update_and_set_cache()
   end
 
+  @spec add_emoji_reaction_to_object(Activity.t(), Object.t()) ::
+          {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
+
+  def add_emoji_reaction_to_object(
+        %Activity{data: %{"content" => emoji, "actor" => actor}},
+        object
+      ) do
+    reactions = object.data["reactions"] || %{}
+    emoji_actors = reactions[emoji] || []
+    new_emoji_actors = [actor | emoji_actors] |> Enum.uniq()
+    new_reactions = Map.put(reactions, emoji, new_emoji_actors)
+    update_element_in_object("reaction", new_reactions, object)
+  end
+
   @spec add_like_to_object(Activity.t(), Object.t()) ::
           {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
   def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
index b168e85a014741f560e2c20fd0086704d6b4b79d..67dfb9394b93e6aa4e5df28c80a60c32fb099bc5 100644 (file)
@@ -694,8 +694,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert reaction_activity.data["object"] == object.data["id"]
       assert reaction_activity.data["to"] == [User.ap_followers(reactor), activity.data["actor"]]
       assert reaction_activity.data["context"] == object.data["context"]
-      assert object.data["reaction_count"] == 1
-      # assert object.data["reactions"] == [user.ap_id]
+      assert object.data["reaction_count"] == 1
+      assert object.data["reactions"]["🔥"] == [reactor.ap_id]
     end
   end