Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
authorlain <lain@soykaf.club>
Thu, 23 Apr 2020 13:47:08 +0000 (15:47 +0200)
committerlain <lain@soykaf.club>
Thu, 23 Apr 2020 13:47:08 +0000 (15:47 +0200)
1  2 
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/side_effects.ex
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs

index 2effb74f8bcf545655837c0d5ba5a92ab1e4f934,5981e754567e0c098340a1c77395b4402c87dc1f..ebe3071b08fc55ffe48dd5225ed950a517654489
@@@ -17,24 -15,19 +17,29 @@@ defmodule Pleroma.Web.ActivityPub.SideE
    # - Add like to object
    # - Set up notification
    def handle(%{data: %{"type" => "Like"}} = object, meta) do
-     liked_object = Object.get_by_ap_id(object.data["object"])
-     Utils.add_like_to_object(object, liked_object)
+     {:ok, result} =
+       Pleroma.Repo.transaction(fn ->
+         liked_object = Object.get_by_ap_id(object.data["object"])
+         Utils.add_like_to_object(object, liked_object)
  
-     Notification.create_notifications(object)
+         Notification.create_notifications(object)
  
-     {:ok, object, meta}
+         {:ok, object, meta}
+       end)
+     result
    end
  
 +  def handle(%{data: %{"type" => "Create", "object" => object_id}} = activity, meta) do
 +    object = Object.get_by_ap_id(object_id)
 +
 +    {:ok, _object} = handle_object_creation(object)
 +
 +    Notification.create_notifications(activity)
 +
 +    {:ok, activity, meta}
 +  end
 +
    # Nothing to do
    def handle(object, meta) do
      {:ok, object, meta}
index 86b3648ac2f05806707e67955f4c082da818e565,e87193c83bbcd6f7e6910a0d958a770a428d5da0..0f8860ec524d1747f9b8a946902f1e13fd9eb96d
@@@ -22,55 -21,33 +22,82 @@@ defmodule Pleroma.Web.CommonAPITest d
    setup do: clear_config([:instance, :limit])
    setup do: clear_config([:instance, :max_pinned_statuses])
  
 +  describe "posting chat messages" do
 +    setup do: clear_config([:instance, :chat_limit])
 +
 +    test "it posts a chat message" do
 +      author = insert(:user)
 +      recipient = insert(:user)
 +
 +      {:ok, activity} =
 +        CommonAPI.post_chat_message(
 +          author,
 +          recipient,
 +          "a test message <script>alert('uuu')</script> :firefox:"
 +        )
 +
 +      assert activity.data["type"] == "Create"
 +      assert activity.local
 +      object = Object.normalize(activity)
 +
 +      assert object.data["type"] == "ChatMessage"
 +      assert object.data["to"] == [recipient.ap_id]
 +
 +      assert object.data["content"] ==
 +               "a test message &lt;script&gt;alert(&#39;uuu&#39;)&lt;/script&gt; :firefox:"
 +
 +      assert object.data["emoji"] == %{
 +               "firefox" => "http://localhost:4001/emoji/Firefox.gif"
 +             }
 +
 +      assert Chat.get(author.id, recipient.ap_id)
 +      assert Chat.get(recipient.id, author.ap_id)
 +    end
 +
 +    test "it reject messages over the local limit" do
 +      Pleroma.Config.put([:instance, :chat_limit], 2)
 +
 +      author = insert(:user)
 +      recipient = insert(:user)
 +
 +      {:error, message} =
 +        CommonAPI.post_chat_message(
 +          author,
 +          recipient,
 +          "123"
 +        )
 +
 +      assert message == :content_too_long
 +    end
 +  end
 +
+   test "favoriting race condition" do
+     user = insert(:user)
+     users_serial = insert_list(10, :user)
+     users = insert_list(10, :user)
+     {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
+     users_serial
+     |> Enum.map(fn user ->
+       CommonAPI.favorite(user, activity.id)
+     end)
+     object = Object.get_by_ap_id(activity.data["object"])
+     assert object.data["like_count"] == 10
+     users
+     |> Enum.map(fn user ->
+       Task.async(fn ->
+         CommonAPI.favorite(user, activity.id)
+       end)
+     end)
+     |> Enum.map(&Task.await/1)
+     object = Object.get_by_ap_id(activity.data["object"])
+     assert object.data["like_count"] == 20
+   end
    test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})