ActivityPub: Basic EmojiReactions.
authorlain <lain@soykaf.club>
Mon, 26 Aug 2019 21:29:51 +0000 (16:29 -0500)
committerlain <lain@soykaf.club>
Mon, 26 Aug 2019 21:29:51 +0000 (16:29 -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 172c952d498ed18ee885212410f7ce5c379fc552..a6088a012c5232b1455a38c2445b39fc9297ff53 100644 (file)
@@ -312,6 +312,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     end
   end
 
+  def react_with_emoji(user, object, emoji, options \\ []) do
+    with local <- Keyword.get(options, :local, true),
+         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, object}
+    end
+  end
+
   # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
   def like(
         %User{ap_id: ap_id} = user,
index 1c305865841fe3dd03d36d077b86e4b11353d8a0..d57830da3e7575188a582ff0131758820bbe5fa3 100644 (file)
@@ -296,6 +296,16 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     Repo.all(query)
   end
 
+  def is_emoji?(emoji) do
+    String.length(emoji) == 1
+  end
+
+  def make_emoji_reaction_data(user, object, emoji, activity_id) do
+    make_like_data(user, object, activity_id)
+    |> Map.put("type", "EmojiReaction")
+    |> Map.put("content", emoji)
+  end
+
   def make_like_data(
         %User{ap_id: ap_id} = actor,
         %{data: %{"actor" => object_actor_id, "id" => id}} = object,
index 1515f4eb6a677d62602e673693e8b8266c758803..cf93f624a00f66f8ce32a25bcc4bed3e49d26090 100644 (file)
@@ -675,6 +675,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  describe "react to an object" do
+    test "adds an emoji reaction activity to the db" do
+      user = insert(:user)
+      reactor = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
+      assert object = Object.normalize(activity)
+
+      {:ok, reaction_activity, object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
+
+      assert reaction_activity
+
+      assert reaction_activity.data["actor"] == reactor.ap_id
+      assert reaction_activity.data["type"] == "EmojiReaction"
+      assert reaction_activity.data["content"] == "🔥"
+      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]
+    end
+  end
+
   describe "like an object" do
     test "adds a like activity to the db" do
       note_activity = insert(:note_activity)