Merge remote-tracking branch 'origin/develop' into reactions
authorlain <lain@soykaf.club>
Tue, 27 Aug 2019 21:38:51 +0000 (16:38 -0500)
committerlain <lain@soykaf.club>
Tue, 27 Aug 2019 21:38:51 +0000 (16:38 -0500)
1  2 
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/utils.ex
test/web/activity_pub/activity_pub_test.exs

index d57830da3e7575188a582ff0131758820bbe5fa3,c9c0c376335f5bc43b627f8a35b4589faac7d428..32e22d8d7bd102084f697b5ddea5541b3356c508
@@@ -279,33 -271,13 +271,23 @@@ defmodule Pleroma.Web.ActivityPub.Util
    Returns like activities targeting an object
    """
    def get_object_likes(%{data: %{"id" => id}}) do
-     query =
-       from(
-         activity in Activity,
-         # this is to use the index
-         where:
-           fragment(
-             "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
-             activity.data,
-             activity.data,
-             ^id
-           ),
-         where: fragment("(?)->>'type' = 'Like'", activity.data)
-       )
-     Repo.all(query)
+     id
+     |> Activity.Queries.by_object_id()
+     |> Activity.Queries.by_type("Like")
+     |> Repo.all()
    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
 +
+   @spec make_like_data(User.t(), map(), String.t()) :: map()
    def make_like_data(
          %User{ap_id: ap_id} = actor,
          %{data: %{"actor" => object_actor_id, "id" => id}} = object,
index cf93f624a00f66f8ce32a25bcc4bed3e49d26090,f72b44aed4c0f531b6ebda36e778da5fc4e7c825..b168e85a014741f560e2c20fd0086704d6b4b79d
@@@ -675,29 -677,30 +677,52 @@@ defmodule Pleroma.Web.ActivityPub.Activ
      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_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
+       Pleroma.Config.put([:instance, :federating], true)
+       note_activity = insert(:note_activity)
+       assert object_activity = Object.normalize(note_activity)
+       user = insert(:user)
+       {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
+       assert called(Pleroma.Web.Federator.publish(like_activity, 5))
+     end
+     test "returns exist activity if object already liked" do
+       note_activity = insert(:note_activity)
+       assert object_activity = Object.normalize(note_activity)
+       user = insert(:user)
+       {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
+       {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
+       assert like_activity == like_activity_exist
+     end
      test "adds a like activity to the db" do
        note_activity = insert(:note_activity)
        assert object = Object.normalize(note_activity)